lazy_loader_spec.js 1.8 KB
Newer Older
1 2 3 4
import LazyLoader from '~/lazy_loader';

let lazyLoader = null;

T
Tim Zallmann 已提交
5
describe('LazyLoader', function() {
6 7
  preloadFixtures('issues/issue_with_comment.html.raw');

T
Tim Zallmann 已提交
8
  beforeEach(function() {
9 10 11 12 13 14 15
    loadFixtures('issues/issue_with_comment.html.raw');
    lazyLoader = new LazyLoader({
      observerNode: 'body',
    });
    // Doing everything that happens normally in onload
    lazyLoader.loadCheck();
  });
T
Tim Zallmann 已提交
16 17
  describe('behavior', function() {
    it('should copy value from data-src to src for img 1', function(done) {
18 19 20 21 22 23 24 25 26 27 28
      const img = document.querySelectorAll('img[data-src]')[0];
      const originalDataSrc = img.getAttribute('data-src');
      img.scrollIntoView();

      setTimeout(() => {
        expect(img.getAttribute('src')).toBe(originalDataSrc);
        expect(document.getElementsByClassName('js-lazy-loaded').length).toBeGreaterThan(0);
        done();
      }, 100);
    });

T
Tim Zallmann 已提交
29
    it('should lazy load dynamically added data-src images', function(done) {
30 31 32 33 34 35 36 37 38 39 40 41 42 43
      const newImg = document.createElement('img');
      const testPath = '/img/testimg.png';
      newImg.className = 'lazy';
      newImg.setAttribute('data-src', testPath);
      document.body.appendChild(newImg);
      newImg.scrollIntoView();

      setTimeout(() => {
        expect(newImg.getAttribute('src')).toBe(testPath);
        expect(document.getElementsByClassName('js-lazy-loaded').length).toBeGreaterThan(0);
        done();
      }, 100);
    });

T
Tim Zallmann 已提交
44
    it('should not alter normal images', function(done) {
45 46 47 48 49 50 51 52 53 54 55 56 57
      const newImg = document.createElement('img');
      const testPath = '/img/testimg.png';
      newImg.setAttribute('src', testPath);
      document.body.appendChild(newImg);
      newImg.scrollIntoView();

      setTimeout(() => {
        expect(newImg).not.toHaveClass('js-lazy-loaded');
        done();
      }, 100);
    });
  });
});