gitlab-org--gitlab-foss/spec/javascripts/fly_out_nav_spec.js

382 lines
9.4 KiB
JavaScript
Raw Normal View History

2017-08-01 03:49:03 -04:00
import {
calculateTop,
showSubLevelItems,
2017-08-02 05:22:13 -04:00
canShowSubItems,
canShowActiveSubItems,
mouseEnterTopItems,
mouseLeaveTopItem,
getOpenMenu,
setOpenMenu,
mousePos,
getHideSubItemsInterval,
documentMouseMove,
getHeaderHeight,
setSidebar,
subItemsMouseLeave,
2017-08-01 03:49:03 -04:00
} from '~/fly_out_nav';
import bp from '~/breakpoints';
describe('Fly out sidebar navigation', () => {
2017-08-01 03:49:03 -04:00
let el;
2017-08-02 05:22:13 -04:00
let breakpointSize = 'lg';
2017-08-01 03:49:03 -04:00
beforeEach(() => {
el = document.createElement('div');
2017-08-01 07:41:55 -04:00
el.style.position = 'relative';
2017-08-01 03:49:03 -04:00
document.body.appendChild(el);
2017-08-02 05:22:13 -04:00
spyOn(bp, 'getBreakpointSize').and.callFake(() => breakpointSize);
setOpenMenu(null);
2017-08-01 03:49:03 -04:00
});
afterEach(() => {
document.body.innerHTML = '';
2017-08-02 05:22:13 -04:00
breakpointSize = 'lg';
mousePos.length = 0;
setSidebar(null);
2017-08-01 03:49:03 -04:00
});
describe('calculateTop', () => {
it('returns boundingRect top', () => {
const boundingRect = {
top: 100,
height: 100,
};
expect(
calculateTop(boundingRect, 100),
).toBe(100);
});
it('returns boundingRect - bottomOverflow', () => {
const boundingRect = {
top: window.innerHeight - 50,
height: 100,
};
expect(
calculateTop(boundingRect, 100),
).toBe(window.innerHeight - 50);
});
});
2017-08-01 03:49:03 -04:00
describe('getHideSubItemsInterval', () => {
2017-08-01 03:49:03 -04:00
beforeEach(() => {
el.innerHTML = '<div class="sidebar-sub-level-items" style="position: fixed; top: 0; left: 100px; height: 150px;"></div>';
2017-08-01 03:49:03 -04:00
});
it('returns 0 if currentOpenMenu is nil', () => {
expect(
getHideSubItemsInterval(),
).toBe(0);
});
it('returns 0 if mousePos is empty', () => {
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,
});
2017-08-01 03:49:03 -04:00
expect(
getHideSubItemsInterval(),
).toBe(0);
2017-08-01 03:49:03 -04:00
});
it('returns 0 when mouse is below sub-items', () => {
const subItems = el.querySelector('.sidebar-sub-level-items');
2017-08-02 05:22:13 -04:00
showSubLevelItems(el);
documentMouseMove({
clientX: el.getBoundingClientRect().left,
clientY: el.getBoundingClientRect().top,
});
documentMouseMove({
clientX: el.getBoundingClientRect().left,
clientY: (el.getBoundingClientRect().top - subItems.getBoundingClientRect().height) + 50,
});
2017-08-02 05:22:13 -04:00
expect(
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);
2017-08-02 05:22:13 -04:00
});
});
2017-08-02 05:22:13 -04:00
describe('mouseLeaveTopItem', () => {
beforeEach(() => {
2017-08-01 03:49:03 -04:00
spyOn(el.classList, 'remove');
});
2017-08-01 03:49:03 -04:00
it('removes is-over class if currentOpenMenu is null', () => {
mouseLeaveTopItem(el);
2017-08-01 03:49:03 -04:00
expect(
el.classList.remove,
).toHaveBeenCalledWith('is-over');
});
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');
});
2017-08-01 03:49:03 -04:00
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>';
2017-08-01 03:49:03 -04:00
setOpenMenu(el.querySelector('.sidebar-sub-level-items'));
mouseLeaveTopItem(el);
2017-08-01 03:49:03 -04:00
expect(
el.classList.remove,
).not.toHaveBeenCalled();
2017-08-01 03:49:03 -04:00
});
});
2017-08-01 03:49:03 -04:00
describe('mouseEnterTopItems', () => {
beforeEach(() => {
jasmine.clock().install();
2017-08-01 03:49:03 -04:00
el.innerHTML = '<div class="sidebar-sub-level-items" style="position: absolute; top: 0; left: 100px; height: 200px;"></div>';
});
2017-08-01 03:49:03 -04:00
afterEach(() => {
jasmine.clock().uninstall();
});
it('shows sub-items after 0ms if no menu is open', () => {
mouseEnterTopItems(el);
2017-08-01 03:49:03 -04:00
expect(
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');
2017-08-01 03:49:03 -04:00
});
});
describe('showSubLevelItems', () => {
beforeEach(() => {
2017-08-01 07:41:55 -04:00
el.innerHTML = '<div class="sidebar-sub-level-items" style="position: absolute;"></div>';
2017-08-01 03:49:03 -04:00
});
it('adds is-over class to el', () => {
spyOn(el.classList, 'add');
showSubLevelItems(el);
expect(
el.classList.add,
).toHaveBeenCalledWith('is-over');
});
2017-08-02 05:22:13 -04:00
it('does not show sub-items on mobile', () => {
breakpointSize = 'xs';
2017-08-02 05:22:13 -04:00
showSubLevelItems(el);
expect(
el.querySelector('.sidebar-sub-level-items').style.display,
).not.toBe('block');
});
it('shows sub-items', () => {
2017-08-01 03:49:03 -04:00
showSubLevelItems(el);
expect(
el.querySelector('.sidebar-sub-level-items').style.display,
).toBe('block');
});
it('shows collapsed only sub-items if icon only sidebar', () => {
const subItems = el.querySelector('.sidebar-sub-level-items');
const sidebar = document.createElement('div');
sidebar.classList.add('sidebar-icons-only');
subItems.classList.add('is-fly-out-only');
setSidebar(sidebar);
showSubLevelItems(el);
expect(
el.querySelector('.sidebar-sub-level-items').style.display,
).toBe('block');
});
it('does not show collapsed only sub-items if icon only sidebar', () => {
const subItems = el.querySelector('.sidebar-sub-level-items');
subItems.classList.add('is-fly-out-only');
showSubLevelItems(el);
expect(
subItems.style.display,
).not.toBe('block');
});
2017-08-01 03:49:03 -04:00
it('sets transform of sub-items', () => {
const sidebar = document.createElement('div');
2017-08-01 07:41:55 -04:00
const subItems = el.querySelector('.sidebar-sub-level-items');
sidebar.style.width = '200px';
document.body.appendChild(sidebar);
setSidebar(sidebar);
2017-08-01 03:49:03 -04:00
showSubLevelItems(el);
expect(
2017-08-01 07:41:55 -04:00
subItems.style.transform,
).toBe(`translate3d(200px, ${Math.floor(el.getBoundingClientRect().top) - getHeaderHeight()}px, 0px)`);
2017-08-01 03:49:03 -04:00
});
it('sets is-above when element is above', () => {
const subItems = el.querySelector('.sidebar-sub-level-items');
subItems.style.height = `${window.innerHeight + el.offsetHeight}px`;
el.style.top = `${window.innerHeight - el.offsetHeight}px`;
2017-08-01 03:49:03 -04:00
spyOn(subItems.classList, 'add');
2017-08-01 03:49:03 -04:00
showSubLevelItems(el);
expect(
subItems.classList.add,
2017-08-01 03:49:03 -04:00
).toHaveBeenCalledWith('is-above');
});
});
2017-08-02 05:22:13 -04:00
describe('canShowSubItems', () => {
it('returns true if on desktop size', () => {
expect(
canShowSubItems(),
).toBeTruthy();
});
it('returns false if on mobile size', () => {
breakpointSize = 'xs';
2017-08-02 05:22:13 -04:00
expect(
canShowSubItems(),
).toBeFalsy();
});
});
describe('canShowActiveSubItems', () => {
it('returns true by default', () => {
expect(
canShowActiveSubItems(el),
).toBeTruthy();
});
it('returns false when active & expanded sidebar', () => {
const sidebar = document.createElement('div');
el.classList.add('active');
setSidebar(sidebar);
expect(
canShowActiveSubItems(el),
).toBeFalsy();
});
it('returns true when active & collapsed sidebar', () => {
const sidebar = document.createElement('div');
sidebar.classList.add('sidebar-icons-only');
el.classList.add('active');
setSidebar(sidebar);
expect(
canShowActiveSubItems(el),
).toBeTruthy();
});
});
describe('subItemsMouseLeave', () => {
beforeEach(() => {
el.innerHTML = '<div class="sidebar-sub-level-items" style="position: absolute;"></div>';
setOpenMenu(el.querySelector('.sidebar-sub-level-items'));
});
it('hides subMenu if element is not hovered', () => {
subItemsMouseLeave(el);
expect(
getOpenMenu(),
).toBeNull();
});
it('does not hide subMenu if element is hovered', () => {
el.classList.add('is-over');
subItemsMouseLeave(el);
expect(
getOpenMenu(),
).not.toBeNull();
});
});
});