This repository was archived by the owner on Jan 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathcore.js
More file actions
347 lines (282 loc) · 12.9 KB
/
core.js
File metadata and controls
347 lines (282 loc) · 12.9 KB
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
'use strict';
/* globals describe, beforeEach, afterEach, it, expect, Imager, jQuery, document, sinon */
describe('Imager.js', function () {
var fixtures, sandbox;
beforeEach(function () {
fixtures = undefined;
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
cleanFixtures(fixtures);
});
describe('constructor', function () {
it('should initialise without arguments', function (done) {
fixtures = loadFixtures('regular');
var imgr = new Imager();
imgr.ready(function () {
expect(imgr.initialized).to.equal(true);
expect(imgr.scrolled).to.equal(false);
expect(imgr.divs).to.have.length(5);
expect(imgr.selector).to.equal('.delayed-image-load');
done();
});
});
it('should initialise with one argument, the options', function () {
fixtures = loadFixtures('regular');
var imgr = new Imager({selector: '#main .delayed-image-load'});
expect(imgr.divs).to.have.length(3);
expect(imgr.selector).to.equal('#main .delayed-image-load');
});
it('should target elements with a string as first argument', function (done) {
fixtures = loadFixtures('regular');
var imgr = new Imager('#main .delayed-image-load');
imgr.ready(function () {
expect(imgr.initialized).to.equal(true);
expect(imgr.scrolled).to.equal(false);
expect(imgr.divs).to.have.length(3);
expect(imgr.selector).to.equal('#main .delayed-image-load');
done();
});
});
it('should target elements contained in a static NodeList collection', function (done) {
fixtures = loadFixtures('regular');
var imgr = new Imager(document.querySelectorAll('#main .delayed-image-load'));
imgr.ready(function () {
expect(imgr.initialized).to.equal(true);
expect(imgr.scrolled).to.equal(false);
expect(imgr.divs).to.have.length(3);
expect(imgr.selector).to.equal(null);
done();
});
});
it('should target elements contained in a live NodeList collection', function (done) {
fixtures = loadFixtures('regular');
var imgr = new Imager(document.getElementById('main').getElementsByTagName('div'));
imgr.ready(function () {
expect(imgr.initialized).to.equal(true);
expect(imgr.scrolled).to.equal(false);
expect(imgr.divs).to.have.length(3);
expect(imgr.selector).to.equal(null);
done();
});
});
it('should target elements contained in a third-party library collection', function (done) {
fixtures = loadFixtures('regular');
var imgr = new Imager(jQuery('#main .delayed-image-load'));
imgr.ready(function () {
expect(imgr.initialized).to.equal(true);
expect(imgr.scrolled).to.equal(false);
expect(imgr.divs).to.have.length(3);
expect(imgr.selector).to.equal(null);
done();
});
});
});
describe('add', function () {
it('should add additional images based on a custom selector', function (done) {
fixtures = loadFixtures('add');
var imgr = new Imager();
imgr.ready(function () {
expect(imgr.divs).to.have.length(2);
imgr.add('.triggered-image-load');
expect(imgr.divs).to.have.length(4);
done();
});
});
it('should add additional images based on the default selector', function (done) {
fixtures = loadFixtures('add');
var imgr = new Imager();
imgr.ready(function () {
var elements = document.querySelectorAll('#test-case div');
Imager.applyEach(elements, function (element) {
element.className = 'delayed-image-load';
});
imgr.add();
expect(imgr.divs).to.have.length(4);
done();
});
});
it('should add additional images based on NodeList passed in', function (done) {
fixtures = loadFixtures('add');
var imgr = new Imager();
imgr.ready(function () {
var elements = document.querySelectorAll('#test-case div');
imgr.add(elements);
expect(imgr.divs).to.have.length(4);
done();
});
});
it('should add additional images based on Node array passed in', function (done) {
fixtures = loadFixtures('add');
var imgr = new Imager();
imgr.ready(function () {
var elements = document.querySelectorAll('#test-case div');
elements = Imager.applyEach(elements, function (element) { return element; });
imgr.add(elements);
expect(imgr.divs).to.have.length(4);
done();
});
});
it('should handle "null" selector scenario gracefully', function (done) {
fixtures = loadFixtures('add');
var imgr = new Imager([]);
imgr.ready(function () {
imgr.add();
expect(imgr.divs).to.have.length(0);
done();
});
});
});
describe('determineAppropriateResolution', function () {
var imgr, windowWidth, availableWidths = [320, 640, 1024];
beforeEach(function () {
fixtures = loadFixtures('widths');
imgr = new Imager({availableWidths: availableWidths});
windowWidth = window.innerWidth;
});
it('should pick the closest value from the container\'s width (no container size)', function () {
var maxContainerWidth = Imager.getClosestValue(windowWidth, availableWidths);
expect(imgr.determineAppropriateResolution(imgr.divs[0])).to.equal(maxContainerWidth);
});
it('should pick the data-width and not the container\'s size (no container size)', function () {
expect(imgr.determineAppropriateResolution(imgr.divs[1])).to.equal(640);
});
it('should pick the closest value from the container\'s width (container\'s size contained in availableWidths)', function () {
expect(imgr.determineAppropriateResolution(imgr.divs[2])).to.equal(640);
});
it('should pick the data-width and not the container\'s size (container\'s size contained in availableWidths)', function () {
expect(imgr.determineAppropriateResolution(imgr.divs[3])).to.equal(640);
});
it('should pick the closest value from the container\'s width (container\'s size smaller than availableWidths)', function () {
expect(imgr.determineAppropriateResolution(imgr.divs[4])).to.equal(320);
});
it('should pick the data-width and not the container\'s size (container\'s size smaller than availableWidths)', function () {
expect(imgr.determineAppropriateResolution(imgr.divs[5])).to.equal(640);
});
});
describe('devicePixelRatio', function () {
var imgrOptions = {availablePixelRatios: [1, 1.3, 2]};
it('should pick a value of 1 if the device pixel ratio is lower than 1', function () {
sandbox.stub(Imager, 'getPixelRatio', function () {
return 0.8
});
expect(new Imager(imgrOptions)).to.have.property('devicePixelRatio', 1);
});
it('should pick a value of 1.3 if the device pixel ratio is equal to 1.3', function () {
sandbox.stub(Imager, 'getPixelRatio', function () {
return 1.3
});
expect(new Imager(imgrOptions)).to.have.property('devicePixelRatio', 1.3);
});
it('should pick the biggest ratio if the device pixel ratio is greater than the biggest available one', function () {
sandbox.stub(Imager, 'getPixelRatio', function () {
return 3
});
expect(new Imager(imgrOptions)).to.have.property('devicePixelRatio', 2);
});
});
describe('getPageOffsetGenerator', function () {
it('should use `window.pageYOffset` if the property is available', function () {
var pageYOffsetIsAvailable = true;
var generator = Imager.getPageOffsetGenerator(pageYOffsetIsAvailable);
expect(generator.toString()).to.have.string('.pageYOffset');
});
it('should rather use `document.documentElement.scrollTop` if `window.pageYOffset` is not available', function () {
var pageYOffsetIsAvailable = false;
var generator = Imager.getPageOffsetGenerator(pageYOffsetIsAvailable);
expect(generator.toString()).to.have.string('.documentElement.scrollTop');
});
});
describe('cleanup', function() {
var removeEvtStub, getExpectedEvtName;
beforeEach(function() {
if(document.removeEventListener) {
removeEvtStub = sandbox.stub(window, 'removeEventListener');
getExpectedEvtName = function(name) {
return name;
};
} else {
removeEvtStub = sandbox.stub(window, 'detachEvent');
getExpectedEvtName = function(name) {
return 'on' + name;
};
}
});
it('should cleanup after using onResize option', function(done) {
fixtures = loadFixtures('cleanup');
var imgr = new Imager({
onResize: true
});
imgr.ready(function () {
expect(imgr.resizeListeners.length).to.be(1);
expect(imgr.scrollListeners.length).to.be(0);
imgr.cleanup();
expect(imgr.resizeListeners.length).to.be(0);
expect(imgr.scrollListeners.length).to.be(0);
expect(removeEvtStub.called).to.be(true);
expect(removeEvtStub.callCount).to.be(1);
expect(removeEvtStub.firstCall.args[0]).to.be(getExpectedEvtName('resize'));
done();
});
});
it('should cleanup after using lazyload option', function(done) {
sandbox.stub(window, 'clearInterval');
fixtures = loadFixtures('cleanup');
var imgr = new Imager({
lazyload: true
});
imgr.ready(function () {
expect(imgr.resizeListeners.length).to.be(2);
expect(imgr.scrollListeners.length).to.be(1);
expect(imgr.interval).to.be.defined;
imgr.cleanup();
expect(imgr.resizeListeners.length).to.be(0);
expect(imgr.scrollListeners.length).to.be(0);
expect(imgr.interval).to.be.null;
expect(removeEvtStub.called).to.be(true);
expect(removeEvtStub.callCount).to.be(3);
expect(removeEvtStub.firstCall.args[0]).to.be(getExpectedEvtName('resize'));
expect(removeEvtStub.secondCall.args[0]).to.be(getExpectedEvtName('resize'));
expect(removeEvtStub.thirdCall.args[0]).to.be(getExpectedEvtName('scroll'));
expect(window.clearInterval.called).to.be(true);
done();
});
});
});
describe('isThisElementOnScreen', function(){
var offsetStub, imgr;
beforeEach(function(){
imgr = new Imager();
offsetStub = sandbox.stub(imgr, 'viewportHeight', 1024);
});
it('should not detect an element if itself and its parent outreach the viewport height', function(){
var element = {
offsetTop: 100,
offsetParent: {
offsetTop: 1024
}
};
expect(imgr.isThisElementOnScreen(element)).to.equal(false);
});
it('should detect an element on screen if its offsetPosition is located within the viewport', function(){
var element = {
offsetTop: 100,
offsetParent: {
offsetTop: 0
}
};
expect(imgr.isThisElementOnScreen(element)).to.equal(true);
});
it('should detect an element on screen if its offsetPosition is out of the viewport but within the viewport+lazyloadOffset', function(){
var element = {
offsetTop: 100,
offsetParent: {
offsetTop: 1024
}
};
imgr.lazyloadOffset = 300;
expect(imgr.isThisElementOnScreen(element)).to.equal(true);
});
});
});