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( {}} title="X"> body , ) expect(screen.queryByText('body')).toBeNull() }) it('renders content when open=true', () => { render( {}} title="X"> body , ) expect(screen.getByText('body')).toBeInTheDocument() }) it('calls onClose on backdrop click', async () => { const onClose = vi.fn() render( body , ) 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( {}} title="Remove the photo?" description={

It goes for good.

} >
, ) 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( {}} title="X"> body , ) 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( body , ) 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( {}} label="Menu"> , ) 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( , ) 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( , ) 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( , ) 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( , ) 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 (
{openerShown && ( )}
setOpen(false)} title="Confirm">
) } describe('Dialog focus return', () => { it('returns focus to the opener', async () => { render() 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() 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() }) })