|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + * |
| 4 | + * Accessibility contract for the overlay surfaces migrated onto the shared `<Modal>` |
| 5 | + * primitive: ChannelMapEditor, CopyFromAnimalDialog, and CalendarDayCreator. Each |
| 6 | + * must open as a real `role="dialog"` (aria-modal), trap Tab/Shift-Tab focus, return |
| 7 | + * focus to its opener on close, and close on Escape — the behaviors the primitive |
| 8 | + * owns. One parameterized case per dialog. |
| 9 | + */ |
| 10 | +import { useState } from 'react'; |
| 11 | +import { describe, it, expect, vi } from 'vitest'; |
| 12 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 13 | +import userEvent from '@testing-library/user-event'; |
| 14 | +import ChannelMapEditor from '../../pages/AnimalEditor/ChannelMapEditor'; |
| 15 | +import CopyFromAnimalDialog from '../../pages/AnimalEditor/CopyFromAnimalDialog'; |
| 16 | +import { CalendarDayCreator } from '../../components/CalendarDayCreator/CalendarDayCreator'; |
| 17 | + |
| 18 | +const DIALOGS = [ |
| 19 | + { |
| 20 | + name: 'ChannelMapEditor', |
| 21 | + accessibleName: /channel map editor/i, |
| 22 | + render: (onClose) => ( |
| 23 | + <ChannelMapEditor |
| 24 | + electrodeGroup={{ id: 'eg1', device_type: 'tetrode_12.5', location: 'CA1' }} |
| 25 | + channelMaps={[ |
| 26 | + { |
| 27 | + electrode_group_id: 'eg1', |
| 28 | + ntrode_id: '0', |
| 29 | + electrode_id: 0, |
| 30 | + bad_channels: [], |
| 31 | + map: { 0: 0, 1: 1, 2: 2, 3: 3 }, |
| 32 | + }, |
| 33 | + ]} |
| 34 | + onSave={() => {}} |
| 35 | + onCancel={onClose} |
| 36 | + /> |
| 37 | + ), |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'CopyFromAnimalDialog', |
| 41 | + accessibleName: /copy electrode groups/i, |
| 42 | + render: (onClose) => ( |
| 43 | + <CopyFromAnimalDialog |
| 44 | + open |
| 45 | + currentAnimalId="a1" |
| 46 | + animals={{ |
| 47 | + a1: { |
| 48 | + subject: { subject_id: 'a1' }, |
| 49 | + devices: { electrode_groups: [], ntrode_electrode_group_channel_map: [] }, |
| 50 | + }, |
| 51 | + a2: { |
| 52 | + subject: { subject_id: 'a2' }, |
| 53 | + devices: { |
| 54 | + electrode_groups: [{ id: '1', device_type: 'tetrode_12.5', location: 'CA1' }], |
| 55 | + ntrode_electrode_group_channel_map: [], |
| 56 | + }, |
| 57 | + }, |
| 58 | + }} |
| 59 | + onCopy={() => {}} |
| 60 | + onCancel={onClose} |
| 61 | + /> |
| 62 | + ), |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'CalendarDayCreator', |
| 66 | + accessibleName: /recording days calendar/i, |
| 67 | + render: (onClose) => ( |
| 68 | + <CalendarDayCreator |
| 69 | + animalId="a1" |
| 70 | + existingDays={[]} |
| 71 | + onCreateDays={() => {}} |
| 72 | + onClose={onClose} |
| 73 | + /> |
| 74 | + ), |
| 75 | + }, |
| 76 | +]; |
| 77 | + |
| 78 | +/** |
| 79 | + * Harness with an opener button so focus-return has a real element to return to. |
| 80 | + * Starts closed; the test focuses + clicks the opener to open the dialog. |
| 81 | + * @param {object} props |
| 82 | + * @param {(onClose: Function) => JSX.Element} props.renderDialog - Renders the dialog. |
| 83 | + * @returns {JSX.Element} |
| 84 | + */ |
| 85 | +function Harness({ renderDialog }) { |
| 86 | + const [open, setOpen] = useState(false); |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <button type="button" data-testid="opener" onClick={() => setOpen(true)}> |
| 90 | + opener |
| 91 | + </button> |
| 92 | + {open && renderDialog(() => setOpen(false))} |
| 93 | + </> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +describe.each(DIALOGS)('migrated dialog on <Modal>: $name', ({ accessibleName, render: renderDialog }) => { |
| 98 | + it('opens as an aria-modal dialog with the expected accessible name', async () => { |
| 99 | + const user = userEvent.setup(); |
| 100 | + render(<Harness renderDialog={renderDialog} />); |
| 101 | + |
| 102 | + await user.click(screen.getByTestId('opener')); |
| 103 | + |
| 104 | + const dialog = screen.getByRole('dialog', { name: accessibleName }); |
| 105 | + expect(dialog).toHaveAttribute('aria-modal', 'true'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('traps focus within the dialog when tabbing past the last element', async () => { |
| 109 | + const user = userEvent.setup(); |
| 110 | + render(<Harness renderDialog={renderDialog} />); |
| 111 | + |
| 112 | + await user.click(screen.getByTestId('opener')); |
| 113 | + const dialog = screen.getByRole('dialog'); |
| 114 | + |
| 115 | + const focusables = dialog.querySelectorAll( |
| 116 | + 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])' |
| 117 | + ); |
| 118 | + expect(focusables.length).toBeGreaterThan(0); |
| 119 | + |
| 120 | + focusables[focusables.length - 1].focus(); |
| 121 | + await user.tab(); |
| 122 | + await waitFor(() => expect(dialog.contains(document.activeElement)).toBe(true)); |
| 123 | + }); |
| 124 | + |
| 125 | + it('closes on Escape and returns focus to the opener', async () => { |
| 126 | + const user = userEvent.setup(); |
| 127 | + render(<Harness renderDialog={renderDialog} />); |
| 128 | + |
| 129 | + const opener = screen.getByTestId('opener'); |
| 130 | + opener.focus(); |
| 131 | + await user.click(opener); |
| 132 | + expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 133 | + |
| 134 | + await user.keyboard('{Escape}'); |
| 135 | + await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); |
| 136 | + await waitFor(() => expect(opener).toHaveFocus()); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +// Silence jsdom "not implemented" noise if any legacy path still reaches alert(). |
| 141 | +vi.stubGlobal('alert', vi.fn()); |
0 commit comments