Skip to content

Commit bb463db

Browse files
committed
tests a bit less confusing
1 parent 5c2a074 commit bb463db

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

spec/fixed-footnotes-spec.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ var utilStub = {};
33
var footnotes = proxyquire('../src/fixed-footnotes', { "./util": utilStub });
44
var jsdom = require("jsdom");
55

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+
615
describe("fixed-footnotes.constructor", function() {
716

817
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) {
1019
global.window = w;
1120
footnotes();
1221
expect(w.$("body > *:last").hasClass("fixed-footnotes-container")).toBe(true);
@@ -15,24 +24,39 @@ describe("fixed-footnotes.constructor", function() {
1524
});
1625

1726
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) {
1928
footnotes({}, w);
2029
expect(w.$(".fixed-footnotes-container").length).toBe(1);
2130
done();
2231
});
2332
});
2433

2534
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) {
2736
spyOn(utilStub, "isElementInViewport").and.returnValues(true, false); // reference visible, note invisible
2837
footnotes({}, w);
2938
expect(w.$(".fixed-footnotes-note").length).toBe(1);
3039
done();
3140
});
3241
});
3342

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+
3458
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) {
3660
spyOn(utilStub, "isElementInViewport").and.returnValues(false); // reference invisible
3761
footnotes({}, w);
3862
expect(w.$(".fixed-footnotes-note").length).toBe(0);
@@ -41,7 +65,7 @@ describe("fixed-footnotes.constructor", function() {
4165
});
4266

4367
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) {
4569
spyOn(utilStub, "isElementInViewport").and.returnValues(true, true); // reference visible, note visible
4670
footnotes({}, w);
4771
expect(w.$(".fixed-footnotes-note").length).toBe(0);
@@ -50,7 +74,7 @@ describe("fixed-footnotes.constructor", function() {
5074
});
5175

5276
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) {
5478
spyOn(utilStub, "isElementInViewport").and.returnValues(true, false); // reference visible, note invisible
5579
footnotes({
5680
referencesSelector: ".myReference",
@@ -72,7 +96,7 @@ describe("fixed-footnotes.constructor", function() {
7296
});
7397

7498
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) {
76100
spyOn(utilStub, "isElementInViewport").and.returnValues(true, false); // reference visible, note invisible
77101
footnotes({}, w);
78102
expect(w.$(".fixed-footnotes-note").length).toBe(0);
@@ -85,7 +109,7 @@ describe("fixed-footnotes.constructor", function() {
85109
describe("fixed-footnotes.stop", function() {
86110

87111
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) {
89113
spyOn(utilStub, "isElementInViewport").and.returnValues(true, false); // reference visible, note invisible
90114
var ffn = footnotes({}, w);
91115
expect(w.$(".fixed-footnotes-container").length).toBe(1);
@@ -102,7 +126,7 @@ describe("fixed-footnotes.stop", function() {
102126
describe("fixed-footnotes.refresh", function() {
103127

104128
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) {
106130
spyOn(utilStub, "isElementInViewport").and.returnValues(false, // reference invisible
107131
true, false); // reference visible, note invisible
108132
var ffn = footnotes({}, w);
@@ -118,7 +142,7 @@ describe("fixed-footnotes.refresh", function() {
118142
describe("fixed-footnotes.addRefreshListener", function() {
119143

120144
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) {
122146
var ffn = footnotes({}, w);
123147
ffn.addRefreshListener(done);
124148
ffn.refresh();
@@ -130,15 +154,21 @@ describe("fixed-footnotes.addRefreshListener", function() {
130154
describe("fixed-footnotes.removeRefreshListener", function() {
131155

132156
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() {} };
135159
spyOn(someObj, "someFunc");
160+
spyOn(someObj, "someFunc2");
161+
spyOn(someObj, "someFunc3");
136162
var ffn = footnotes({}, w);
137163
ffn.addRefreshListener(someObj.someFunc);
138-
ffn.removeRefreshListener(someObj.someFunc);
164+
ffn.addRefreshListener(someObj.someFunc2);
165+
ffn.addRefreshListener(someObj.someFunc3);
166+
ffn.removeRefreshListener(someObj.someFunc2);
139167
ffn.refresh();
140168
setTimeout(function() {
141-
expect(someObj.someFunc).not.toHaveBeenCalled();
169+
expect(someObj.someFunc).toHaveBeenCalled();
170+
expect(someObj.someFunc2).not.toHaveBeenCalled();
171+
expect(someObj.someFunc3).toHaveBeenCalled();
142172
done();
143173
}, 20);
144174
});

0 commit comments

Comments
 (0)