docbrown/app/javascript/topNavigation/__tests__/utilities.test.js
ludwiczakpawel 73e560f7a2
Updating navigation (especially mobile) (#10424)
* .

* .

* .

* .

* .

* specs

* specs

* .

* .

* merge

* moving cheese around

* layout

* .

* merge

* Fixed invalid selector for search.

* Fixed issue with URL not updating after a search.

* do things better

* kinda merge

* whoops

* whoops

* .

* .

* im a backend developer

* tooltips 1.0.1

* tooltips 1.0.1

* fix

* specs

* .

* reduce the spacing

* schema drop

* reverts

* revert

* reverts

* revert

* drop unused nav

* drop unused nav

* drop unused nav

* drop unused nav

* typos

* paths fixes

* speeding up JS

* js

* search

* Reworked initializeTopBarIcons.js

* Reworked initializeNavigation.js

* Small refactor and updated exported function API docs.

* Fixed a bug I created when refactoring the top navigation icons.

* sidebar nav links

* spec

* Fixed issues with logged out experience for top nav icons.

* Small refactor of top nav icons.

* Small refactor and added some API docs.

* Added @testing-library/dom

* Added tests for top navigation utilities.

* spec

* spec

Co-authored-by: Nick Taylor <nick@dev.to>
2020-12-08 15:59:21 +01:00

137 lines
5 KiB
JavaScript

// TODO: Once this is merged, PR up the removal of this and in other tests and move it to testSetup.js
import '@testing-library/jest-dom';
import { getByLabelText, getByText } from '@testing-library/dom';
import {
getInstantClick,
initializeMobileMenu,
setCurrentPageIconLink,
} from '../utilities';
// TODO: ★★★ These tests should be promoted to E2E tests once we have that in place. ★★★
describe('top navigation utilitities', () => {
beforeEach(() => {
// Recreating the body element completely so as to remove any CSS classes
// that were added to it.
document.body = document.createElement('body');
});
describe('getInstantClick', () => {
it('should fail if not found in the alloted time.', async () => {
await expect(getInstantClick(50)).rejects.toEqual(
new Error('Unable to resolve InstantClick'),
);
});
it('should resolve InstantClick.', async () => {
global.InstantClick = {};
expect(await getInstantClick(50)).toEqual(InstantClick);
delete global.InstantClick;
});
});
describe('initializeMobileMenu', () => {
it('should open the hamburger menu', () => {
document.body.innerHTML = `
<button aria-label="nav-button-left" class="crayons-btn crayons-btn--ghost crayons-btn--icon-rounded js-hamburger-trigger inline-block m:hidden mx-2">
<svg><title>Navigation menu</title></svg>
</button>
`;
const navButton = getByLabelText(document.body, 'nav-button-left');
initializeMobileMenu([navButton], []);
expect(document.body).not.toHaveClass('hamburger-open');
navButton.click();
expect(document.body).toHaveClass('hamburger-open');
});
it('should close the hamburger menu', () => {
document.body.innerHTML = `
<button aria-label="nav-button-left" class="crayons-btn crayons-btn--ghost crayons-btn--icon-rounded js-hamburger-trigger inline-block m:hidden mx-2">
<svg><title>Navigation menu</title></svg>
</button>
`;
const navButton = getByLabelText(document.body, 'nav-button-left');
initializeMobileMenu([navButton], []);
expect(document.body).not.toHaveClass('hamburger-open');
navButton.click();
expect(document.body).toHaveClass('hamburger-open');
navButton.click();
expect(document.body).not.toHaveClass('hamburger-open');
});
it('should open the more menu', () => {
document.body.innerHTML = `
<a href="javascript:void(0)" class="crayons-link crayons-link--secondary crayons-link--block crayons-link--block--indented fs-s js-nav-more-trigger">More...</a>
<div class="hidden js-nav-more spec-nav-more">
<div class="flex justify-around p-4 mt-4 border-solid border-0 border-t-1 border-base-10">
</div>
</div>
`;
const navMoreLink = getByText(document.body, 'More...');
initializeMobileMenu([], [navMoreLink]);
navMoreLink.click();
expect(navMoreLink).toHaveClass('hidden');
});
});
describe('setCurrentPageIconLink', () => {
it('should set the current page icon', () => {
document.body.innerHTML = `
<a href="/connect" id="connect-link" class="crayons-header__link crayons-btn crayons-btn--ghost crayons-btn--icon-rounded" aria-label="Connect"></a>
<a href="/notifications" id="notifications-link" class="crayons-header__link crayons-btn crayons-btn--ghost crayons-btn--icon-rounded" aria-label="Notifications"></a>
`;
const connectLink = getByLabelText(document.body, 'Connect');
const notificationsLink = getByLabelText(document.body, 'Notifications');
const pageEntries = Object.entries({
'page-1': connectLink,
'page-2': notificationsLink,
});
const page = 'page-1';
setCurrentPageIconLink(page, pageEntries);
expect(connectLink).toHaveClass('crayons-header__link--current');
expect(notificationsLink).not.toHaveClass(
'crayons-header__link--current',
);
});
it('should set the current page icon and remove the previous current page icon styling', () => {
document.body.innerHTML = `
<a href="/connect" id="connect-link" class="crayons-header__link crayons-btn crayons-btn--ghost crayons-btn--icon-rounded" aria-label="Connect"></a>
<a href="/notifications" id="notifications-link" class="crayons-header__link crayons-header__link--current crayons-btn crayons-btn--ghost crayons-btn--icon-rounded" aria-label="Notifications"></a>
`;
const connectLink = getByLabelText(document.body, 'Connect');
const notificationsLink = getByLabelText(document.body, 'Notifications');
const pageEntries = Object.entries({
'page-1': connectLink,
'page-2': notificationsLink,
});
const page = 'page-1';
setCurrentPageIconLink(page, pageEntries);
expect(connectLink).toHaveClass('crayons-header__link--current');
expect(notificationsLink).not.toHaveClass(
'crayons-header__link--current',
);
});
});
});