fly_out_nav_spec.js 8.07 KB
Newer Older
1
import Cookies from 'js-cookie';
2 3 4
import {
  calculateTop,
  showSubLevelItems,
5
  canShowSubItems,
6
  canShowActiveSubItems,
7 8 9 10 11 12
  mouseEnterTopItems,
  mouseLeaveTopItem,
  setOpenMenu,
  mousePos,
  getHideSubItemsInterval,
  documentMouseMove,
13
} from '~/fly_out_nav';
14
import bp from '~/breakpoints';
15

16
describe('Fly out sidebar navigation', () => {
17
  let el;
18 19
  let breakpointSize = 'lg';

20 21
  beforeEach(() => {
    el = document.createElement('div');
Phil Hughes's avatar
Phil Hughes committed
22
    el.style.position = 'relative';
23
    document.body.appendChild(el);
24 25

    spyOn(bp, 'getBreakpointSize').and.callFake(() => breakpointSize);
26 27

    setOpenMenu(null);
28 29 30
  });

  afterEach(() => {
31
    document.body.innerHTML = '';
32
    breakpointSize = 'lg';
33
    mousePos.length = 0;
34 35
  });

36 37 38 39
  describe('calculateTop', () => {
    it('returns boundingRect top', () => {
      const boundingRect = {
        top: 100,
40
        height: 100,
41 42 43 44 45 46 47 48 49
      };

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

    it('returns boundingRect - bottomOverflow', () => {
      const boundingRect = {
50 51
        top: window.innerHeight - 50,
        height: 100,
52 53 54 55
      };

      expect(
        calculateTop(boundingRect, 100),
56
      ).toBe(window.innerHeight - 50);
57 58
    });
  });
59

60
  describe('getHideSubItemsInterval', () => {
61
    beforeEach(() => {
62
      el.innerHTML = '<div class="sidebar-sub-level-items" style="position: fixed; top: 0; left: 100px; height: 50px;"></div>';
63 64
    });

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    it('returns 0 if currentOpenMenu is nil', () => {
      expect(
        getHideSubItemsInterval(),
      ).toBe(0);
    });

    it('returns 0 when mouse above sub-items', () => {
      showSubLevelItems(el);
      documentMouseMove({
        clientX: el.getBoundingClientRect().left,
        clientY: el.getBoundingClientRect().top,
      });
      documentMouseMove({
        clientX: el.getBoundingClientRect().left,
        clientY: el.getBoundingClientRect().top - 50,
      });
81 82

      expect(
83 84
        getHideSubItemsInterval(),
      ).toBe(0);
85 86
    });

87 88
    it('returns 0 when mouse is below sub-items', () => {
      const subItems = el.querySelector('.sidebar-sub-level-items');
89

90 91 92 93 94 95 96 97 98
      showSubLevelItems(el);
      documentMouseMove({
        clientX: el.getBoundingClientRect().left,
        clientY: el.getBoundingClientRect().top,
      });
      documentMouseMove({
        clientX: el.getBoundingClientRect().left,
        clientY: (el.getBoundingClientRect().top - subItems.getBoundingClientRect().height) + 50,
      });
99 100

      expect(
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        getHideSubItemsInterval(),
      ).toBe(0);
    });

    it('returns 300 when mouse is moved towards sub-items', () => {
      documentMouseMove({
        clientX: el.getBoundingClientRect().left,
        clientY: el.getBoundingClientRect().top,
      });
      showSubLevelItems(el);
      documentMouseMove({
        clientX: el.getBoundingClientRect().left + 20,
        clientY: el.getBoundingClientRect().top + 10,
      });

      expect(
        getHideSubItemsInterval(),
      ).toBe(300);
119
    });
120
  });
121

122 123
  describe('mouseLeaveTopItem', () => {
    beforeEach(() => {
124
      spyOn(el.classList, 'remove');
125
    });
126

127 128
    it('removes is-over class if currentOpenMenu is null', () => {
      mouseLeaveTopItem(el);
129 130 131 132 133 134

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

135 136 137 138 139 140 141 142 143
    it('removes is-over class if currentOpenMenu is null & there are sub-items', () => {
      el.innerHTML = '<div class="sidebar-sub-level-items" style="position: absolute;"></div>';

      mouseLeaveTopItem(el);

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

145 146
    it('does not remove is-over class if currentOpenMenu is the passed in sub-items', () => {
      el.innerHTML = '<div class="sidebar-sub-level-items" style="position: absolute;"></div>';
147

148 149
      setOpenMenu(el.querySelector('.sidebar-sub-level-items'));
      mouseLeaveTopItem(el);
150 151

      expect(
152 153
        el.classList.remove,
      ).not.toHaveBeenCalled();
154
    });
155
  });
156

157 158 159
  describe('mouseEnterTopItems', () => {
    beforeEach(() => {
      jasmine.clock().install();
160

161 162
      el.innerHTML = '<div class="sidebar-sub-level-items" style="position: absolute; top: 0; left: 100px; height: 200px;"></div>';
    });
163

164 165 166 167 168 169
    afterEach(() => {
      jasmine.clock().uninstall();
    });

    it('shows sub-items after 0ms if no menu is open', () => {
      mouseEnterTopItems(el);
170 171

      expect(
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
        getHideSubItemsInterval(),
      ).toBe(0);

      jasmine.clock().tick(0);

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

    it('shows sub-items after 300ms if a menu is currently open', () => {
      documentMouseMove({
        clientX: el.getBoundingClientRect().left,
        clientY: el.getBoundingClientRect().top,
      });

      setOpenMenu(el.querySelector('.sidebar-sub-level-items'));

      documentMouseMove({
        clientX: el.getBoundingClientRect().left + 20,
        clientY: el.getBoundingClientRect().top + 10,
      });

      mouseEnterTopItems(el);

      expect(
        getHideSubItemsInterval(),
      ).toBe(300);

      jasmine.clock().tick(300);

      expect(
        el.querySelector('.sidebar-sub-level-items').style.display,
      ).toBe('block');
206 207 208 209 210
    });
  });

  describe('showSubLevelItems', () => {
    beforeEach(() => {
Phil Hughes's avatar
Phil Hughes committed
211
      el.innerHTML = '<div class="sidebar-sub-level-items" style="position: absolute;"></div>';
212 213 214 215 216 217 218 219 220 221 222 223
    });

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

      showSubLevelItems(el);

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

224
    it('does not show sub-items on mobile', () => {
225
      breakpointSize = 'xs';
226 227 228 229 230 231 232 233

      showSubLevelItems(el);

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

234
    it('shows sub-items', () => {
235 236 237 238 239 240 241 242
      showSubLevelItems(el);

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

    it('sets transform of sub-items', () => {
Phil Hughes's avatar
Phil Hughes committed
243
      const subItems = el.querySelector('.sidebar-sub-level-items');
244 245 246
      showSubLevelItems(el);

      expect(
Phil Hughes's avatar
Phil Hughes committed
247
        subItems.style.transform,
248
      ).toBe(`translate3d(0px, ${Math.floor(el.getBoundingClientRect().top)}px, 0px)`);
249 250 251 252
    });

    it('sets is-above when element is above', () => {
      const subItems = el.querySelector('.sidebar-sub-level-items');
Phil Hughes's avatar
Phil Hughes committed
253 254
      subItems.style.height = `${window.innerHeight + el.offsetHeight}px`;
      el.style.top = `${window.innerHeight - el.offsetHeight}px`;
255

Phil Hughes's avatar
Phil Hughes committed
256
      spyOn(subItems.classList, 'add');
257 258 259 260

      showSubLevelItems(el);

      expect(
Phil Hughes's avatar
Phil Hughes committed
261
        subItems.classList.add,
262 263 264
      ).toHaveBeenCalledWith('is-above');
    });
  });
265 266 267 268 269 270 271 272 273

  describe('canShowSubItems', () => {
    it('returns true if on desktop size', () => {
      expect(
        canShowSubItems(),
      ).toBeTruthy();
    });

    it('returns false if on mobile size', () => {
274
      breakpointSize = 'xs';
275 276 277 278 279 280

      expect(
        canShowSubItems(),
      ).toBeFalsy();
    });
  });
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

  describe('canShowActiveSubItems', () => {
    afterEach(() => {
      Cookies.remove('sidebar_collapsed');
    });

    it('returns true by default', () => {
      expect(
        canShowActiveSubItems(el),
      ).toBeTruthy();
    });

    it('returns false when cookie is false & element is active', () => {
      Cookies.set('sidebar_collapsed', 'false');
      el.classList.add('active');

      expect(
        canShowActiveSubItems(el),
      ).toBeFalsy();
    });

    it('returns true when cookie is false & element is active', () => {
      Cookies.set('sidebar_collapsed', 'true');
      el.classList.add('active');

      expect(
        canShowActiveSubItems(el),
      ).toBeTruthy();
    });

    it('returns true when element is active & breakpoint is sm', () => {
      breakpointSize = 'sm';
      el.classList.add('active');

      expect(
        canShowActiveSubItems(el),
      ).toBeTruthy();
    });

    it('returns true when element is active & breakpoint is md', () => {
      breakpointSize = 'md';
      el.classList.add('active');

      expect(
        canShowActiveSubItems(el),
      ).toBeTruthy();
    });
  });
329
});