ModalDialog wraps showModal(): the page behind is inert and does not scroll, focus starts on the first control and returns to the opener, or to the first control of its nearest container when the opener is gone. Escape and close requests only ask the consumer. Dialog gains aria-labelledby and an optional described message; the confirmations pass theirs. The mobile drawer uses the same primitive and closes at the sidebar breakpoint.
212 lines
6.7 KiB
TypeScript
212 lines
6.7 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest'
|
||
import { useState } from 'react'
|
||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||
import userEvent from '@testing-library/user-event'
|
||
import Dialog from '@/components/ui/Dialog'
|
||
import ModalDialog from '@/components/ui/ModalDialog'
|
||
import { useLocaleStore } from '@/state/localeStore'
|
||
|
||
// happy-dom implements neither the top layer nor inertness nor the dialog's focus
|
||
// steps: these tests cover the React wiring, the browser behaviour is proven by
|
||
// the Playwright suites (apps/app/e2e).
|
||
|
||
describe('Dialog', () => {
|
||
it('renders nothing when open=false', () => {
|
||
render(
|
||
<Dialog open={false} onClose={() => {}} title="X">
|
||
body
|
||
</Dialog>,
|
||
)
|
||
expect(screen.queryByText('body')).toBeNull()
|
||
})
|
||
|
||
it('renders content when open=true', () => {
|
||
render(
|
||
<Dialog open={true} onClose={() => {}} title="X">
|
||
body
|
||
</Dialog>,
|
||
)
|
||
expect(screen.getByText('body')).toBeInTheDocument()
|
||
})
|
||
|
||
it('calls onClose on backdrop click', async () => {
|
||
const onClose = vi.fn()
|
||
render(
|
||
<Dialog open onClose={onClose} title="X">
|
||
body
|
||
</Dialog>,
|
||
)
|
||
await userEvent.click(screen.getByTestId('dialog-backdrop'))
|
||
expect(onClose).toHaveBeenCalled()
|
||
})
|
||
|
||
it('is a native dialog named by its title and described by its message', () => {
|
||
render(
|
||
<Dialog
|
||
open
|
||
onClose={() => {}}
|
||
title="Remove the photo?"
|
||
description={<p>It goes for good.</p>}
|
||
>
|
||
<button type="button">Remove</button>
|
||
</Dialog>,
|
||
)
|
||
const dialog = screen.getByRole('dialog', { name: 'Remove the photo?' })
|
||
expect(dialog.tagName).toBe('DIALOG')
|
||
expect(dialog).toHaveAccessibleDescription('It goes for good.')
|
||
})
|
||
|
||
it('has no description when none is given', () => {
|
||
render(
|
||
<Dialog open onClose={() => {}} title="X">
|
||
body
|
||
</Dialog>,
|
||
)
|
||
expect(screen.getByRole('dialog')).not.toHaveAttribute('aria-describedby')
|
||
})
|
||
|
||
it.each([
|
||
['fr', 'Fermer'],
|
||
['en', 'Close'],
|
||
] as const)(
|
||
'asks to close from the close button, named in %s',
|
||
async (locale, name) => {
|
||
useLocaleStore.setState({ locale })
|
||
const onClose = vi.fn()
|
||
render(
|
||
<Dialog open onClose={onClose} title="X">
|
||
body
|
||
</Dialog>,
|
||
)
|
||
await userEvent.click(screen.getByRole('button', { name }))
|
||
expect(onClose).toHaveBeenCalledTimes(1)
|
||
},
|
||
)
|
||
})
|
||
|
||
describe('ModalDialog', () => {
|
||
it('opens as a modal while mounted and closes on unmount', () => {
|
||
const showModal = vi.spyOn(HTMLDialogElement.prototype, 'showModal')
|
||
const close = vi.spyOn(HTMLDialogElement.prototype, 'close')
|
||
const { unmount } = render(
|
||
<ModalDialog onClose={() => {}} label="Menu">
|
||
<button type="button">Inside</button>
|
||
</ModalDialog>,
|
||
)
|
||
expect(showModal).toHaveBeenCalledTimes(1)
|
||
unmount()
|
||
expect(close).toHaveBeenCalledTimes(1)
|
||
showModal.mockRestore()
|
||
close.mockRestore()
|
||
})
|
||
|
||
it('turns Escape into a request: the consumer decides, the dialog stays open', () => {
|
||
const onClose = vi.fn()
|
||
render(
|
||
<ModalDialog onClose={onClose} label="Menu">
|
||
<button type="button">Inside</button>
|
||
</ModalDialog>,
|
||
)
|
||
const dialog = screen.getByRole('dialog', {
|
||
name: 'Menu',
|
||
}) as HTMLDialogElement
|
||
const escape = new KeyboardEvent('keydown', {
|
||
key: 'Escape',
|
||
bubbles: true,
|
||
cancelable: true,
|
||
})
|
||
screen.getByRole('button', { name: 'Inside' }).dispatchEvent(escape)
|
||
fireEvent.keyDown(dialog, { key: 'Escape' })
|
||
expect(escape.defaultPrevented).toBe(true)
|
||
expect(onClose).toHaveBeenCalledTimes(2)
|
||
expect(dialog.open).toBe(true)
|
||
})
|
||
|
||
it('ignores Escape while a composition is in progress', () => {
|
||
const onClose = vi.fn()
|
||
render(
|
||
<ModalDialog onClose={onClose} label="Menu">
|
||
<input aria-label="Name" />
|
||
</ModalDialog>,
|
||
)
|
||
fireEvent.keyDown(screen.getByLabelText('Name'), {
|
||
key: 'Escape',
|
||
isComposing: true,
|
||
})
|
||
expect(onClose).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('cancels the platform close request and asks the consumer instead', () => {
|
||
const onClose = vi.fn()
|
||
render(
|
||
<ModalDialog onClose={onClose} label="Menu">
|
||
<button type="button">Inside</button>
|
||
</ModalDialog>,
|
||
)
|
||
const cancel = new Event('cancel', { cancelable: true })
|
||
screen.getByRole('dialog').dispatchEvent(cancel)
|
||
expect(cancel.defaultPrevented).toBe(true)
|
||
expect(onClose).toHaveBeenCalledTimes(1)
|
||
})
|
||
|
||
it('follows a close the platform forced, and ignores a close event while still open', () => {
|
||
const onClose = vi.fn()
|
||
render(
|
||
<ModalDialog onClose={onClose} label="Menu">
|
||
<button type="button">Inside</button>
|
||
</ModalDialog>,
|
||
)
|
||
const dialog = screen.getByRole('dialog') as HTMLDialogElement
|
||
dialog.dispatchEvent(new Event('close'))
|
||
expect(onClose).not.toHaveBeenCalled()
|
||
dialog.close()
|
||
expect(onClose).toHaveBeenCalledTimes(1)
|
||
})
|
||
})
|
||
|
||
/** An opener that can disappear while its dialog is open, beside a sibling control. */
|
||
function Harness({ removeOpener = false }: { removeOpener?: boolean }) {
|
||
const [open, setOpen] = useState(false)
|
||
const [openerShown, setOpenerShown] = useState(true)
|
||
const confirm = () => {
|
||
if (removeOpener) setOpenerShown(false)
|
||
setOpen(false)
|
||
}
|
||
return (
|
||
<main>
|
||
<section>
|
||
<button type="button">Sibling</button>
|
||
{openerShown && (
|
||
<button type="button" onClick={() => setOpen(true)}>
|
||
Open
|
||
</button>
|
||
)}
|
||
</section>
|
||
<Dialog open={open} onClose={() => setOpen(false)} title="Confirm">
|
||
<button type="button" onClick={confirm}>
|
||
Confirm
|
||
</button>
|
||
</Dialog>
|
||
</main>
|
||
)
|
||
}
|
||
|
||
describe('Dialog focus return', () => {
|
||
it('returns focus to the opener', async () => {
|
||
render(<Harness />)
|
||
await userEvent.click(screen.getByRole('button', { name: 'Open' }))
|
||
fireEvent.keyDown(screen.getByRole('dialog'), { key: 'Escape' })
|
||
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
||
expect(screen.getByRole('button', { name: 'Open' })).toHaveFocus()
|
||
})
|
||
|
||
it('falls back to the first control of the opener’s container when the opener is gone', async () => {
|
||
render(<Harness removeOpener />)
|
||
await userEvent.click(screen.getByRole('button', { name: 'Open' }))
|
||
await userEvent.click(screen.getByRole('button', { name: 'Confirm' }))
|
||
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
||
expect(screen.queryByRole('button', { name: 'Open' })).toBeNull()
|
||
expect(screen.getByRole('button', { name: 'Sibling' })).toHaveFocus()
|
||
})
|
||
})
|