fly_out_nav_spec.js 3.1 KB
Newer Older
P
Phil Hughes 已提交
1 2 3 4 5
import {
  calculateTop,
  hideSubLevelItems,
  showSubLevelItems,
} from '~/fly_out_nav';
6 7

describe('Fly out sidebar navigation', () => {
P
Phil Hughes 已提交
8 9 10 11 12 13 14 15 16 17
  let el;
  beforeEach(() => {
    el = document.createElement('div');
    document.body.appendChild(el);
  });

  afterEach(() => {
    el.remove();
  });

18 19 20 21
  describe('calculateTop', () => {
    it('returns boundingRect top', () => {
      const boundingRect = {
        top: 100,
22
        height: 100,
23 24 25 26 27 28 29 30 31
      };

      expect(
        calculateTop(boundingRect, 100),
      ).toBe(100);
    });

    it('returns boundingRect - bottomOverflow', () => {
      const boundingRect = {
32 33
        top: window.innerHeight - 50,
        height: 100,
34 35 36 37
      };

      expect(
        calculateTop(boundingRect, 100),
38
      ).toBe(window.innerHeight - 50);
39 40
    });
  });
P
Phil Hughes 已提交
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

  describe('hideSubLevelItems', () => {
    beforeEach(() => {
      el.innerHTML = '<div class="sidebar-sub-level-items"></div>';
    });

    it('hides subitems', () => {
      hideSubLevelItems(el);

      expect(
        el.querySelector('.sidebar-sub-level-items').style.display,
      ).toBe('none');
    });

    it('removes is-over class', () => {
      spyOn(el.classList, 'remove');

      hideSubLevelItems(el);

      expect(
        el.classList.remove,
      ).toHaveBeenCalledWith('is-over');
    });

    it('removes is-above class from sub-items', () => {
      const subItems = el.querySelector('.sidebar-sub-level-items');

      spyOn(subItems.classList, 'remove');

      hideSubLevelItems(el);

      expect(
        subItems.classList.remove,
      ).toHaveBeenCalledWith('is-above');
    });

    it('does nothing if el has no sub-items', () => {
      el.innerHTML = '';

      spyOn(el.classList, 'remove');

      hideSubLevelItems(el);

      expect(
        el.classList.remove,
      ).not.toHaveBeenCalledWith();
    });
  });

  describe('showSubLevelItems', () => {
    beforeEach(() => {
      el.innerHTML = '<div class="sidebar-sub-level-items"></div>';
    });

    it('adds is-over class to el', () => {
      spyOn(el.classList, 'add');

      showSubLevelItems(el);

      expect(
        el.classList.add,
      ).toHaveBeenCalledWith('is-over');
    });

    it('shows sub-items', () => {
      showSubLevelItems(el);

      expect(
        el.querySelector('.sidebar-sub-level-items').style.display,
      ).toBe('block');
    });

    it('sets transform of sub-items', () => {
      showSubLevelItems(el);

      expect(
        el.querySelector('.sidebar-sub-level-items').style.transform,
      ).toBe(`translate3d(0px, ${el.offsetTop}px, 0px)`);
    });

    it('sets is-above when element is above', () => {
      const subItems = el.querySelector('.sidebar-sub-level-items');
P
Phil Hughes 已提交
123 124
      subItems.style.height = `${window.innerHeight + el.offsetHeight}px`;
      subItems.style.position = 'absolute';
P
Phil Hughes 已提交
125
      el.style.position = 'relative';
P
Phil Hughes 已提交
126
      el.style.top = `${window.innerHeight - el.offsetHeight}px`;
P
Phil Hughes 已提交
127

P
Phil Hughes 已提交
128
      spyOn(subItems.classList, 'add');
P
Phil Hughes 已提交
129 130 131 132

      showSubLevelItems(el);

      expect(
P
Phil Hughes 已提交
133
        subItems.classList.add,
P
Phil Hughes 已提交
134 135 136
      ).toHaveBeenCalledWith('is-above');
    });
  });
137
});