import { act, screen, waitFor, within } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import shootTypes from '@/content/shootTypes'; import { shootPagePath } from '@/lib/shootPages'; import { FakeIntersectionObserver } from '@/test/fakeIntersectionObserver'; import { mockViewport } from '@/test/navViewport'; import { renderApp } from '@/test/renderApp'; // The real route tree with the real navigation: current state (D4), closing // on a route change and the mobile menu (FR-ST-10) // The homepage's review carousel (Zag) needs an IntersectionObserver and // Element.scrollTo, which jsdom lacks (as in app.test.tsx) beforeEach(() => { vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver); Element.prototype.scrollTo = () => {}; }); afterEach(() => { vi.unstubAllGlobals(); FakeIntersectionObserver.reset(); // @ts-expect-error -- jsdom has no Element.scrollTo of its own delete Element.prototype.scrollTo; }); const PARENT = 'Soorten fotoshoots'; const TOGGLE = 'Submenu Soorten fotoshoots'; const header = () => screen.getByRole('banner'); const panel = () => document.getElementById('submenu-soorten-fotoshoots')!; const footerNav = () => screen.getByRole('navigation', { name: 'Footermenu' }); const toggle = () => screen.getByRole('button', { name: TOGGLE }); const hamburger = () => screen.getByTestId('nav-menu-toggle'); // By text: the name of a link in a closed (hidden) panel is empty const panelLinks = () => within(panel()).getAllByRole('link', { hidden: true }); const panelLink = (text: string) => panelLinks().find((link) => link.textContent === text)!; const parentLink = () => within(header()).getByRole('link', { name: PARENT }); const footerLink = () => within(footerNav()).getByRole('link', { name: PARENT }); async function renderDesktop(path: string) { mockViewport(1280); const rendered = await renderApp(path); // The shell is there once the lazy route has loaded await screen.findByRole('main'); return { ...rendered, user: userEvent.setup() }; } async function renderMobile(path: string) { const viewport = mockViewport(375); const rendered = await renderApp(path); await screen.findByRole('main'); return { ...rendered, viewport, user: userEvent.setup() }; } describe('current page in the navigation (D4)', () => { it.each(shootTypes)( 'marks $name in the panel, the section on the parent and nothing in the footer', async (shoot) => { await renderDesktop(shootPagePath(shoot.id)); for (const link of panelLinks()) { if (link.textContent === shoot.name) { expect(link).toHaveAttribute('aria-current', 'page'); } else { expect(link).not.toHaveAttribute('aria-current'); } } expect(parentLink()).not.toHaveAttribute('aria-current'); expect(parentLink()).toHaveAttribute('data-section-current'); expect(footerLink()).not.toHaveAttribute('aria-current'); }, ); it('marks the parent and the footer on the overview, and no panel link', async () => { await renderDesktop('/soorten-fotoshoots'); expect(parentLink()).toHaveAttribute('aria-current', 'page'); expect(parentLink()).toHaveAttribute('data-section-current'); expect(footerLink()).toHaveAttribute('aria-current', 'page'); for (const link of panelLinks()) { expect(link).not.toHaveAttribute('aria-current'); } }); it('marks no panel link and no section on another page', async () => { await renderDesktop('/portfolio'); for (const link of panelLinks()) { expect(link).not.toHaveAttribute('aria-current'); } expect(parentLink()).not.toHaveAttribute('data-section-current'); expect(parentLink()).not.toHaveAttribute('aria-current'); }); it('treats a trailing slash like the plain path (BR-NAV-04)', async () => { await renderDesktop('/soorten-fotoshoots/newborn/'); expect(panelLink('Newborn')).toHaveAttribute('aria-current', 'page'); expect(parentLink()).toHaveAttribute('data-section-current'); expect(parentLink()).not.toHaveAttribute('aria-current'); expect(footerLink()).not.toHaveAttribute('aria-current'); }); it('shows the page on a case variant, underlines the section but marks no link (router case rule)', async () => { await renderDesktop('/soorten-fotoshoots/Newborn'); // The router matches case-insensitively and renders the page, but // its exact aria-current compares case-sensitively (Step 2). The // only mark left is the breadcrumb's own current item (BR-SP-01). const current = document.querySelectorAll('[aria-current="page"]'); expect(current).toHaveLength(1); expect(current[0]).not.toHaveAttribute('href'); expect(current[0].closest('nav')).toHaveAccessibleName('Kruimelpad'); expect(parentLink()).toHaveAttribute('data-section-current'); }); it('keeps the panel in step with the shoot types (single source)', async () => { await renderDesktop('/'); expect(panelLinks().map((link) => link.textContent)).toEqual( shootTypes.map((shoot) => shoot.name), ); expect(panelLinks().map((link) => link.getAttribute('href'))).toEqual( shootTypes.map((shoot) => shootPagePath(shoot.id)), ); }); }); describe('desktop submenu and the router', () => { it('closes when the route changes', async () => { const { router, user } = await renderDesktop('/'); await user.click(toggle()); expect(toggle()).toHaveAttribute('aria-expanded', 'true'); await act(() => router.navigate({ to: '/portfolio' })); expect(toggle()).toHaveAttribute('aria-expanded', 'false'); }); it('closes on a link choice, opens the page and focuses main (BR-NAV-26)', async () => { const { user } = await renderDesktop('/'); await user.click(toggle()); await user.click( within(panel()).getByRole('link', { name: 'Zwangerschap' }), ); await waitFor(() => expect( screen.getByRole('heading', { level: 1 }), ).toBeInTheDocument(), ); expect(toggle()).toHaveAttribute('aria-expanded', 'false'); await waitFor(() => expect(document.getElementById('main')).toHaveFocus(), ); expect(panelLink('Zwangerschap')).toHaveAttribute( 'aria-current', 'page', ); }); it('does not close on the first render', async () => { const { user } = await renderDesktop('/'); await user.click(toggle()); expect(toggle()).toHaveAttribute('aria-expanded', 'true'); }); }); describe('mobile menu and the router', () => { it('closes when the route changes without a menu click', async () => { const { router, user } = await renderMobile('/contact'); await user.click(hamburger()); expect(hamburger()).toHaveAttribute('aria-expanded', 'true'); await act(() => router.navigate({ to: '/portfolio' })); expect(hamburger()).toHaveAttribute('aria-expanded', 'false'); }); it('closes on the browser back button (BR-NAV-32)', async () => { const { router, user } = await renderMobile('/contact'); await act(() => router.navigate({ to: '/portfolio' })); await user.click(hamburger()); expect(hamburger()).toHaveAttribute('aria-expanded', 'true'); await act(async () => router.history.back()); await waitFor(() => expect(hamburger()).toHaveAttribute('aria-expanded', 'false'), ); expect(router.state.location.pathname).toBe('/contact'); }); it('expands the group when it opens on a shoot page', async () => { const { user } = await renderMobile('/soorten-fotoshoots/loveshoot'); await user.click(hamburger()); const group = within(document.getElementById('mobile-nav')!).getByRole( 'button', { name: TOGGLE }, ); expect(group).toHaveAttribute('aria-expanded', 'true'); expect( within(document.getElementById('mobile-nav')!).getByRole('link', { name: 'Loveshoot', }), ).toHaveAttribute('aria-current', 'page'); }); }); describe('crossing the lg breakpoint', () => { it('closes the mobile menu that no longer exists', async () => { const { viewport, user } = await renderMobile('/contact'); await user.click(hamburger()); expect(hamburger()).toHaveAttribute('aria-expanded', 'true'); viewport.resize(1280); viewport.resize(375); await waitFor(() => expect(hamburger()).toHaveAttribute('aria-expanded', 'false'), ); expect(document.body.style.overflow).toBe(''); }); it('closes the desktop submenu that no longer exists', async () => { const rendered = await renderMobile('/contact'); rendered.viewport.resize(1280); await waitFor(() => expect(toggle()).toBeInTheDocument()); await rendered.user.click(toggle()); expect(toggle()).toHaveAttribute('aria-expanded', 'true'); rendered.viewport.resize(375); rendered.viewport.resize(1280); await waitFor(() => expect(toggle()).toHaveAttribute('aria-expanded', 'false'), ); }); });