« 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.
173 lines
5.3 KiB
TypeScript
173 lines
5.3 KiB
TypeScript
/** Rev 5 Task 12: the citations layer's guard (Back, refusal, close, order, creation). */
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { act, cleanup, render } from '@testing-library/react'
|
|
import type { ElementKey } from '@faunosteo/core-wasm/types'
|
|
import {
|
|
MemoryRouter,
|
|
useLocation,
|
|
useNavigate,
|
|
type Location,
|
|
type NavigateFunction,
|
|
} from 'react-router-dom'
|
|
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
|
|
import useEditorHistory from '@/editor/history/useEditorHistory'
|
|
import { LAYERS_TOP_DOWN, topDirtyLayer } from '@/editor/leftDraft'
|
|
import { defaultView } from '@/editor/navigation'
|
|
import {
|
|
useEditorNavigation,
|
|
type EditorNavigation,
|
|
type LayerName,
|
|
} from '@/editor/useEditorNavigation'
|
|
|
|
afterEach(cleanup)
|
|
|
|
const owner = { sheetId: 'S', tag: 'T' }
|
|
const node: ElementKey = {
|
|
kind: 'finding',
|
|
zone_id: 'zone',
|
|
path: ['family', 'node'],
|
|
}
|
|
|
|
let nav: EditorNavigation
|
|
let navigate: NavigateFunction
|
|
let location: Location
|
|
const entry = () => (location.state as Record<string, HistoryEntry>)[ENTRY_KEY]
|
|
|
|
function Page() {
|
|
nav = useEditorNavigation('findings')
|
|
navigate = useNavigate()
|
|
location = useLocation()
|
|
useEditorHistory(nav, owner, defaultView('findings'))
|
|
return null
|
|
}
|
|
|
|
function mount() {
|
|
render(
|
|
<MemoryRouter initialEntries={['/x']}>
|
|
<Page />
|
|
</MemoryRouter>,
|
|
)
|
|
}
|
|
|
|
/** A layer whose form holds changes: a user ask refuses, a forced one says the note. */
|
|
const dirty = vi.fn((forced: boolean) =>
|
|
forced ? { allowed: true, note: 'dropped' } : { allowed: false },
|
|
)
|
|
const register = (layer: LayerName) =>
|
|
act(() => nav.guards.register(layer, dirty))
|
|
|
|
/** Node's finding dialog, then its citations dialog: two entries. */
|
|
function cited() {
|
|
mount()
|
|
act(() => nav.openFinding(node))
|
|
act(() => nav.openCitations(node))
|
|
}
|
|
|
|
describe('the layer order', () => {
|
|
it('reference, citations, finding: asked top down', () => {
|
|
expect(LAYERS_TOP_DOWN).toEqual(['reference', 'citations', 'finding'])
|
|
mount()
|
|
const order: string[] = []
|
|
for (const layer of ['finding', 'citations', 'reference'] as const) {
|
|
nav.guards.register(layer, () => {
|
|
order.push(layer)
|
|
return { allowed: true }
|
|
})
|
|
}
|
|
nav.guards.askAll(false)
|
|
expect(order).toEqual(['reference', 'citations', 'finding'])
|
|
})
|
|
|
|
it('topDirtyLayer: citations over finding, reference over both', () => {
|
|
mount()
|
|
register('finding')
|
|
register('citations')
|
|
expect(topDirtyLayer(nav.guards)).toBe('citations')
|
|
expect(topDirtyLayer(nav.guards, 'citations')).toBe('finding')
|
|
register('reference')
|
|
expect(topDirtyLayer(nav.guards)).toBe('reference')
|
|
expect(topDirtyLayer(nav.guards, 'reference')).toBe('citations')
|
|
})
|
|
})
|
|
|
|
describe('a Back over a citations draft', () => {
|
|
it('closing only the citations dialog asks; the view and the entry stay', () => {
|
|
cited()
|
|
register('citations')
|
|
dirty.mockClear()
|
|
act(() => navigate(-1))
|
|
expect(dirty).toHaveBeenCalledWith(false)
|
|
expect(nav.state.finding).toMatchObject({ citations: true })
|
|
expect(entry().view.finding).toMatchObject({ citations: true })
|
|
})
|
|
|
|
it('a clean citations layer lets it close', () => {
|
|
cited()
|
|
act(() => nav.guards.register('citations', () => ({ allowed: true })))
|
|
act(() => navigate(-1))
|
|
expect(nav.state.finding).toMatchObject({ citations: false })
|
|
})
|
|
|
|
it('under a clean reference dialog: the reference closes, the draft asks', () => {
|
|
cited()
|
|
act(() => nav.references.open('ref-a'))
|
|
register('citations')
|
|
dirty.mockClear()
|
|
act(() => navigate(-2))
|
|
expect(dirty).toHaveBeenCalledWith(false)
|
|
expect(nav.state.reference).toBeNull()
|
|
expect(nav.state.finding).toMatchObject({ citations: true })
|
|
expect(entry().view).toMatchObject({
|
|
reference: null,
|
|
finding: { citations: true },
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('closeCitations', () => {
|
|
it('a user close is refused by a draft; a forced one goes', () => {
|
|
cited()
|
|
register('citations')
|
|
let closed = true
|
|
act(() => {
|
|
closed = nav.closeCitations()
|
|
})
|
|
expect(closed).toBe(false)
|
|
expect(nav.state.finding).toMatchObject({ citations: true })
|
|
act(() => {
|
|
closed = nav.closeCitations(true)
|
|
})
|
|
expect(closed).toBe(true)
|
|
expect(nav.state.finding).toMatchObject({ citations: false })
|
|
})
|
|
})
|
|
|
|
describe('a creation started over the citations dialog', () => {
|
|
it('hands its id to the form that started it; no « added » notice', async () => {
|
|
cited()
|
|
const cite = vi.fn()
|
|
act(() => nav.references.start(null, cite))
|
|
expect(nav.state).toMatchObject({
|
|
tab: 'findings',
|
|
reference: { create: true },
|
|
})
|
|
await act(async () => nav.references.created('ref-new'))
|
|
expect(cite).toHaveBeenCalledWith('ref-new')
|
|
expect(nav.references.added).toBeNull()
|
|
expect(nav.state.reference).toBeNull()
|
|
expect(nav.state.finding).toMatchObject({ citations: true })
|
|
})
|
|
|
|
it('a later creation from the library sets the notice, cites nothing', async () => {
|
|
cited()
|
|
const cite = vi.fn()
|
|
act(() => nav.references.start(null, cite))
|
|
act(() => nav.references.close(true))
|
|
act(() => nav.selectTab('references'))
|
|
act(() => nav.references.start(null))
|
|
await act(async () => nav.references.created('ref-lib'))
|
|
expect(cite).not.toHaveBeenCalled()
|
|
expect(nav.references.added).toMatchObject({ id: 'ref-lib' })
|
|
})
|
|
})
|