forked from zuk/jquery.inview
-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathtest.js
356 lines (263 loc) · 9.19 KB
/
test.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
QUnit.config.reorder = false;
window['jQuery 1.6'].each(['jQuery 1.4', 'jQuery 1.5', 'jQuery 1.6', 'jQuery 1.7', 'jQuery 1.8'], function(i, version) {
var jQuery = window[version],
$ = jQuery;
module('jquery.inview - ' + version, {
setup: function() {
$(window).scrollTop(0).scrollLeft(0);
this.size = 20000;
this.container = $('<div>', {
"class": 'test-container'
}).appendTo("body");
this.element = $('<div>', {
html: 'testing ...',
"class": 'test-element'
}).css({
background: '#eee',
width: '50px',
height: '50px',
position: 'absolute'
});
this.element2 = this.element.clone();
},
teardown: function() {
$(window).scrollTop(0).scrollLeft(0);
this.container.remove();
this.element.remove();
}
});
asyncTest('Check vertical scrolling', function() {
expect(5);
var element = this.element,
firstCall,
secondCall,
thirdCall,
inView;
element.css({ left: 0, top: this.size - 50 + 'px' });
element.appendTo('body');
element.bind('inview.firstCall', function() { firstCall = true; });
setTimeout(function() {
$(window).scrollTop(0).scrollLeft(0);
ok(!firstCall, 'inview shouldn\'t be triggered initially when the element isn\'t in the viewport');
element.unbind('inview.firstCall');
element.bind('inview.secondCall', function(event, inViewParam) {
secondCall = true;
inView = inViewParam;
});
$(window).scrollTop(9999999);
setTimeout(function() {
ok(secondCall, 'Triggered handler after element appeared in viewport');
ok(inView, 'Parameter, indicating whether the element is in the viewport, is set to "true"');
element.unbind('inview.secondCall');
element.bind('inview.thirdCall', function(event, inViewParam) {
thirdCall = true;
inView = inViewParam;
});
$(window).scrollTop(0).scrollLeft(0);
setTimeout(function() {
ok(thirdCall, 'Triggered handler after element disappeared in viewport');
strictEqual(inView, false, 'Parameter, indicating whether the element is in the viewport, is set to "false"');
start();
}, 1000);
}, 1000);
}, 1000);
});
asyncTest('Check horizontal scrolling', function() {
expect(5);
var element = this.element,
firstCall,
secondCall,
thirdCall,
inView;
element.css({ top: 0, left: this.size - 50 + 'px' });
element.appendTo('body');
element.bind('inview.firstCall', function() { firstCall = true; });
setTimeout(function() {
$(window).scrollTop(0).scrollLeft(0);
ok(!firstCall, 'inview shouldn\'t be triggered initially when the element isn\'t in the viewport');
element.unbind('inview.firstCall');
element.bind('inview.secondCall', function(event, inViewParam) {
secondCall = true;
inView = inViewParam;
});
$(window).scrollLeft(9999999);
setTimeout(function() {
ok(secondCall, 'Triggered handler after element appeared in viewport');
ok(inView, 'Parameter, indicating whether the element is in the viewport, is set to "true"');
element.unbind('inview.secondCall');
element.bind('inview.thirdCall', function(event, inViewParam) {
thirdCall = true;
inView = inViewParam;
});
$(window).scrollTop(0).scrollLeft(0);
setTimeout(function() {
ok(thirdCall, 'Triggered handler after element disappeared in viewport');
strictEqual(inView, false, 'Parameter, indicating whether the element is in the viewport, is set to "false"');
start();
}, 1000);
}, 1000);
}, 1000);
});
asyncTest('Move element into viewport without scrolling', function() {
expect(3);
var element = this.element, calls = 0;
element
.css({ left: '-500px', top: 0 })
.appendTo('body')
.bind('inview', function(event) { calls++; });
setTimeout(function() {
equal(calls, 0, 'Callback hasn\'t been fired since the element isn\'t in the viewport');
element.css({ left: 0 });
setTimeout(function() {
equal(calls, 1, 'Callback has been fired after the element appeared in the viewport');
element.css({ left: '10000px' });
setTimeout(function() {
equal(calls, 2, 'Callback has been fired after the element disappeared from viewport');
start();
}, 1000);
}, 1000);
}, 1000);
});
asyncTest('Check whether element which isn\'t in the dom tree triggers the callback', function() {
expect(0);
this.element.bind('inview', function(event, isInView) {
ok(false, 'Callback shouldn\'t be fired since the element isn\'t even in the dom tree');
start();
});
setTimeout(function() { start(); }, 1000);
});
asyncTest('Check whether element which is on the top outside of viewport is not firing the event', function() {
expect(0);
this.element.bind('inview', function(event, isInView) {
ok(false, 'Callback shouldn\'t be fired since the element is outside of viewport');
start();
});
this.element.css({
top: '-50px',
left: '50px'
}).appendTo('body');
setTimeout(function() { start(); }, 1000);
});
asyncTest('Check whether element which is on the left outside of viewport is not firing the event', function() {
expect(0);
this.element.bind('inview', function(event, isInView) {
ok(false, 'Callback shouldn\'t be fired since the element is outside of viewport');
start();
});
this.element.css({
top: '50px',
left: '-50px'
}).appendTo('body');
setTimeout(function() { start(); }, 1000);
});
asyncTest('Check visiblePartX & visiblePartY parameters #1', function() {
expect(2);
this.element.css({
top: '-25px',
left: '-25px'
}).appendTo('body');
this.element.bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
equal(visiblePartX, 'right', 'visiblePartX has correct value');
equal(visiblePartY, 'bottom', 'visiblePartY has correct value');
start();
});
});
asyncTest('Check visiblePartX & visiblePartY parameters #2', function() {
expect(2);
this.element.css({
top: '0',
left: '-25px'
}).appendTo('body');
this.element.bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
equal(visiblePartX, 'right', 'visiblePartX has correct value');
equal(visiblePartY, 'both', 'visiblePartY has correct value');
start();
});
});
asyncTest('Check visiblePartX & visiblePartY parameters #3', function() {
expect(2);
this.element.css({
top: '0',
left: '0'
}).appendTo('body');
this.element.bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
equal(visiblePartX, 'both', 'visiblePartX has correct value');
equal(visiblePartY, 'both', 'visiblePartY has correct value');
start();
});
});
asyncTest('Check "live" events', function() {
expect(3);
var that = this,
elems = $("body .test-container > div.test-element");
elems.live("inview", function(event) {
elems.die("inview");
ok(true, "Live event correctly fired");
equal(event.currentTarget, that.element[0], "event.currentTarget correctly set");
equal(this, that.element[0], "Handler bound to target element");
start();
});
this.element.css({
top: '0',
left: '0'
}).appendTo(this.container);
});
asyncTest('Check "delegate" events', function() {
expect(3);
var that = this;
this.container.delegate(".test-element", "inview", function(event) {
ok(true, "Delegated event correctly fired");
equal(event.currentTarget, that.element[0], "event.currentTarget correctly set");
equal(this, that.element[0], "Handler bound to target element");
start();
});
this.element.css({
top: '0',
left: '0'
}).appendTo(this.container);
});
asyncTest('Check namespaced "delegate" events', function() {
expect(1);
this.container.delegate(".test-element", "inview.foo", function(event) {
ok(true, "Delegated event correctly fired");
start();
});
this.element.css({
top: '0',
left: '0'
}).appendTo(this.container);
});
asyncTest('Check multiple elements', function() {
expect(2);
var i = 0;
this.element.add(this.element2).css({
top: '0',
left: '0'
}).appendTo(this.container);
$('.test-element').bind('inview', function() {
ok(true);
if (++i == 2) {
start();
}
});
});
if (!("ontouchstart" in window)) {
asyncTest('Scroll to element via focus()', function() {
// This test will fail on iOS
expect(1);
var $input = $("<input>").css({
position: "absolute",
top: "7000px",
left: "5000px"
}).appendTo(this.container);
$input.bind('inview', function() {
ok(true);
$input.remove();
start();
});
setTimeout(function() {
$input.focus();
}, 1000);
});
}
});