Files
faunosteo/apps/app/tests/unit/editor-leave-cascade.test.tsx
T
bchanot 3dc5820e3a feat(editor): edit mode of the references dialog
« Modifier » turns the references dialog into a draft of adds and removes,
saved as one update on the current citations. A new layer, citations,
guards it between the finding and the reference dialogs: close, Back,
POP refusal and the leave cascade ask it. « Modifier la référence » and
« Nouvelle référence » stack the reference dialog over it; a creation it
started joins its draft.
2026-11-15 16:39:43 +01:00

135 lines
4.5 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'
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()
})
})