/** 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 ? : null } function Asking(props: { guards: Guards layer: LayerName close: (open: boolean) => void }) { const guard = useDirtyGuard(props.guards, true, props.layer) return (
{guard.confirming && ( props.close(false))} /> )}
) } function Editor() { const nav = useEditorNavigation('findings') useLeaveConfirm(nav) return ( <> ) } 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( , ) }) 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( , ) 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( , ) 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( , ) 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 }