import { cleanup, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import type { FaqItem } from '@/content/types'; import { faqItems } from '@/test/arbitraries'; import { axeResults } from '@/test/axe'; import { renderWithProviders } from '@/test/renderWithProviders'; import FaqList from './FaqList'; const items: FaqItem[] = [ { id: 'een', question: 'Wanneer boek ik?', answer: [ 'Zo vroeg mogelijk, zie ', { text: 'de prijzen', to: '/', hash: 'prijzen' }, '.', ], }, { id: 'twee', question: 'Waar?', answer: ['Bij jullie thuis.'] }, { id: 'drie', question: 'Hoe lang?', answer: ['Twee uur.'] }, ]; describe('FaqList', () => { it('shows every question closed, with the answers in the page', async () => { const { container } = await renderWithProviders( , ); const details = container.querySelectorAll('details'); expect(details).toHaveLength(3); for (const item of details) expect(item).not.toHaveAttribute('open'); for (const item of items) { expect(screen.getByText(item.question).tagName).toBe('SUMMARY'); } expect( screen.getByText('Bij jullie thuis.', { exact: false }), ).toBeInTheDocument(); expect( screen.getByRole('link', { name: 'de prijzen' }), ).toHaveAttribute('href', '/#prijzen'); // The questions are labels, not headings expect(screen.queryByRole('heading')).toBeNull(); expect( container.querySelectorAll('summary svg[aria-hidden="true"]'), ).toHaveLength(3); expect(await axeResults(container)).toHaveNoViolations(); }); // jsdom does not turn Enter or Space on a summary into the click that // opens the details (browsers do, for any summary that is the first // child of its details), and user-event only does so for buttons and // links. So the test checks what the page controls: the summary is // focusable and is the first child of its details, and activating it // (the click a browser sends) toggles the item. it('has a focusable summary as first child, which toggles the item when activated', async () => { const user = userEvent.setup(); const { container } = await renderWithProviders( , ); const first = container.querySelector('details')!; const summary = first.firstElementChild as HTMLElement; expect(summary.tagName).toBe('SUMMARY'); expect(summary).toHaveTextContent('Wanneer boek ik?'); expect(summary.getAttribute('tabindex')).not.toBe('-1'); await user.tab(); expect(summary).toHaveFocus(); await user.click(summary); expect(first).toHaveAttribute('open'); await user.click(summary); expect(first).not.toHaveAttribute('open'); }); it('renders nothing without questions', async () => { const { container } = await renderWithProviders(); expect(container.querySelector('details')).toBeNull(); }); it('has one closed details per item, with every answer in the page (invariant)', async () => { for (const sample of fc.sample(faqItems, { numRuns: 6 })) { const { container } = await renderWithProviders( , ); const details = container.querySelectorAll('details'); expect(details).toHaveLength(sample.length); details.forEach((item, index) => { expect(item.hasAttribute('open')).toBe(false); expect(item.querySelector('summary')!.textContent).toBe( sample[index].question, ); const answer = sample[index].answer .map((piece) => typeof piece === 'string' ? piece : piece.text, ) .join(''); expect(item.querySelector('p')!.textContent).toBe(answer); }); cleanup(); } }); });