forked from vvo/in-viewport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
not-visible.js
76 lines (61 loc) · 1.83 KB
/
not-visible.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function testElem(x, y, offsetTest) {
offsetTest = offsetTest || 0;
describe('dealing with an element located at '+x+','+y, function() {
require('./fixtures/bootstrap.js');
beforeEach(h.clean);
afterEach(h.clean);
var test;
var calls;
beforeEach(function() {
calls = [];
test = h.createTest({
style: {
left: x + 'px',
top: y + 'px'
}
});
h.insertTest(test);
inViewport(test, cb);
});
describe('when we scroll down a little', function() {
beforeEach(h.scroller(0, 1000));
it('cb not called', function() {
assert.strictEqual(calls.length, 0);
});
});
describe('when we scroll down too far (debounce)', function() {
beforeEach(h.scroller(x * 2, y * 2));
it('cb not called', function() {
assert.strictEqual(calls.length, 0);
});
});
describe('when we scroll near the element (offset)', function() {
beforeEach(h.scroller(x - offsetTest, y - offsetTest));
beforeEach(h.scroller(x - offsetTest + 1, y - offsetTest + 1));
beforeEach(h.wait(50));
it('cb called', function() {
assert.strictEqual(calls.length, 1);
});
});
describe('when we scroll down, up, like crazy (debounce)', function() {
beforeEach(h.scroller(0, 200));
beforeEach(h.scroller(0, 20000));
beforeEach(h.scroller(0, 0));
it('doesnt calls the cb', function() {
assert.strictEqual(calls.length, 0);
});
describe('when we are at the element', function() {
beforeEach(h.scroller(x, y));
it('calls the cb', function() {
assert.strictEqual(calls.length, 1);
});
})
});
function cb(result) {
calls.push(result);
}
});
}
testElem(0, 3000);
testElem(0, 3000, 100);
testElem(3000, 3000);