// 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 { getByTestId, getByLabelText, getByText, fireEvent, waitFor, } from '@testing-library/dom'; import { getInstantClick, initializeMobileMenu, setCurrentPageIconLink, initializeMemberMenu, } 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 = ` `; const navButton = getByLabelText(document.body, 'nav-button-left'); initializeMobileMenu([navButton], []); expect(document.body.dataset.leftNavState).toBeUndefined(); navButton.click(); expect(document.body.dataset.leftNavState).toEqual('open'); }); it('should close the hamburger menu', () => { document.body.innerHTML = ` `; const navButton = getByLabelText(document.body, 'nav-button-left'); initializeMobileMenu([navButton], []); expect(document.body.dataset.leftNavState).toBeUndefined(); navButton.click(); expect(document.body.dataset.leftNavState).toEqual('open'); navButton.click(); expect(document.body.dataset.leftNavState).toEqual('closed'); }); it('should open the more menu', () => { document.body.innerHTML = ` More... `; 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 = ` `; 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 = ` `; 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', ); }); }); describe('initializeMemberMenu', () => { const initialMenuHTML = `
`; it('toggles dropdown menu expanded state', async () => { document.body.innerHTML = initialMenuHTML; const menuNavButton = getByLabelText(document.body, 'Navigation menu'); const memberMenu = getByTestId(document.body, 'menu-dropdown'); initializeMemberMenu(memberMenu, menuNavButton); await waitFor(() => expect(menuNavButton.getAttribute('aria-expanded')).toEqual('false'), ); expect(menuNavButton.getAttribute('aria-expanded')).toEqual('false'); expect(memberMenu).not.toHaveClass('showing'); fireEvent.click(menuNavButton); expect(menuNavButton.getAttribute('aria-expanded')).toEqual('true'); expect(memberMenu).toHaveClass('showing'); fireEvent.click(menuNavButton); expect(menuNavButton.getAttribute('aria-expanded')).toEqual('false'); expect(memberMenu).not.toHaveClass('showing'); }); it('closes menu on Escape key', async () => { document.body.innerHTML = initialMenuHTML; const menuNavButton = getByLabelText(document.body, 'Navigation menu'); const memberMenu = getByTestId(document.body, 'menu-dropdown'); initializeMemberMenu(memberMenu, menuNavButton); await waitFor(() => expect(menuNavButton.getAttribute('aria-expanded')).toEqual('false'), ); fireEvent.click(menuNavButton); expect(menuNavButton.getAttribute('aria-expanded')).toEqual('true'); expect(memberMenu).toHaveClass('showing'); fireEvent.keyUp(memberMenu, { key: 'Escape' }); await waitFor(() => expect(menuNavButton.getAttribute('aria-expanded')).toEqual('false'), ); expect(memberMenu).not.toHaveClass('showing'); }); it('closes the menu on click outside', async () => { document.body.innerHTML = `
${initialMenuHTML}
`; const menuNavButton = getByLabelText(document.body, 'Navigation menu'); const memberMenu = getByTestId(document.body, 'menu-dropdown'); initializeMemberMenu(memberMenu, menuNavButton); await waitFor(() => expect(menuNavButton.getAttribute('aria-expanded')).toEqual('false'), ); fireEvent.click(menuNavButton); expect(menuNavButton.getAttribute('aria-expanded')).toEqual('true'); expect(memberMenu).toHaveClass('showing'); // Click on a link in the menu to close load a page and close the menu. memberMenu.querySelector('a').click(); await waitFor(() => expect(menuNavButton.getAttribute('aria-expanded')).toEqual('false'), ); expect(memberMenu).not.toHaveClass('showing'); }); }); });