|
| 1 | +import { afterAll, afterEach, beforeAll, expect, test } from 'bun:test' |
| 2 | +import { cleanup, render, within } from '@testing-library/react' |
| 3 | +import { MemoryRouter } from 'react-router' |
| 4 | +import { setupDom, teardownDom } from '../test-setup' |
| 5 | +import { BottomNav } from './BottomNav' |
| 6 | + |
| 7 | +// `@testing-library/dom`'s `screen` singleton snapshots `document` the |
| 8 | +// moment the module is first imported (dist/screen.js), so it only works |
| 9 | +// when a global document exists before any test file's imports run — which |
| 10 | +// means registering it process-wide via bunfig's preload, for every |
| 11 | +// workspace in this monorepo. That broke unrelated suites elsewhere (see |
| 12 | +// test-setup.ts). Using the queries `render` returns needs the DOM only once |
| 13 | +// the test body actually runs, so registering it here in beforeAll, scoped to |
| 14 | +// this file, is enough. |
| 15 | +beforeAll(setupDom) |
| 16 | +afterAll(teardownDom) |
| 17 | + |
| 18 | +// Every render is appended to the same document and stays there, so without |
| 19 | +// this each test sees the leftovers of the ones before it. |
| 20 | +afterEach(cleanup) |
| 21 | + |
| 22 | +// Queries are scoped to the container this render owns rather than the whole |
| 23 | +// body. The hub renders a link labelled Filters too, so a document-wide query |
| 24 | +// finds more than one and getByRole throws for being ambiguous. Whether that |
| 25 | +// happened depended on which files had already run, which is why this passed |
| 26 | +// locally and failed in CI. |
| 27 | +test('shows the four primary destinations in order', () => { |
| 28 | + const { container } = render( |
| 29 | + <MemoryRouter initialEntries={['/map']}> |
| 30 | + <BottomNav /> |
| 31 | + </MemoryRouter>, |
| 32 | + ) |
| 33 | + const labels = within(container) |
| 34 | + .getAllByRole('link') |
| 35 | + .map((link) => link.textContent) |
| 36 | + expect(labels).toEqual(['Map', 'Filters', 'Alerts', 'Me']) |
| 37 | +}) |
| 38 | + |
| 39 | +test('marks the active destination for assistive tech', () => { |
| 40 | + const { container } = render( |
| 41 | + <MemoryRouter initialEntries={['/filters']}> |
| 42 | + <BottomNav /> |
| 43 | + </MemoryRouter>, |
| 44 | + ) |
| 45 | + const active = within(container).getByRole('link', { name: 'Filters' }) |
| 46 | + expect(active.getAttribute('aria-current')).toBe('page') |
| 47 | +}) |
0 commit comments