Files
faunosteo/apps/app/tests/unit/ui.Dialog.test.tsx
T
bchanot 9a0fe3784c fix(editor): veil ignores clicks, names merged apart, honest tests
U2 Task 6 fix round 1 (review minors 1 to 9).

- R-6: `Dialog` takes `veilCloses`; the annotation dialog passes false,
  so its veil ignores clicks centred too. Other dialogs unchanged.
- Save merges each name on its own (an EN refreshed meanwhile survives
  an FR-only edit); the merged annotation is typed per kind, no cast.
- Tests: the category change and the region row really run (a second
  category and a region in the fixture); EN kept under an FR edit;
  focus to the drawing when a filter hides the row; a centred veil
  ignores a click; act() around the store write; ui.Dialog restores
  innerHeight and visualViewport after each test.
- e2e: `pickAnnotation` no longer scrolls; `scrollToTop` is explicit
  where a low row leaves the drawing out of view (editor-zones x3,
  editor-geometry). editor-geometry runs the accepted repair from the
  page's end, buttons pressed in place, scroll unchanged. The touch
  and double-click tests wait on the clicks themselves; the stray
  click on the veil can fail.
- CreationPanel's `required.fields` recorded as a shortcut for Task 7;
  TODO lines updated. focusTarget comment wrapped.
2026-11-17 22:47:02 +01:00

351 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, 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('its ✕ is a 44 px box with a visible focus ring; a long title wraps beside it (Rev 6)', () => {
useLocaleStore.setState({ locale: 'fr' })
render(
<Dialog open onClose={() => {}} title="X">
body
</Dialog>,
)
const close = screen.getByRole('button', { name: 'Fermer' })
for (const name of [
'grid',
'h-11',
'w-11',
'shrink-0',
'place-items-center',
'rounded-lg',
'focus-visible:ring-2',
]) {
expect(close).toHaveClass(name)
}
expect(screen.getByRole('heading', { name: 'X' })).toHaveClass(
'min-w-0',
'break-words',
)
})
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()
})
})
/** U2 Task 6 (A2, R-6): the panel over an anchor box, the keyboard fallback, the veil. */
describe('Dialog anchored over a column', () => {
const rect = (box: Partial<DOMRect>) =>
({ left: 0, top: 0, width: 0, height: 0, ...box }) as DOMRect
function anchorAt(box: Partial<DOMRect>) {
const element = document.createElement('aside')
element.getBoundingClientRect = () => rect(box)
document.body.append(element)
return { current: element }
}
const panel = () => screen.getByTestId('dialog-panel')
const withViewport = (value: unknown) =>
Object.defineProperty(window, 'visualViewport', {
configurable: true,
value,
})
const height = window.innerHeight
const viewport = Object.getOwnPropertyDescriptor(window, 'visualViewport')
afterEach(() => {
window.innerHeight = height
if (viewport) Object.defineProperty(window, 'visualViewport', viewport)
else withViewport(undefined)
})
it('sits at the anchor’s left and width, from its top, down to 12 px above the bottom', () => {
withViewport(undefined)
window.innerHeight = 800
const anchor = anchorAt({ left: 900, top: 140, width: 352 })
render(
<Dialog open anchor={anchor} onClose={() => {}} title="X">
body
</Dialog>,
)
expect(panel().style.left).toBe('900px')
expect(panel().style.width).toBe('352px')
expect(panel().style.top).toBe('140px')
expect(panel().style.maxHeight).toBe('648px')
})
it('never starts above 12 px when the column’s top is scrolled away', () => {
withViewport(undefined)
window.innerHeight = 800
const anchor = anchorAt({ left: 900, top: -300, width: 352 })
render(
<Dialog open anchor={anchor} onClose={() => {}} title="X">
body
</Dialog>,
)
expect(panel().style.top).toBe('12px')
expect(panel().style.maxHeight).toBe('776px')
})
it('a tablet keyboard leaving under 320 px: the visual viewport’s top and height', () => {
window.innerHeight = 800
withViewport({
height: 350,
offsetTop: 0,
addEventListener: () => undefined,
removeEventListener: () => undefined,
})
const anchor = anchorAt({ left: 900, top: 140, width: 352 })
render(
<Dialog open anchor={anchor} onClose={() => {}} title="X">
body
</Dialog>,
)
expect(panel().style.top).toBe('12px')
expect(panel().style.height).toBe('326px')
expect(panel().style.left).toBe('900px')
})
it('without an anchor box the panel is centred, as before', () => {
render(
<Dialog open anchor={{ current: null }} onClose={() => {}} title="X">
body
</Dialog>,
)
expect(panel().style.left).toBe('')
expect(panel().className).toContain('max-w-md')
})
it('the veil of an anchored dialog ignores clicks; a centred one still asks', async () => {
withViewport(undefined)
const onClose = vi.fn()
const anchor = anchorAt({ left: 900, top: 140, width: 352 })
const { unmount } = render(
<Dialog open anchor={anchor} onClose={onClose} title="X">
body
</Dialog>,
)
await userEvent.click(screen.getByTestId('dialog-backdrop'))
expect(onClose).not.toHaveBeenCalled()
unmount()
render(
<Dialog open onClose={onClose} title="X">
body
</Dialog>,
)
await userEvent.click(screen.getByTestId('dialog-backdrop'))
expect(onClose).toHaveBeenCalled()
})
it('a centred dialog whose veil must not close (R-6) ignores its clicks too', async () => {
const onClose = vi.fn()
render(
<Dialog open veilCloses={false} onClose={onClose} title="X">
body
</Dialog>,
)
await userEvent.click(screen.getByTestId('dialog-backdrop'))
expect(onClose).not.toHaveBeenCalled()
})
})