2017-07-24 13:36:52 -04:00
|
|
|
import LazyLoader from '~/lazy_loader';
|
2018-09-06 10:36:05 -04:00
|
|
|
import { TEST_HOST } from './test_constants';
|
2018-11-26 03:57:21 -05:00
|
|
|
import scrollIntoViewPromise from './helpers/scroll_into_view_promise';
|
|
|
|
import waitForPromises from './helpers/wait_for_promises';
|
|
|
|
import waitForAttributeChange from './helpers/wait_for_attribute_change';
|
2017-07-24 13:36:52 -04:00
|
|
|
|
2018-09-06 10:36:05 -04:00
|
|
|
const execImmediately = callback => {
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
2018-09-03 03:44:46 -04:00
|
|
|
describe('LazyLoader', function() {
|
2018-11-26 03:57:21 -05:00
|
|
|
let lazyLoader = null;
|
|
|
|
|
2019-03-26 12:03:28 -04:00
|
|
|
preloadFixtures('issues/issue_with_comment.html');
|
2017-07-24 13:36:52 -04:00
|
|
|
|
2018-11-26 03:57:21 -05:00
|
|
|
describe('without IntersectionObserver', () => {
|
2018-09-06 10:36:05 -04:00
|
|
|
beforeEach(function() {
|
2019-03-26 12:03:28 -04:00
|
|
|
loadFixtures('issues/issue_with_comment.html');
|
2018-09-06 10:36:05 -04:00
|
|
|
|
|
|
|
lazyLoader = new LazyLoader({
|
|
|
|
observerNode: 'foobar',
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(LazyLoader, 'supportsIntersectionObserver').and.callFake(() => false);
|
|
|
|
|
|
|
|
spyOn(LazyLoader, 'loadImage').and.callThrough();
|
|
|
|
|
|
|
|
spyOn(window, 'requestAnimationFrame').and.callFake(execImmediately);
|
|
|
|
spyOn(window, 'requestIdleCallback').and.callFake(execImmediately);
|
|
|
|
|
|
|
|
// Doing everything that happens normally in onload
|
|
|
|
lazyLoader.register();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
lazyLoader.unregister();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should copy value from data-src to src for img 1', function(done) {
|
|
|
|
const img = document.querySelectorAll('img[data-src]')[0];
|
|
|
|
const originalDataSrc = img.getAttribute('data-src');
|
2018-11-26 03:57:21 -05:00
|
|
|
|
|
|
|
Promise.all([scrollIntoViewPromise(img), waitForAttributeChange(img, ['data-src', 'src'])])
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).toHaveBeenCalled();
|
|
|
|
expect(img.getAttribute('src')).toBe(originalDataSrc);
|
|
|
|
expect(img).toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2018-09-06 10:36:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should lazy load dynamically added data-src images', function(done) {
|
|
|
|
const newImg = document.createElement('img');
|
|
|
|
const testPath = `${TEST_HOST}/img/testimg.png`;
|
|
|
|
newImg.className = 'lazy';
|
|
|
|
newImg.setAttribute('data-src', testPath);
|
|
|
|
document.body.appendChild(newImg);
|
2018-11-26 03:57:21 -05:00
|
|
|
|
|
|
|
Promise.all([
|
|
|
|
scrollIntoViewPromise(newImg),
|
|
|
|
waitForAttributeChange(newImg, ['data-src', 'src']),
|
|
|
|
])
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).toHaveBeenCalled();
|
|
|
|
expect(newImg.getAttribute('src')).toBe(testPath);
|
|
|
|
expect(newImg).toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2018-09-06 10:36:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not alter normal images', function(done) {
|
|
|
|
const newImg = document.createElement('img');
|
|
|
|
const testPath = `${TEST_HOST}/img/testimg.png`;
|
|
|
|
newImg.setAttribute('src', testPath);
|
|
|
|
document.body.appendChild(newImg);
|
|
|
|
|
2018-11-26 03:57:21 -05:00
|
|
|
scrollIntoViewPromise(newImg)
|
|
|
|
.then(waitForPromises)
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).not.toHaveBeenCalled();
|
|
|
|
expect(newImg).not.toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2018-09-06 10:36:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not load dynamically added pictures if content observer is turned off', done => {
|
|
|
|
lazyLoader.stopContentObserver();
|
|
|
|
|
|
|
|
const newImg = document.createElement('img');
|
|
|
|
const testPath = `${TEST_HOST}/img/testimg.png`;
|
|
|
|
newImg.className = 'lazy';
|
|
|
|
newImg.setAttribute('data-src', testPath);
|
|
|
|
document.body.appendChild(newImg);
|
|
|
|
|
2018-11-26 03:57:21 -05:00
|
|
|
scrollIntoViewPromise(newImg)
|
|
|
|
.then(waitForPromises)
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).not.toHaveBeenCalled();
|
|
|
|
expect(newImg).not.toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2018-09-06 10:36:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should load dynamically added pictures if content observer is turned off and on again', done => {
|
|
|
|
lazyLoader.stopContentObserver();
|
|
|
|
lazyLoader.startContentObserver();
|
|
|
|
|
|
|
|
const newImg = document.createElement('img');
|
|
|
|
const testPath = `${TEST_HOST}/img/testimg.png`;
|
|
|
|
newImg.className = 'lazy';
|
|
|
|
newImg.setAttribute('data-src', testPath);
|
|
|
|
document.body.appendChild(newImg);
|
|
|
|
|
2018-11-26 03:57:21 -05:00
|
|
|
Promise.all([
|
|
|
|
scrollIntoViewPromise(newImg),
|
|
|
|
waitForAttributeChange(newImg, ['data-src', 'src']),
|
|
|
|
])
|
|
|
|
.then(waitForPromises)
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).toHaveBeenCalled();
|
|
|
|
expect(newImg).toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-07-24 13:36:52 -04:00
|
|
|
});
|
|
|
|
});
|
2018-09-06 10:36:05 -04:00
|
|
|
|
2018-11-26 03:57:21 -05:00
|
|
|
describe('with IntersectionObserver', () => {
|
2018-09-06 10:36:05 -04:00
|
|
|
beforeEach(function() {
|
2019-03-26 12:03:28 -04:00
|
|
|
loadFixtures('issues/issue_with_comment.html');
|
2018-09-06 10:36:05 -04:00
|
|
|
|
|
|
|
lazyLoader = new LazyLoader({
|
|
|
|
observerNode: 'foobar',
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(LazyLoader, 'loadImage').and.callThrough();
|
|
|
|
|
|
|
|
spyOn(window, 'requestAnimationFrame').and.callFake(execImmediately);
|
|
|
|
spyOn(window, 'requestIdleCallback').and.callFake(execImmediately);
|
|
|
|
|
|
|
|
// Doing everything that happens normally in onload
|
|
|
|
lazyLoader.register();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
lazyLoader.unregister();
|
|
|
|
});
|
|
|
|
|
2018-09-03 03:44:46 -04:00
|
|
|
it('should copy value from data-src to src for img 1', function(done) {
|
2017-07-24 13:36:52 -04:00
|
|
|
const img = document.querySelectorAll('img[data-src]')[0];
|
|
|
|
const originalDataSrc = img.getAttribute('data-src');
|
2018-11-26 03:57:21 -05:00
|
|
|
|
|
|
|
Promise.all([scrollIntoViewPromise(img), waitForAttributeChange(img, ['data-src', 'src'])])
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).toHaveBeenCalled();
|
|
|
|
expect(img.getAttribute('src')).toBe(originalDataSrc);
|
|
|
|
expect(img).toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-07-24 13:36:52 -04:00
|
|
|
});
|
|
|
|
|
2018-09-03 03:44:46 -04:00
|
|
|
it('should lazy load dynamically added data-src images', function(done) {
|
2017-07-24 13:36:52 -04:00
|
|
|
const newImg = document.createElement('img');
|
2018-09-06 10:36:05 -04:00
|
|
|
const testPath = `${TEST_HOST}/img/testimg.png`;
|
2017-07-24 13:36:52 -04:00
|
|
|
newImg.className = 'lazy';
|
|
|
|
newImg.setAttribute('data-src', testPath);
|
|
|
|
document.body.appendChild(newImg);
|
2018-11-26 03:57:21 -05:00
|
|
|
|
|
|
|
Promise.all([
|
|
|
|
scrollIntoViewPromise(newImg),
|
|
|
|
waitForAttributeChange(newImg, ['data-src', 'src']),
|
|
|
|
])
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).toHaveBeenCalled();
|
|
|
|
expect(newImg.getAttribute('src')).toBe(testPath);
|
|
|
|
expect(newImg).toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-07-24 13:36:52 -04:00
|
|
|
});
|
|
|
|
|
2018-09-03 03:44:46 -04:00
|
|
|
it('should not alter normal images', function(done) {
|
2017-07-24 13:36:52 -04:00
|
|
|
const newImg = document.createElement('img');
|
2018-09-06 10:36:05 -04:00
|
|
|
const testPath = `${TEST_HOST}/img/testimg.png`;
|
2017-07-24 13:36:52 -04:00
|
|
|
newImg.setAttribute('src', testPath);
|
|
|
|
document.body.appendChild(newImg);
|
|
|
|
|
2018-11-26 03:57:21 -05:00
|
|
|
scrollIntoViewPromise(newImg)
|
|
|
|
.then(waitForPromises)
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).not.toHaveBeenCalled();
|
|
|
|
expect(newImg).not.toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2018-09-06 10:36:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not load dynamically added pictures if content observer is turned off', done => {
|
|
|
|
lazyLoader.stopContentObserver();
|
|
|
|
|
|
|
|
const newImg = document.createElement('img');
|
|
|
|
const testPath = `${TEST_HOST}/img/testimg.png`;
|
|
|
|
newImg.className = 'lazy';
|
|
|
|
newImg.setAttribute('data-src', testPath);
|
|
|
|
document.body.appendChild(newImg);
|
|
|
|
|
2018-11-26 03:57:21 -05:00
|
|
|
scrollIntoViewPromise(newImg)
|
|
|
|
.then(waitForPromises)
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).not.toHaveBeenCalled();
|
|
|
|
expect(newImg).not.toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2018-09-06 10:36:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should load dynamically added pictures if content observer is turned off and on again', done => {
|
|
|
|
lazyLoader.stopContentObserver();
|
|
|
|
lazyLoader.startContentObserver();
|
|
|
|
|
|
|
|
const newImg = document.createElement('img');
|
|
|
|
const testPath = `${TEST_HOST}/img/testimg.png`;
|
|
|
|
newImg.className = 'lazy';
|
|
|
|
newImg.setAttribute('data-src', testPath);
|
|
|
|
document.body.appendChild(newImg);
|
|
|
|
|
2018-11-26 03:57:21 -05:00
|
|
|
Promise.all([
|
|
|
|
scrollIntoViewPromise(newImg),
|
|
|
|
waitForAttributeChange(newImg, ['data-src', 'src']),
|
|
|
|
])
|
|
|
|
.then(() => {
|
|
|
|
expect(LazyLoader.loadImage).toHaveBeenCalled();
|
|
|
|
expect(newImg).toHaveClass('js-lazy-loaded');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-07-24 13:36:52 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|