Files
faunosteo/apps/app/tests/unit/editor-leave-cascade.test.tsx
T
bchanot fe333c2bbd fix(editor): creation question lapses, guard sites and flip tested
The creation's « Abandonner la création ? » no longer outlives it:
« Replacer le point », « Reprendre le tracé », « Créer » (« Utiliser
cette position » included) and « Annuler le tracé » clear a question
still open, which would otherwise reappear over the page. Locals
trimmed in the creation dialog and the tracing actions (5 max).

Tests added (no assertion removed): the two lapses (RED without the
fix, GREEN with it); a view chip and a tool ask during a tracing; the
mode flip keeps the dialog, its form and the ghost; a history jump out
of the editor waits for the creation's question; e2e: « Reprendre le
tracé » and « Revenir aux propriétés » under expectNoShift at 1 024,
1 180 and 1 280 px, the strip's last command inside it, axe on the
resumed state.
2026-11-17 23:41:24 +01:00

220 lines
7.1 KiB
TypeScript

/** Criterion 23: a history jump out of the editor asks every dirty layer, top down. */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { useState } from 'react'
import { MemoryRouter } from 'react-router-dom'
import DiscardConfirm from '@/components/editor/findings/DiscardConfirm'
import { useDirtyGuard } from '@/components/editor/findings/useDirtyGuard'
import useLeaveConfirm from '@/editor/history/useLeaveConfirm'
import {
useEditorNavigation,
type Guards,
type LayerName,
} from '@/editor/useEditorNavigation'
import { useLocaleStore } from '@/state/localeStore'
import { answerDrawing } from '@/editor/annotations/drawingGuard'
import { emptyForm } from '@/editor/annotations/creation'
import { traceVertex } from '@/editor/annotations/creationWork'
import {
resetWork,
setModePreview,
startMove,
useAnnotationWork,
} from '@/editor/annotations/workStore'
const path = '/app/admin/sheets/S/draft'
/** A dirty layer: its question and buttons once asked, gone once closed. */
function Layer({ guards, layer }: { guards: Guards; layer: LayerName }) {
const [open, setOpen] = useState(true)
return open ? <Asking guards={guards} layer={layer} close={setOpen} /> : null
}
function Asking(props: {
guards: Guards
layer: LayerName
close: (open: boolean) => void
}) {
const guard = useDirtyGuard(props.guards, true, props.layer)
return (
<section data-testid={props.layer}>
{guard.confirming && (
<DiscardConfirm
onKeepTyping={guard.keepTyping}
onDiscard={guard.discard(() => props.close(false))}
/>
)}
</section>
)
}
function Editor() {
const nav = useEditorNavigation('findings')
useLeaveConfirm(nav)
return (
<>
<Layer guards={nav.guards} layer="finding" />
<Layer guards={nav.guards} layer="citations" />
<Layer guards={nav.guards} layer="reference" />
</>
)
}
class NavigationStub extends EventTarget {
traverseTo = vi.fn((key: string) => {
void key
return { committed: Promise.resolve(), finished: Promise.resolve() }
})
}
let api: NavigationStub
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
api = new NavigationStub()
Object.defineProperty(window, 'navigation', {
value: api,
configurable: true,
})
render(
<MemoryRouter initialEntries={[path]}>
<Editor />
</MemoryRouter>,
)
})
afterEach(() => {
cleanup()
Reflect.deleteProperty(window, 'navigation')
})
function jump() {
const url = new URL('/app/admin/sheets', window.location.origin).href
const event = Object.assign(new Event('navigate', { cancelable: true }), {
navigationType: 'traverse',
destination: { url, key: 'list', sameDocument: true },
})
act(() => {
api.dispatchEvent(event)
})
return event
}
const layer = (name: LayerName) => screen.queryByTestId(name)
const button = (name: LayerName, label: string) =>
within(layer(name)!).getByRole('button', { name: label })
describe('reference, citations and finding dialogs, all dirty', () => {
it('asks the reference, the citations, then the finding, then the jump goes', async () => {
const user = userEvent.setup()
expect(jump().defaultPrevented).toBe(true)
expect(within(layer('finding')!).queryByRole('group')).toBeNull()
expect(within(layer('citations')!).queryByRole('group')).toBeNull()
await user.click(button('reference', 'Abandonner'))
expect(layer('reference')).toBeNull()
expect(within(layer('finding')!).queryByRole('group')).toBeNull()
await user.click(button('citations', 'Abandonner'))
expect(layer('citations')).toBeNull()
expect(api.traverseTo).not.toHaveBeenCalled()
await user.click(button('finding', 'Abandonner'))
expect(api.traverseTo).toHaveBeenCalledTimes(1)
expect(api.traverseTo).toHaveBeenCalledWith('list')
})
it('« Continuer la saisie » on the last keeps the finding, no jump', async () => {
const user = userEvent.setup()
jump()
await user.click(button('reference', 'Abandonner'))
await user.click(button('citations', 'Abandonner'))
await user.click(button('finding', 'Continuer la saisie'))
expect(layer('finding')).not.toBeNull()
expect(within(layer('finding')!).queryByRole('group')).toBeNull()
expect(api.traverseTo).not.toHaveBeenCalled()
})
it('« Continuer la saisie » on the first question keeps every draft', async () => {
const user = userEvent.setup()
jump()
await user.click(button('reference', 'Continuer la saisie'))
expect(layer('reference')).not.toBeNull()
expect(layer('citations')).not.toBeNull()
expect(layer('finding')).not.toBeNull()
expect(api.traverseTo).not.toHaveBeenCalled()
})
})
describe('the drawing guard on a history jump out of the editor (R-12)', () => {
const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 }
afterEach(resetWork)
it('with only a started move: the jump waits for its question, then goes', async () => {
cleanup()
render(
<MemoryRouter initialEntries={[path]}>
<DrawingOnly />
</MemoryRouter>,
)
startMove('a', 'doc')
setModePreview({ ...STORED, cx: 9 }, STORED)
expect(jump().defaultPrevented).toBe(true)
expect(useAnnotationWork.getState().question?.work).toBe('move')
expect(api.traverseTo).not.toHaveBeenCalled()
act(() => answerDrawing(true))
expect(api.traverseTo).toHaveBeenCalledWith('list')
})
it('during a creation (A12): the jump waits for « Abandonner la création ? », then goes', () => {
cleanup()
render(
<MemoryRouter initialEntries={[path]}>
<DrawingOnly />
</MemoryRouter>,
)
traceVertex(
{
viewId: 'v',
frame: null,
origin: 'tool',
form: emptyForm({ categories: [] } as never),
},
'zone',
[1, 1],
)
expect(jump().defaultPrevented).toBe(true)
expect(useAnnotationWork.getState().question?.work).toBe('creation')
expect(api.traverseTo).not.toHaveBeenCalled()
act(() => answerDrawing(true))
expect(useAnnotationWork.getState().creation).toBeNull()
expect(api.traverseTo).toHaveBeenCalledWith('list')
})
it('with nothing started: the jump is not held and the mode ends', () => {
cleanup()
render(
<MemoryRouter initialEntries={[path]}>
<DrawingOnly />
</MemoryRouter>,
)
startMove('a', 'doc')
expect(jump().defaultPrevented).toBe(false)
})
it('dirty layers ask first, then the drawing, then the jump goes', async () => {
const user = userEvent.setup()
startMove('a', 'doc')
setModePreview({ ...STORED, cx: 9 }, STORED)
jump()
await user.click(button('reference', 'Abandonner'))
await user.click(button('citations', 'Abandonner'))
await user.click(button('finding', 'Abandonner'))
expect(api.traverseTo).not.toHaveBeenCalled()
expect(useAnnotationWork.getState().question?.work).toBe('move')
act(() => answerDrawing(true))
expect(api.traverseTo).toHaveBeenCalledWith('list')
})
})
function DrawingOnly() {
useLeaveConfirm(useEditorNavigation('findings'))
return null
}