/** R2F-2: kept-refusal re-show, returnFocus, afterModalsClosed, held Escape, reduced motion. */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { useState } from 'react' import { cleanup, 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 { afterModalsClosed, flushAfterModals, } from '@/components/ui/afterModalsClosed' import { focusRow } from '@/editor/focusRequest' import { useLocaleStore } from '@/state/localeStore' import { setReducedMotion } from '../helpers/motion' beforeEach(() => { useLocaleStore.setState({ locale: 'en' }) setReducedMotion(false) }) afterEach(() => { cleanup() vi.restoreAllMocks() }) const dialogOf = () => screen.getByRole('dialog') as HTMLDialogElement describe('a refused close', () => { it('shows the dialog again when the consumer answers kept', () => { const onClose = vi.fn(() => 'kept' as const) render( , ) const dialog = dialogOf() dialog.close() expect(onClose).toHaveBeenCalledTimes(1) expect(dialog.open).toBe(true) }) it('leaves it closed when the consumer accepts', () => { const onClose = vi.fn() render( , ) const dialog = dialogOf() dialog.close() expect(dialog.open).toBe(false) }) it('never shows a Dialog again once it is set closed and then closed natively', () => { const onClose = vi.fn(() => 'kept' as const) const showModal = vi.spyOn(HTMLDialogElement.prototype, 'showModal') const view = (open: boolean) => ( body ) const { rerender } = render(view(true)) const dialog = dialogOf() expect(showModal).toHaveBeenCalledTimes(1) rerender(view(false)) dialog.close() expect(dialog.open).toBe(false) expect(showModal).toHaveBeenCalledTimes(1) }) }) describe('a held Escape', () => { it('is no close request, and stays cancelled', () => { const onClose = vi.fn() render( , ) const held = new KeyboardEvent('keydown', { key: 'Escape', repeat: true, bubbles: true, cancelable: true, }) screen.getByRole('button', { name: 'Inside' }).dispatchEvent(held) expect(held.defaultPrevented).toBe(true) expect(onClose).not.toHaveBeenCalled() fireEvent.keyDown(dialogOf(), { key: 'Escape' }) expect(onClose).toHaveBeenCalledTimes(1) }) }) function Returning({ returnFocus, }: { returnFocus?: (() => HTMLElement | null) | null }) { const [open, setOpen] = useState(false) return (
setOpen(false)} returnFocus={returnFocus} title="X" > body
) } async function openThenEscape() { await userEvent.click(screen.getByRole('button', { name: 'Open' })) fireEvent.keyDown(screen.getByRole('dialog'), { key: 'Escape' }) await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) } describe('returnFocus', () => { it('a function resolves the target, focused without scrolling', async () => { const focus = vi.spyOn(HTMLElement.prototype, 'focus') render( screen.getByRole('button', { name: 'Elsewhere' })} />, ) await openThenEscape() expect(screen.getByRole('button', { name: 'Elsewhere' })).toHaveFocus() expect(focus).toHaveBeenCalledWith({ preventScroll: true }) }) it('a function that answers null falls back to the opener', async () => { render( null} />) await openThenEscape() expect(screen.getByRole('button', { name: 'Open' })).toHaveFocus() }) it('null leaves the focus to the move that closed the dialog', async () => { render() await openThenEscape() expect(screen.getByRole('button', { name: 'Open' })).not.toHaveFocus() }) }) describe('afterModalsClosed', () => { it('runs at once when no dialog is open', () => { const fn = vi.fn() afterModalsClosed(fn) expect(fn).toHaveBeenCalledTimes(1) }) it('waits for the last dialog, then runs once', async () => { const fn = vi.fn() const { unmount } = render( {}} label="Menu"> , ) afterModalsClosed(fn) flushAfterModals() expect(fn).not.toHaveBeenCalled() unmount() await waitFor(() => expect(fn).toHaveBeenCalledTimes(1)) }) it('a cancelled wait never runs', async () => { const fn = vi.fn() const { unmount } = render( {}} label="Menu"> , ) afterModalsClosed(fn)() unmount() await Promise.resolve() expect(fn).not.toHaveBeenCalled() }) it('runs after the focus went back, so a landing is not undone', async () => { const order: string[] = [] render() await userEvent.click(screen.getByRole('button', { name: 'Open' })) afterModalsClosed(() => order.push(`landed on ${document.activeElement?.textContent}`), ) fireEvent.keyDown(screen.getByRole('dialog'), { key: 'Escape' }) await waitFor(() => expect(order).toHaveLength(1)) expect(order[0]).toBe('landed on Open') }) }) describe('Dialog motion', () => { it('does not scale in when the user asks for reduced motion', () => { setReducedMotion(true) render( {}} title="X"> body , ) const panel = screen.getByRole('heading', { name: 'X' }).closest('div') expect(panel?.getAttribute('style') ?? '').not.toContain('0.96') }) it('scales in otherwise', () => { render( {}} title="X"> body , ) const panel = screen.getByRole('heading', { name: 'X' }).closest('div') expect(panel?.getAttribute('style') ?? '').toContain('0.96') }) }) describe('focusRow', () => { it('reports found only when the row holds the focus', () => { const key = { kind: 'zone', id: 'z' } as const render( , ) expect(focusRow(key)).toBe(false) cleanup() render() expect(focusRow(key)).toBe(true) }) })