Skip to content

Commit 571cf7b

Browse files
committed
Fix tests
Tests had one big mistake - setTimeout executed after test case passed. In other words - no checks are performed inside `setTimeout` calls. The other thing is that we don't check if scrollTop is changed, but I don't know how to test it correctly
1 parent 761319c commit 571cf7b

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

test/spec/spec-smoothScroll.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,58 @@ describe('Smooth Scroll', function () {
33
//
44
// Helper Methods
55
//
6-
6+
var removeElements = function() {
7+
var p = document.querySelector('p');
8+
if (p && p.remove) {
9+
p.remove();
10+
}
11+
var a = document.querySelector('a');
12+
if (a && a.remove) {
13+
a.remove();
14+
}
15+
var target = document.querySelector('div');
16+
if (target && target.remove) {
17+
target.remove();
18+
}
19+
};
720
/**
8-
* Create a link element, add it to the body.
9-
* @public
10-
* @param {String} the href attribute of the new link
11-
* @param {Boolean} whether to add data-scroll to the link or not
12-
* @returns {Element}
13-
*/
21+
* Create a link element, add it to the body.
22+
* @public
23+
* @param {String} the href attribute of the new link
24+
* @param {Boolean} whether to add data-scroll to the link or not
25+
* @returns {Element}
26+
*/
1427
var injectElem = function (href, smooth) {
28+
removeElements();
29+
1530
var elt = document.createElement('a');
1631
elt.href = href;
32+
elt.style.background = '#f0f';
33+
elt.textContent="newtext";
1734
if (smooth) {
1835
elt.setAttribute('data-scroll', true);
1936
}
2037
document.body.appendChild(elt);
38+
39+
var p = document.createElement('p');
40+
p.style.height = '20000px';
41+
p.style.background = '#f00';
42+
document.body.appendChild(p);
43+
44+
var target = document.createElement('div');
45+
target.id = 'target';
46+
target.style.height = '200px';
47+
target.style.background = '#0ff';
48+
document.body.appendChild(target);
49+
window.top.callPhantom({type: 'render', fname: './myscreen.png'});
2150
return elt;
2251
};
2352

2453
/**
25-
* Simulate a click event.
26-
* @public
27-
* @param {Element} the element to simulate a click on
28-
*/
54+
* Simulate a click event.
55+
* @public
56+
* @param {Element} the element to simulate a click on
57+
*/
2958
var simulateClick = function (elt) {
3059
var click = document.createEvent('MouseEvents');
3160
click.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
@@ -70,18 +99,19 @@ describe('Smooth Scroll', function () {
7099

71100
describe('Should merge user options into defaults', function () {
72101

73-
var elt = injectElem('#anchor', true);
102+
var elt = injectElem('#target', true);
74103

75104
beforeEach(function () {
76105
smoothScroll.init({
77106
callback: function () { document.documentElement.classList.add('callback'); }
78107
});
79108
});
80109

81-
it('User options should be merged into defaults', function () {
110+
it('User options should be merged into defaults', function (done) {
82111
simulateClick(elt);
83112
setTimeout(function() {
84113
expect(document.documentElement.classList.contains('callback')).toBe(true);
114+
done();
85115
}, 200);
86116
});
87117

@@ -93,8 +123,8 @@ describe('Smooth Scroll', function () {
93123
//
94124

95125
describe('Should animate scroll when anchor clicked', function () {
96-
var elt = injectElem('#anchor', true);
97-
document.body.id = 'anchor';
126+
var elt = injectElem('#target', true);
127+
// document.body.id = 'anchor';
98128

99129
beforeEach(function() {
100130
spyOn(smoothScroll, 'animateScroll');
@@ -104,11 +134,13 @@ describe('Smooth Scroll', function () {
104134
smoothScroll.destroy();
105135
});
106136

107-
it('Should trigger smooth scrolling on click', function () {
137+
138+
it('Should trigger smooth scrolling on click', function (done) {
108139
smoothScroll.init();
109140
simulateClick(elt);
110141
setTimeout(function() {
111-
expect(smoothScroll.animateScroll).toHaveBeenCalledWith(elt, '#anchor', jasmine.objectContaining(settingsStub));
142+
expect(smoothScroll.animateScroll).toHaveBeenCalledWith('#target', elt, jasmine.objectContaining(settingsStub));
143+
done();
112144
}, 200);
113145
});
114146

@@ -117,13 +149,14 @@ describe('Smooth Scroll', function () {
117149
expect(smoothScroll.animateScroll).not.toHaveBeenCalled();
118150
});
119151

120-
it('Should do nothing if destroyed', function () {
152+
it('Should do nothing if destroyed', function (done) {
121153
smoothScroll.init();
122154
smoothScroll.destroy();
123155
simulateClick(elt);
124156
setTimeout(function() {
125157
expect(smoothScroll.animateScroll).not.toHaveBeenCalled();
158+
done();
126159
}, 200);
127160
});
128161
});
129-
});
162+
});

0 commit comments

Comments
 (0)