With a started move, sign-out and an in-app link now ask « Abandonner le déplacement ? » (R-12) through the page's own question, registered by the draft route in leaveGuardStore (`ask`; the shell imports no editor module). The generic leave and sign-out dialogs cover only sheet-level unsaved work; with both pending the drawing asks first, then the sheet question, as useLeaveConfirm orders them. A move with nothing started arms nothing (it ends); a started one still guards a reload. Tests: sign-out and link with a started move (alone and with sheet changes), the history jump with only a move and after dirty layers, a section entry and « Tout afficher ».
193 lines
6.3 KiB
TypeScript
193 lines
6.3 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 {
|
|
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('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
|
|
}
|