@@ -3,10 +3,19 @@ var utilStub = {};
3
3
var footnotes = proxyquire ( '../src/fixed-footnotes' , { "./util" : utilStub } ) ;
4
4
var jsdom = require ( "jsdom" ) ;
5
5
6
+ const EMPTY_BODY = "<body></body>" ;
7
+ const DOC_WITH_ONE_REF = "<body><p class='reference' href='#note'>reference</p><p id='note'>note</p></body>" ;
8
+
9
+ var createDomEnv = function ( html , callback ) {
10
+ jsdom . env ( html , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
11
+ callback ( w ) ;
12
+ } ) ;
13
+ }
14
+
6
15
describe ( "fixed-footnotes.constructor" , function ( ) {
7
16
8
17
it ( "should create a container as the last node of body" , function ( done ) {
9
- jsdom . env ( "<body></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
18
+ createDomEnv ( EMPTY_BODY , function ( w ) {
10
19
global . window = w ;
11
20
footnotes ( ) ;
12
21
expect ( w . $ ( "body > *:last" ) . hasClass ( "fixed-footnotes-container" ) ) . toBe ( true ) ;
@@ -15,24 +24,39 @@ describe("fixed-footnotes.constructor", function() {
15
24
} ) ;
16
25
17
26
it ( "should use the window passed as parameter" , function ( done ) {
18
- jsdom . env ( "<body></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
27
+ createDomEnv ( EMPTY_BODY , function ( w ) {
19
28
footnotes ( { } , w ) ;
20
29
expect ( w . $ ( ".fixed-footnotes-container" ) . length ) . toBe ( 1 ) ;
21
30
done ( ) ;
22
31
} ) ;
23
32
} ) ;
24
33
25
34
it ( "should display a note if its reference is visible and the original note isn't" , function ( done ) {
26
- jsdom . env ( "<body><p class='reference' href='#note'>reference</p><p id='note'>note</p></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
35
+ createDomEnv ( DOC_WITH_ONE_REF , function ( w ) {
27
36
spyOn ( utilStub , "isElementInViewport" ) . and . returnValues ( true , false ) ; // reference visible, note invisible
28
37
footnotes ( { } , w ) ;
29
38
expect ( w . $ ( ".fixed-footnotes-note" ) . length ) . toBe ( 1 ) ;
30
39
done ( ) ;
31
40
} ) ;
32
41
} ) ;
33
42
43
+ it ( "should display 2 notes if there references are visible and the original notes aren't" , function ( done ) {
44
+ createDomEnv ( "<body> \
45
+ <p class='reference' href='#note'>reference</p> \
46
+ <p class='reference' href='#note2'>reference2</p> \
47
+ <p id='note'>note</p> \
48
+ <p id='note2'>note</p> \
49
+ </body>" , function ( w ) {
50
+ spyOn ( utilStub , "isElementInViewport" ) . and . returnValues ( true , false , // reference visible, note invisible for note 1
51
+ true , false ) ; // reference visible, note invisible for note 2
52
+ footnotes ( { } , w ) ;
53
+ expect ( w . $ ( ".fixed-footnotes-note" ) . length ) . toBe ( 2 ) ;
54
+ done ( ) ;
55
+ } ) ;
56
+ } ) ;
57
+
34
58
it ( "shouldn't display a note if its reference is not visible" , function ( done ) {
35
- jsdom . env ( "<body><p class='reference' href='#note'>reference</p><p id='note'>note</p></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
59
+ createDomEnv ( DOC_WITH_ONE_REF , function ( w ) {
36
60
spyOn ( utilStub , "isElementInViewport" ) . and . returnValues ( false ) ; // reference invisible
37
61
footnotes ( { } , w ) ;
38
62
expect ( w . $ ( ".fixed-footnotes-note" ) . length ) . toBe ( 0 ) ;
@@ -41,7 +65,7 @@ describe("fixed-footnotes.constructor", function() {
41
65
} ) ;
42
66
43
67
it ( "shouldn't display a note if its original note is visible" , function ( done ) {
44
- jsdom . env ( "<body><p class='reference' href='#note'>reference</p><p id='note'>note</p></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
68
+ createDomEnv ( DOC_WITH_ONE_REF , function ( w ) {
45
69
spyOn ( utilStub , "isElementInViewport" ) . and . returnValues ( true , true ) ; // reference visible, note visible
46
70
footnotes ( { } , w ) ;
47
71
expect ( w . $ ( ".fixed-footnotes-note" ) . length ) . toBe ( 0 ) ;
@@ -50,7 +74,7 @@ describe("fixed-footnotes.constructor", function() {
50
74
} ) ;
51
75
52
76
it ( "should take the options into account" , function ( done ) {
53
- jsdom . env ( "<body><p class='myReference' href='#note'>reference</p><p id='note'>note</p><div id='myParent'></div></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
77
+ createDomEnv ( "<body><p class='myReference' href='#note'>reference</p><p id='note'>note</p><div id='myParent'></div></body>" , function ( w ) {
54
78
spyOn ( utilStub , "isElementInViewport" ) . and . returnValues ( true , false ) ; // reference visible, note invisible
55
79
footnotes ( {
56
80
referencesSelector : ".myReference" ,
@@ -72,7 +96,7 @@ describe("fixed-footnotes.constructor", function() {
72
96
} ) ;
73
97
74
98
it ( "shouldn't display a note if we can't find it" , function ( done ) {
75
- jsdom . env ( "<body><p class='reference' href='#note'>reference</p></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
99
+ createDomEnv ( "<body><p class='reference' href='#note'>reference</p></body>" , function ( w ) {
76
100
spyOn ( utilStub , "isElementInViewport" ) . and . returnValues ( true , false ) ; // reference visible, note invisible
77
101
footnotes ( { } , w ) ;
78
102
expect ( w . $ ( ".fixed-footnotes-note" ) . length ) . toBe ( 0 ) ;
@@ -85,7 +109,7 @@ describe("fixed-footnotes.constructor", function() {
85
109
describe ( "fixed-footnotes.stop" , function ( ) {
86
110
87
111
it ( "should remove the footnotes container and all its notes" , function ( done ) {
88
- jsdom . env ( "<body><p class='reference' href='#note'>reference</p><p id='note'>note</p></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
112
+ createDomEnv ( DOC_WITH_ONE_REF , function ( w ) {
89
113
spyOn ( utilStub , "isElementInViewport" ) . and . returnValues ( true , false ) ; // reference visible, note invisible
90
114
var ffn = footnotes ( { } , w ) ;
91
115
expect ( w . $ ( ".fixed-footnotes-container" ) . length ) . toBe ( 1 ) ;
@@ -102,7 +126,7 @@ describe("fixed-footnotes.stop", function() {
102
126
describe ( "fixed-footnotes.refresh" , function ( ) {
103
127
104
128
it ( "should display a note if a previously hidden reference is now visible" , function ( done ) {
105
- jsdom . env ( "<body><p class='reference' href='#note'>reference</p><p id='note'>note</p></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
129
+ createDomEnv ( DOC_WITH_ONE_REF , function ( w ) {
106
130
spyOn ( utilStub , "isElementInViewport" ) . and . returnValues ( false , // reference invisible
107
131
true , false ) ; // reference visible, note invisible
108
132
var ffn = footnotes ( { } , w ) ;
@@ -118,7 +142,7 @@ describe("fixed-footnotes.refresh", function() {
118
142
describe ( "fixed-footnotes.addRefreshListener" , function ( ) {
119
143
120
144
it ( "should add a function executed on refresh" , function ( done ) {
121
- jsdom . env ( "<body></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
145
+ createDomEnv ( EMPTY_BODY , function ( w ) {
122
146
var ffn = footnotes ( { } , w ) ;
123
147
ffn . addRefreshListener ( done ) ;
124
148
ffn . refresh ( ) ;
@@ -130,15 +154,21 @@ describe("fixed-footnotes.addRefreshListener", function() {
130
154
describe ( "fixed-footnotes.removeRefreshListener" , function ( ) {
131
155
132
156
it ( "should remove a function from the listener list" , function ( done ) {
133
- jsdom . env ( "<body></body>" , [ "http://code.jquery.com/jquery.js" ] , function ( err , w ) {
134
- var someObj = { someFunc : ( ) => false } ;
157
+ createDomEnv ( EMPTY_BODY , function ( w ) {
158
+ var someObj = { someFunc : function ( ) { } , someFunc2 : function ( ) { } , someFunc3 : function ( ) { } } ;
135
159
spyOn ( someObj , "someFunc" ) ;
160
+ spyOn ( someObj , "someFunc2" ) ;
161
+ spyOn ( someObj , "someFunc3" ) ;
136
162
var ffn = footnotes ( { } , w ) ;
137
163
ffn . addRefreshListener ( someObj . someFunc ) ;
138
- ffn . removeRefreshListener ( someObj . someFunc ) ;
164
+ ffn . addRefreshListener ( someObj . someFunc2 ) ;
165
+ ffn . addRefreshListener ( someObj . someFunc3 ) ;
166
+ ffn . removeRefreshListener ( someObj . someFunc2 ) ;
139
167
ffn . refresh ( ) ;
140
168
setTimeout ( function ( ) {
141
- expect ( someObj . someFunc ) . not . toHaveBeenCalled ( ) ;
169
+ expect ( someObj . someFunc ) . toHaveBeenCalled ( ) ;
170
+ expect ( someObj . someFunc2 ) . not . toHaveBeenCalled ( ) ;
171
+ expect ( someObj . someFunc3 ) . toHaveBeenCalled ( ) ;
142
172
done ( ) ;
143
173
} , 20 ) ;
144
174
} ) ;
0 commit comments