scroll_helper_spec.js 1.99 KB
Newer Older
1 2 3 4
import $ from 'jquery';
import ScrollHelper from '~/helpers/scroll_helper';

describe('ScrollHelper', () => {
5 6 7 8
  const width = 10;

  describe('getScrollWidth', () => {
    const parent = jasmine.createSpyObj('parent', ['css', 'appendTo', 'remove']);
9
    const child = jasmine.createSpyObj('child', ['css', 'appendTo', 'get']);
10 11 12 13 14 15 16 17
    let scrollWidth;

    beforeEach(() => {
      spyOn($.fn, 'init').and.returnValues(parent, child);
      spyOn(jasmine.Fixtures.prototype, 'cleanUp'); // disable jasmine-jquery cleanup, we dont want it but its imported in test_bundle :(

      parent.css.and.returnValue(parent);
      child.css.and.returnValue(child);
18 19 20
      child.get.and.returnValue({
        offsetWidth: width,
      });
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

      scrollWidth = ScrollHelper.getScrollWidth();
    });

    it('inserts 2 nested hidden scrollable divs, calls parents outerWidth, removes parent and returns the width', () => {
      const initArgs = $.fn.init.calls.allArgs();

      expect(initArgs[0][0]).toEqual('<div>');
      expect(initArgs[1][0]).toEqual('<div>');
      expect(parent.css).toHaveBeenCalledWith({
        visibility: 'hidden',
        width: 100,
        overflow: 'scroll',
      });
      expect(child.css).toHaveBeenCalledWith({
        width: 100,
      });
      expect(child.appendTo).toHaveBeenCalledWith(parent);
      expect(parent.appendTo).toHaveBeenCalledWith('body');
40
      expect(child.get).toHaveBeenCalledWith(0);
41 42 43 44 45
      expect(parent.remove).toHaveBeenCalled();
      expect(scrollWidth).toEqual(100 - width);
    });
  });

46 47
  describe('setScrollWidth', () => {
    it('calls getScrollWidth and sets data-scroll-width', () => {
48
      spyOn($.fn, 'find').and.callThrough();
49 50 51 52 53
      spyOn($.fn, 'attr');
      spyOn(ScrollHelper, 'getScrollWidth').and.returnValue(width);

      ScrollHelper.setScrollWidth();

54
      expect($.fn.find).toHaveBeenCalledWith('body');
55
      expect($.fn.attr).toHaveBeenCalledWith('data-scroll-width', width);
56
      expect(ScrollHelper.getScrollWidth).toHaveBeenCalled();
57 58 59
    });
  });
});