/** 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)[ENTRY_KEY] function Page() { nav = useEditorNavigation('findings') navigate = useNavigate() location = useLocation() useEditorHistory(nav, owner, defaultView('findings')) return null } function mount() { render( , ) } /** 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', 'annotation', ]) 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 }) }) it('a forced close of a draft says it was dropped', async () => { cited() register('citations') act(() => { nav.closeCitations(true) }) await act(async () => undefined) expect(nav.live).toContain('dropped') }) }) describe('a creation started over the citations dialog (Rev 6: a draft creation)', () => { const pending = { id: 'ref-p', type: 'book', citation: 'Neuf', doi: null, url: null, } as const it('holds its citing form and the pending value; created() never cites', async () => { cited() const cite = vi.fn() act(() => nav.references.start(null, cite, pending)) expect(nav.state).toMatchObject({ tab: 'findings', reference: { create: true }, }) expect(nav.references.drafting()).toEqual({ initial: pending, cite }) act(() => nav.references.close(true)) expect(nav.references.drafting()).toBeNull() expect(nav.state.reference).toBeNull() expect(nav.state.finding).toMatchObject({ citations: true }) expect(cite).not.toHaveBeenCalled() expect(nav.references.added).toBeNull() }) it('a new one has no initial value', () => { cited() const cite = vi.fn() act(() => nav.references.start(null, cite)) expect(nav.references.drafting()).toEqual({ initial: null, cite }) }) it('a creation replaced forgets its citing form', () => { cited() act(() => nav.references.start(null, vi.fn(), pending)) act(() => nav.references.replace(null)) expect(nav.references.drafting()).toBeNull() }) it('a Back that removes the creation dialog forgets its citing form', () => { cited() act(() => nav.references.start(null, vi.fn(), pending)) act(() => navigate(-1)) expect(nav.state.reference).toBeNull() expect(nav.references.drafting()).toBeNull() }) it('a creation from the library is no draft; created() sets the notice', async () => { cited() act(() => nav.references.start(null, vi.fn())) act(() => nav.references.close(true)) act(() => nav.selectTab('references')) act(() => nav.references.start(null)) expect(nav.references.drafting()).toBeNull() await act(async () => nav.references.created('ref-lib')) expect(nav.references.added).toMatchObject({ id: 'ref-lib' }) }) })