|
1 |
| -var footnotes = require('../src/fixed-footnotes'); |
| 1 | +var proxyquire = require('proxyquire'); |
| 2 | +var utilStub = {}; |
| 3 | +var footnotes = proxyquire('../src/fixed-footnotes', { "./util": utilStub }); |
2 | 4 | var jsdom = require("jsdom");
|
3 | 5 |
|
4 |
| -describe("fixed-footnotes", function() { |
| 6 | +describe("fixed-footnotes.constructor", function() { |
5 | 7 |
|
6 |
| - it("should work with no argument", function(done) { |
| 8 | + it("should create a container as the last node of body", function(done) { |
7 | 9 | jsdom.env("<body></body>", ["http://code.jquery.com/jquery.js"], function(err, w) {
|
8 | 10 | global.window = w;
|
9 |
| - var ffn = footnotes(); |
| 11 | + footnotes(); |
| 12 | + expect(w.$("body > *:last").hasClass("fixed-footnotes-container")).toBe(true); |
| 13 | + done(); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should use the window passed as parameter", function(done) { |
| 18 | + jsdom.env("<body></body>", ["http://code.jquery.com/jquery.js"], function(err, w) { |
| 19 | + footnotes({}, w); |
10 | 20 | expect(w.$(".fixed-footnotes-container").length).toBe(1);
|
11 | 21 | done();
|
12 | 22 | });
|
13 | 23 | });
|
14 | 24 |
|
15 |
| - it("should properly override default options", function(done) { |
16 |
| - jsdom.env("<body></body>", function(err, w) { |
17 |
| - var ffn = footnotes({ referencesSelector: "test" }, w); |
18 |
| - expect(ffn.options.referencesSelector).toBe("test"); |
19 |
| - expect(ffn.defaultOptions.referencesSelector).toBe(".reference"); |
| 25 | + 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) { |
| 27 | + spyOn(utilStub, "isElementInViewport").and.returnValues(true, false); // reference visible, note invisible |
| 28 | + footnotes({}, w); |
| 29 | + expect(w.$(".fixed-footnotes-note").length).toBe(1); |
20 | 30 | done();
|
21 | 31 | });
|
22 | 32 | });
|
23 | 33 |
|
24 |
| - it("should create a container as the last node of body", function(done) { |
25 |
| - jsdom.env("<body><div></div></body>", ["http://code.jquery.com/jquery.js"], |
26 |
| - function(err, w) { |
27 |
| - var ffn = footnotes({}, w); |
28 |
| - expect(w.$("body > *:last").hasClass("fixed-footnotes-container")).toBe(true); |
| 34 | + 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) { |
| 36 | + spyOn(utilStub, "isElementInViewport").and.returnValues(false); // reference invisible |
| 37 | + footnotes({}, w); |
| 38 | + expect(w.$(".fixed-footnotes-note").length).toBe(0); |
29 | 39 | done();
|
30 | 40 | });
|
31 | 41 | });
|
32 | 42 |
|
33 |
| - it("should create the container properly with the options provided", function(done) { |
34 |
| - jsdom.env("<body><div id='super'></div></body>", ["http://code.jquery.com/jquery.js"], |
35 |
| - function(err, w) { |
36 |
| - var ffn = footnotes({ |
37 |
| - fixedContainerLocation: "#super", |
38 |
| - fixedContainerId: "myId", |
39 |
| - fixedContainerClass: "myClass" |
40 |
| - }, w); |
41 |
| - var $container = w.$("#super > *:last"); |
42 |
| - expect($container.attr("id")).toBe("myId"); |
43 |
| - expect($container.hasClass("myClass")).toBe(true); |
| 43 | + 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) { |
| 45 | + spyOn(utilStub, "isElementInViewport").and.returnValues(true, true); // reference visible, note visible |
| 46 | + footnotes({}, w); |
| 47 | + expect(w.$(".fixed-footnotes-note").length).toBe(0); |
44 | 48 | done();
|
45 | 49 | });
|
46 | 50 | });
|
47 | 51 |
|
48 |
| - it("should find a note given its reference", function(done) { |
49 |
| - jsdom.env("<body><p class='footnote' href='#note'>reference</p><p id='note'>note</p></body>", |
50 |
| - ["http://code.jquery.com/jquery.js"], |
51 |
| - function(err, w) { |
52 |
| - global.window = w; |
53 |
| - var ffn = footnotes({}, w); |
54 |
| - var note = ffn._getNoteFromRef(w.$(".footnote")[0]) |
55 |
| - expect(note.id).toBe("note"); |
| 52 | + 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) { |
| 54 | + spyOn(utilStub, "isElementInViewport").and.returnValues(true, false); // reference visible, note invisible |
| 55 | + footnotes({ |
| 56 | + referencesSelector: ".myReference", |
| 57 | + fixedContainerLocation: "#myParent", |
| 58 | + fixedContainerId: "myContainerId", |
| 59 | + fixedContainerClass: "myContainerClass", |
| 60 | + footnoteClass: "myFootnoteClass", |
| 61 | + transformNote: function(elem) { |
| 62 | + elem.className += " addedClass"; |
| 63 | + return elem; |
| 64 | + } |
| 65 | + }, w); |
| 66 | + expect(w.$("#myParent > #myContainerId").length).toBe(1); |
| 67 | + expect(w.$("#myContainerId").hasClass("myContainerClass")).toBe(true); |
| 68 | + expect(w.$(".myFootnoteClass").length).toBe(1); |
| 69 | + expect(w.$(".addedClass").length).toBe(1); |
56 | 70 | done();
|
57 | 71 | });
|
58 | 72 | });
|
59 | 73 |
|
60 |
| - it("should return null if the note cant be found", function(done) { |
61 |
| - jsdom.env("<body><p class='footnote' href='#note'>reference</p></body>", |
62 |
| - ["http://code.jquery.com/jquery.js"], |
63 |
| - function(err, w) { |
64 |
| - global.window = w; |
65 |
| - var ffn = footnotes({}, w); |
66 |
| - var note = ffn._getNoteFromRef(w.$(".footnote")[0]) |
67 |
| - expect(note).toBe(null); |
| 74 | + 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) { |
| 76 | + spyOn(utilStub, "isElementInViewport").and.returnValues(true, false); // reference visible, note invisible |
| 77 | + footnotes({}, w); |
| 78 | + expect(w.$(".fixed-footnotes-note").length).toBe(0); |
68 | 79 | done();
|
69 | 80 | });
|
70 | 81 | });
|
71 | 82 |
|
72 |
| - it("should add a note to the container", function(done) { |
73 |
| - jsdom.env("<body><p class='footnote' href='#note'>reference</p><p id='note'>note</p></body>", |
74 |
| - ["http://code.jquery.com/jquery.js"], |
75 |
| - function(err, w) { |
76 |
| - global.window = w; |
| 83 | +}); |
| 84 | + |
| 85 | +describe("fixed-footnotes.stop", function() { |
| 86 | + |
| 87 | + 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) { |
| 89 | + spyOn(utilStub, "isElementInViewport").and.returnValues(true, false); // reference visible, note invisible |
77 | 90 | var ffn = footnotes({}, w);
|
78 |
| - ffn._displayNote(w.$("#note")[0]) |
| 91 | + expect(w.$(".fixed-footnotes-container").length).toBe(1); |
79 | 92 | expect(w.$(".fixed-footnotes-note").length).toBe(1);
|
| 93 | + ffn.stop(); |
| 94 | + expect(w.$(".fixed-footnotes-container").length).toBe(0); |
| 95 | + expect(w.$(".fixed-footnotes-note").length).toBe(0); |
80 | 96 | done();
|
81 | 97 | });
|
82 | 98 | });
|
83 | 99 |
|
84 |
| - it("should add the note properly with the options", function(done) { |
85 |
| - jsdom.env("<body><p class='footnote' href='#note'>reference</p><p id='note'>note</p></body>", |
86 |
| - ["http://code.jquery.com/jquery.js"], |
87 |
| - function(err, w) { |
88 |
| - global.window = w; |
89 |
| - var ffn = footnotes({ |
90 |
| - footnoteClass: "fixed-footnotes-note anotherClass", |
91 |
| - transformNote: function(elem) { |
92 |
| - w.$(elem).text(w.$(elem).text().toUpperCase()); |
93 |
| - return elem; |
94 |
| - } |
95 |
| - }, w); |
96 |
| - ffn._displayNote(w.$("#note")[0]) |
| 100 | +}); |
| 101 | + |
| 102 | +describe("fixed-footnotes.refresh", function() { |
| 103 | + |
| 104 | + 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) { |
| 106 | + spyOn(utilStub, "isElementInViewport").and.returnValues(false, // reference invisible |
| 107 | + true, false); // reference visible, note invisible |
| 108 | + var ffn = footnotes({}, w); |
| 109 | + expect(w.$(".fixed-footnotes-note").length).toBe(0); |
| 110 | + ffn.refresh(); |
97 | 111 | expect(w.$(".fixed-footnotes-note").length).toBe(1);
|
98 |
| - expect(w.$(".fixed-footnotes-note").hasClass("anotherClass")).toBe(true); |
99 |
| - expect(w.$(".fixed-footnotes-note").text()).toBe("NOTE"); |
100 | 112 | done();
|
101 | 113 | });
|
102 | 114 | });
|
103 | 115 |
|
104 | 116 | });
|
| 117 | + |
| 118 | +describe("fixed-footnotes.addRefreshListener", function() { |
| 119 | + |
| 120 | + it("should add a function executed on refresh", function(done) { |
| 121 | + jsdom.env("<body></body>", ["http://code.jquery.com/jquery.js"], function(err, w) { |
| 122 | + var ffn = footnotes({}, w); |
| 123 | + ffn.addRefreshListener(done); |
| 124 | + ffn.refresh(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | +}); |
| 129 | + |
| 130 | +describe("fixed-footnotes.removeRefreshListener", function() { |
| 131 | + |
| 132 | + 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 }; |
| 135 | + spyOn(someObj, "someFunc"); |
| 136 | + var ffn = footnotes({}, w); |
| 137 | + ffn.addRefreshListener(someObj.someFunc); |
| 138 | + ffn.removeRefreshListener(someObj.someFunc); |
| 139 | + ffn.refresh(); |
| 140 | + setTimeout(function() { |
| 141 | + expect(someObj.someFunc).not.toHaveBeenCalled(); |
| 142 | + done(); |
| 143 | + }, 20); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | +}); |
0 commit comments