fix(editor): Task 6 round 1, draft store tests, dead rescue

This commit is contained in:
bchanot committed 2026-11-14 14:15:11 +01:00
1 parent f0e49fd447
commit 270444e2ec
4 files changed
+66 -4

No files matched your search

@@ -200,7 +200,8 @@ function useEditForm(props: FindingEditFormProps) {
/**
* The element edited in the finding dialog (R2F-3): a local draft, one `update` on
* « Enregistrer », citations included (none when nothing changed), nothing on « Annuler » or a close. When the
* « Enregistrer », citations included (none when nothing changed), nothing on « Annuler »
* or a close. When the
* editor turns read-only the draft stays, its fields read-only, and only « Abandonner »
* is left.
*/
@@ -177,7 +177,8 @@ function Entries({ panel, props }: { panel: Panel; props: Props }) {
/**
* The citations of one finding: the references it cites as tiles, « Tout afficher » for
* the whole library. No search, add, remove or order (P7): citing is the edit form's. The content of the side sheet
* the whole library. No search, add, remove or order (P7): citing is the edit form's.
* The content of the side sheet
* (CitationsSheet), which is the modal; opens from a finding's trigger only.
*/
export default function CitationsPanel(props: Props) {
@@ -12,6 +12,9 @@ interface CitationDraftStore {
publish: (draft: CitationDraft | null) => void
}
// shortcut: module-level store because the citations sheet is a sibling of the finding
// dialog, so no React context reaches both. It holds one draft at a time (one edit form
// is open at once). Upgrade path: a provider above the dialog and the sheet.
export const useCitationDraft = create<CitationDraftStore>((set) => ({
draft: null,
publish: (draft) => set({ draft }),
@@ -11,6 +11,7 @@ import {
import userEvent from '@testing-library/user-event'
import type { Location, NavigateFunction } from 'react-router-dom'
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
import { useCitationDraft } from '@/editor/findings/citationDraft'
import { useLocaleStore } from '@/state/localeStore'
import { edit, insert } from '../helpers/editorFixtures'
import { defaultView } from '@/editor/navigation'
@@ -563,7 +564,7 @@ const reference = (id: string, citation: string) => ({
})
/** Node A cites Alpha; Beta is cited by nobody. */
function citingText(): string {
function citingText(ids = ['ref-a']): string {
let text = insert(sheetText(), reference('ref-a', 'Alpha'))
text = insert(text, reference('ref-b', 'Beta'))
const key = {
@@ -574,7 +575,7 @@ function citingText(): string {
const node = JSON.parse(text).finding_zones[0].categories[0].children[0]
const element = {
kind: 'finding_node',
value: { ...node, reference_ids: ['ref-a'] },
value: { ...node, reference_ids: ids },
}
return edit(text, { op: 'update', key, element } as never)
}
@@ -686,4 +687,60 @@ describe('citing references lives in the edit form (owner item 1)', () => {
expect(sheet.queryByRole('button', { name: /^Retirer/ })).toBeNull()
expect(nodeIds()).toEqual(['ref-a'])
})
it('the published draft returns to null on « Abandonner » and on save', async () => {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
await modify(user)
await cite(user, 'Beta')
expect(useCitationDraft.getState().draft?.ids).toEqual(['ref-a', 'ref-b'])
await user.click(inDialog().getByRole('button', { name: 'Annuler' }))
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
expect(useCitationDraft.getState().draft).toBeNull()
await modify(user)
await cite(user, 'Beta')
await save(user)
expect(useCitationDraft.getState().draft).toBeNull()
})
it('a Retirer-only change counts and lists the draft, the document stays', async () => {
const user = await mount(1280, citingText(['ref-a', 'ref-b']))
await openFinding(user, /Node A/)
await modify(user)
await user.click(inDialog().getByRole('button', { name: 'Retirer Alpha' }))
await user.click(inDialog().getByRole('button', { name: '1 référence' }))
await waitFor(() => expect(panelOpen()).not.toBeNull())
const sheet = within(panelOpen()!)
expect(sheet.getByText(/1 référence citée/)).toBeVisible()
expect(sheet.getByText(/Beta/)).toBeVisible()
expect(sheet.queryByText(/Alpha/)).toBeNull()
expect(nodeIds()).toEqual(['ref-a', 'ref-b'])
})
it('a read-only flip keeps a citation-only draft, never dropped silently', async () => {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
await modify(user)
await cite(user, 'Beta')
act(() => {
setWidth(900)
window.dispatchEvent(new Event('resize'))
})
expect(findingDialog()).not.toBeNull()
expect(
inDialog().getByRole('button', { name: '2 références' }),
).toBeVisible()
expect(inDialog().queryByLabelText('Citer une référence')).toBeNull()
expect(
inDialog().getByRole('button', { name: 'Enregistrer' }),
).toBeDisabled()
expect(nodeIds()).toEqual(['ref-a'])
act(() => {
setWidth(1280)
window.dispatchEvent(new Event('resize'))
})
expect(
inDialog().getByRole('button', { name: '2 références' }),
).toBeVisible()
})
})