Re-show a natively closed dialog only when its consumer answered 'kept'; returnFocus; ignore a held Escape; Dialog honours reduced motion; useBeforeUnload extracted; focusRow reports found only when the row holds focus.
239 lines
6.9 KiB
TypeScript
239 lines
6.9 KiB
TypeScript
/** 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(
|
|
<ModalDialog onClose={onClose} label="Form">
|
|
<button type="button">Inside</button>
|
|
</ModalDialog>,
|
|
)
|
|
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(
|
|
<ModalDialog onClose={onClose} label="Form">
|
|
<button type="button">Inside</button>
|
|
</ModalDialog>,
|
|
)
|
|
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) => (
|
|
<Dialog open={open} onClose={onClose} title="X">
|
|
body
|
|
</Dialog>
|
|
)
|
|
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(
|
|
<ModalDialog onClose={onClose} label="Menu">
|
|
<button type="button">Inside</button>
|
|
</ModalDialog>,
|
|
)
|
|
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 (
|
|
<main>
|
|
<button type="button" onClick={() => setOpen(true)}>
|
|
Open
|
|
</button>
|
|
<button type="button">Elsewhere</button>
|
|
<Dialog
|
|
open={open}
|
|
onClose={() => setOpen(false)}
|
|
returnFocus={returnFocus}
|
|
title="X"
|
|
>
|
|
body
|
|
</Dialog>
|
|
</main>
|
|
)
|
|
}
|
|
|
|
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(
|
|
<Returning
|
|
returnFocus={() => 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(<Returning returnFocus={() => null} />)
|
|
await openThenEscape()
|
|
expect(screen.getByRole('button', { name: 'Open' })).toHaveFocus()
|
|
})
|
|
|
|
it('null leaves the focus to the move that closed the dialog', async () => {
|
|
render(<Returning returnFocus={null} />)
|
|
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(
|
|
<ModalDialog onClose={() => {}} label="Menu">
|
|
<button type="button">Inside</button>
|
|
</ModalDialog>,
|
|
)
|
|
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(
|
|
<ModalDialog onClose={() => {}} label="Menu">
|
|
<button type="button">Inside</button>
|
|
</ModalDialog>,
|
|
)
|
|
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(<Returning />)
|
|
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(
|
|
<Dialog open onClose={() => {}} title="X">
|
|
body
|
|
</Dialog>,
|
|
)
|
|
const panel = screen.getByRole('heading', { name: 'X' }).closest('div')
|
|
expect(panel?.getAttribute('style') ?? '').not.toContain('0.96')
|
|
})
|
|
|
|
it('scales in otherwise', () => {
|
|
render(
|
|
<Dialog open onClose={() => {}} title="X">
|
|
body
|
|
</Dialog>,
|
|
)
|
|
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(
|
|
<button data-focus-key={JSON.stringify(key)} disabled>
|
|
row
|
|
</button>,
|
|
)
|
|
expect(focusRow(key)).toBe(false)
|
|
cleanup()
|
|
render(<button data-focus-key={JSON.stringify(key)}>row</button>)
|
|
expect(focusRow(key)).toBe(true)
|
|
})
|
|
})
|