-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathangular-inview.spec.js
428 lines (398 loc) · 11.2 KB
/
angular-inview.spec.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
'use strict';
describe("angular-inview", function() {
var $rootScope, $compile, $q;
beforeEach(function () {
module('angular-inview');
inject(function (_$rootScope_, _$compile_, _$q_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
$q = _$q_;
});
});
describe("in-view directive", function() {
it("should trigger in-view expression with `$inview` local", function(done) {
makeTestForHtml(
'<div in-view="spy($inview)"></div>'
)
.then(function (test) {
expect(test.spy.calls.count()).toBe(1);
expect(test.spy).toHaveBeenCalledWith(true);
})
.then(done);
});
it("should not trigger in-view expression if out of viewport", function(done) {
makeTestForHtml(
'<div in-view="spy($inview)" style="margin-top:-100px"></div>'
)
.then(function (test) {
expect(test.spy.calls.count()).toBe(0);
})
.then(done);
});
it("should change inview status when scrolling out of view", function(done) {
makeTestForHtml(
'<div in-view="spy($inview)"></div>' +
'<div style="height:200%"></div>'
)
.then(lazyScrollTo(100))
.then(function (test) {
expect(test.spy.calls.count()).toBe(2);
expect(test.spy).toHaveBeenCalledWith(true);
expect(test.spy).toHaveBeenCalledWith(false);
})
.then(done);
});
describe("informations object", function() {
it("should return an info object with additional informations", function(done) {
makeTestForHtml(
'<div in-view="spy($inviewInfo)"></div>'
)
.then(function (test) {
expect(test.spy.calls.count()).toBe(1);
var info = test.spy.calls.mostRecent().args[0];
expect(info.inView).toEqual(true);
expect(info.changed).toEqual(true);
expect(info.elementRect).toBeDefined();
expect(info.viewportRect).toBeDefined();
expect(info.direction).not.toBeDefined();
expect(info.parts).not.toBeDefined();
})
.then(done);
});
it("should return proper `parts` informations", function(done) {
makeTestForHtml(
'<div in-view="spy($inviewInfo)" in-view-options="{ generateParts: true }" style="width: 200px; height: 200px;"></div>' +
'<div style="width:200%; height:200%"></div>'
)
.then(function (test) {
expect(test.spy.calls.count()).toBe(1);
var info = test.spy.calls.argsFor(0)[0];
expect(info.parts).toEqual({
top: true,
left: true,
bottom: true,
right: true
});
return test;
})
.then(lazyScrollTo([400, 400]))
.then(function (test) {
var info = test.spy.calls.argsFor(1)[0];
expect(test.spy.calls.count()).toBe(2);
expect(info.parts).toEqual(undefined);
return test;
})
.then(lazyScrollTo([100, 100]))
.then(function (test) {
var info = test.spy.calls.argsFor(2)[0];
expect(test.spy.calls.count()).toBe(3);
expect(info.parts).toEqual({
top: false,
left: false,
bottom: true,
right: true
});
return test;
})
.then(lazyScrollTo([0, 0]))
.then(function (test) {
var info = test.spy.calls.argsFor(3)[0];
expect(test.spy.calls.count()).toBe(4);
expect(info.parts).toEqual({
top: true,
left: true,
bottom: true,
right: true
});
})
.then(done);
});
it("should return proper `direction` informations", function(done) {
makeTestForHtml(
'<div in-view="spy($inviewInfo)" in-view-options="{ generateDirection: true }" style="width: 200px; height: 200px;"></div>' +
'<div style="width:200%; height:200%"></div>'
)
.then(function (test) {
var info = test.spy.calls.argsFor(0)[0];
expect(info.direction).toEqual(undefined);
return test;
})
.then(lazyScrollTo([100, 100]))
.then(function (test) {
var info = test.spy.calls.argsFor(1)[0];
expect(info.direction).toEqual({
horizontal: -100,
vertical: -100
});
return test;
})
.then(lazyScrollTo([50, 50]))
.then(function (test) {
var info = test.spy.calls.argsFor(2)[0];
expect(info.direction).toEqual({
horizontal: 50,
vertical: 50
});
})
.then(done);
});
});
describe("offset options", function() {
it("should consider element offset option", function(done) {
makeTestForHtml(
'<div in-view="spy($inviewInfo)" in-view-options="{ offset:[100, 0], generateParts: true }"></div>' +
'<div style="height:200%"></div>'
)
.then(function (test) {
var info = test.spy.calls.argsFor(0)[0];
expect(info.inView).toEqual(true);
expect(info.parts).toEqual({
top: false,
left: true,
bottom: true,
right: true
});
return test;
})
.then(done);
});
it("should consider negative offsets", function(done) {
makeTestForHtml(
'<div in-view="spy($inviewInfo)" style="height:200px" in-view-options="{ offset:[-50, 0], generateParts: true }"></div>' +
'<div style="height:200%"></div>'
)
.then(function (test) {
var info = test.spy.calls.argsFor(0)[0];
expect(info.parts).toEqual({
top: true,
left: true,
bottom: true,
right: true
});
return test;
})
.then(lazyScrollTo(100))
.then(function (test) {
var info = test.spy.calls.argsFor(1)[0];
expect(info.parts).toEqual({
top: false,
left: true,
bottom: true,
right: true
});
return test;
})
.then(lazyScrollTo(50))
.then(function (test) {
var info = test.spy.calls.argsFor(2)[0];
expect(info.parts).toEqual({
top: true,
left: true,
bottom: true,
right: true
});
return test;
})
.then(done);
});
it("should consider percent offset option", function(done) {
makeTestForHtml(
'<div in-view="spy($inviewInfo)" style="height:200px" in-view-options="{ offset:[\'25%\', 0], generateParts: true }"></div>' +
'<div style="height:200%"></div>'
)
.then(function (test) {
var info = test.spy.calls.argsFor(0)[0];
expect(info.inView).toEqual(true);
expect(info.parts).toEqual({
top: false,
left: true,
bottom: true,
right: true
});
return test;
})
.then(done);
});
it("should consider viewport offset options", function(done) {
makeTestForHtml(
'<div in-view="spy($inviewInfo)" style="height:200px" in-view-options="{ viewportOffset:[100, 0], generateParts: true }"></div>' +
'<div style="height:200%"></div>'
)
.then(function (test) {
var info = test.spy.calls.argsFor(0)[0];
expect(info.parts).toEqual({
top: true,
left: true,
bottom: true,
right: true
});
return test;
})
.then(lazyScrollTo(200))
.then(function (test) {
var info = test.spy.calls.argsFor(1)[0];
expect(info.parts).toEqual({
top: false,
left: true,
bottom: true,
right: true
});
return test;
})
.then(done);
});
});
it("should accept a `throttle` option", function(done) {
makeTestForHtml(
'<div in-view="spy($inview)" style="height:100px" in-view-options="{ throttle: 200 }"></div>' +
'<div style="height:200%"></div>'
)
.then(function (test) {
expect(test.spy.calls.count()).toBe(1);
expect(test.spy.calls.mostRecent().args[0]).toBe(true);
return test;
})
.then(lazyScrollTo(200))
.then(lazyWait(100))
.then(function (test) {
expect(test.spy.calls.count()).toBe(1);
return test;
})
.then(lazyScrollTo(0))
.then(lazyWait(100))
.then(function (test) {
expect(test.spy.calls.count()).toBe(1);
return test;
})
.then(lazyScrollTo(200))
.then(lazyWait(100))
.then(function (test) {
expect(test.spy.calls.count()).toBe(2);
expect(test.spy.calls.mostRecent().args[0]).toBe(false);
return test;
})
.then(lazyScrollTo(0))
.then(done);
});
});
describe("in-view-container directive", function() {
it("should trigger in-view when scrolling a container", function(done) {
makeTestForHtml(
'<div in-view-container style="height:30%; overflow:scroll">' +
' <div in-view="spy($inview)"></div>' +
' <div style="height:200%"></div>' +
'</div>'
)
.then(function (test) {
expect(test.spy.calls.count()).toBe(1);
expect(test.spy).toHaveBeenCalledWith(true);
return test;
})
.then(lazyScrollTestElementTo(100))
.then(function (test) {
expect(test.spy.calls.count()).toBe(2);
expect(test.spy).toHaveBeenCalledWith(false);
})
.then(done);
});
});
// A test object has the properties:
//
// - `element`: An angular element inserted in the test page
// - `scope`: a new isolated scope that can be referenced in the element
// - `spy`: a conveninence jasmine spy attached to the scope as `spy`
function makeTestForHtml(html) {
var test = {};
// Prepare test elements
window.document.body.style.height = '100%';
window.document.body.parentElement.style.height = '100%';
test.element = angular.element(html);
angular.element(window.document.body).empty().append(test.element);
// Prepare test scope
test.scope = $rootScope.$new(true);
test.spy = test.scope.spy = jasmine.createSpy('spy');
// Compile the element
$compile(test.element)(test.scope);
test.scope.$digest();
return scrollTo(window, [0, 0], true).then(function () {
return test;
});
}
// Scrolls the element to the given x, y position and waits a bit before
// resolving the returned promise.
function scrollTo(element, position, useTimeout) {
if (!angular.isDefined(position)) {
position = element;
element = window;
}
if (!angular.isArray(position)) {
position = [0, position];
}
// Prepare promise resolution
var deferred = $q.defer(), timeout;
var scrollOnceHandler = function () {
var check = (element === window) ?
[element.scrollX, element.scrollY] :
[element.scrollLeft, element.scrollTop];
if (check[0] != position[0] || check[1] != position[1]) {
return;
}
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
angular.element(element).off('scroll', scrollOnceHandler);
deferred.resolve();
$rootScope.$digest();
};
angular.element(element).on('scroll', scrollOnceHandler);
// Actual scrolling
if (element === window) {
element.scrollTo.apply(element, position);
}
else {
element.scrollLeft = position[0];
element.scrollTop = position[1];
}
// Backup resolver
if (useTimeout) timeout = setTimeout(function () {
angular.element(element).off('scroll', scrollOnceHandler);
var check = (element === window) ?
[element.scrollX, element.scrollY] :
[element.scrollLeft, element.scrollTop];
if (check[0] != position[0] || check[1] != position[1]) {
deferred.reject();
}
else {
deferred.resolve();
}
$rootScope.$digest();
}, 100);
return deferred.promise;
}
function lazyScrollTo () {
var args = arguments;
return function (x) {
return scrollTo.apply(null, args).then(function () {
return x;
});
}
}
function lazyScrollTestElementTo (pos) {
return function (test) {
return scrollTo(test.element[0], pos, true).then(function () {
return test;
});
}
}
function lazyWait (millisec) {
return function (x) {
return $q(function (resolve) {
setTimeout(function () {
resolve(x);
$rootScope.$digest();
}, millisec);
});
}
}
});