feat(editor): a reference made in the citations draft saves with it

This commit is contained in:
bchanot committed 2026-11-15 19:55:41 +01:00
1 parent 8c3fcca0b9
commit 70eb2fcbb4
15 files changed
+671 -162

No files matched your search

@@ -44,6 +44,17 @@ vi.mock('@faunosteo/supabase', async (orig) => ({
submitSheetReview: vi.fn(),
}))
/** Ids handed out before the real generator: a clash at the save (Rev 6). */
const minted = vi.hoisted((): string[] => [])
vi.mock('@/editor/ids', async (orig) => {
const real = await orig<typeof import('@/editor/ids')>()
return {
...real,
newElementId: (prefix: Parameters<typeof real.newElementId>[0]) =>
minted.shift() ?? real.newElementId(prefix),
}
})
let navigate: NavigateFunction
let location: Location
const probe = (n: NavigateFunction, l: Location) => {
@@ -59,6 +70,7 @@ beforeEach(() => {
TAG = resolveSessionTag(USER)
})
afterEach(() => {
minted.length = 0
cleanup()
resetStore()
setWidth(1024)
@@ -197,6 +209,24 @@ async function cite(user: User, id: string) {
await user.click(button('Citer'))
}
/** The reference dialog over the edit mode: type and citation, then « Ajouter la référence ». */
async function createPending(user: User, citation: string) {
await waitFor(() => expect(referenceDialog()).toBeDefined())
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
await user.type(inReference().getByLabelText('Citation'), citation)
await user.click(
inReference().getByRole('button', { name: 'Ajouter la référence' }),
)
await waitFor(() => expect(referenceDialog()).toBeUndefined())
}
const pendingRow = () =>
[...citations()!.querySelectorAll<HTMLElement>('[data-citation-row]')][2]
const status = () =>
citations()!.querySelector('[data-place="citations"] > [role="status"]')!
.textContent
const live = () =>
document.querySelector('p.sr-only[aria-live="polite"]')!.textContent ?? ''
describe('the « Modifier » of the references dialog', () => {
it('edit mode: no history entry, no « Tout afficher », no « Aller à »', async () => {
const user = await mount()
@@ -391,9 +421,6 @@ describe('« Citer » (Rev 6)', () => {
})
})
const live = () =>
document.querySelector('p.sr-only[aria-live="polite"]')!.textContent ?? ''
describe('leaving the edit mode with changes', () => {
it('an explicit « Abandonner » after ✕ closes the dialog without a word', async () => {
const user = await mount()
@@ -509,7 +536,7 @@ describe('the reference dialog over the edit mode', () => {
expect(rows()).toEqual(['ref-a'])
})
it('« Nouvelle référence » creates it and cites it in the draft only', async () => {
it('« Nouvelle référence » cites it in the draft only; the library is unchanged', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
@@ -518,40 +545,163 @@ describe('the reference dialog over the edit mode', () => {
'aria-selected',
'true',
)
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
await user.type(inReference().getByLabelText('Citation'), 'Neuf')
await user.click(
inReference().getByRole('button', { name: 'Ajouter la référence' }),
await createPending(user, 'Neuf')
expect(library()).not.toContain('Neuf')
expect(rows()).toHaveLength(3)
expect(rows().slice(0, 2)).toEqual(['ref-a', 'ref-c'])
const row = pendingRow()
expect(within(row).getByText('Neuf')).toBeVisible()
expect(within(row).getByText('Nouvelle')).toBeVisible()
expect(status()).toBe(
'Nouvelle référence citée dans le brouillon : Neuf. ' +
'Elle sera enregistrée avec les citations.',
)
await waitFor(() => expect(referenceDialog()).toBeUndefined())
expect(library()).toContain('Neuf')
expect(live()).not.toContain('sera enregistrée')
expect(undos()).toBe(0)
})
it('« Enregistrer » writes the reference and the citations as ONE undo step', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
await user.click(button('Enregistrer'))
await waitFor(() => expect(button('Modifier')).toHaveFocus())
const id = doc().references.find(
(r: { citation: string }) => r.citation === 'Neuf',
).id
expect(rows()).toEqual(['ref-a', 'ref-c', id])
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
await user.click(button('Enregistrer'))
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c', id])
expect(undos()).toBe(2)
expect(undos()).toBe(1)
act(() => {
store.getState().undoStep()
})
expect(library()).not.toContain('Neuf')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
act(() => {
store.getState().redoStep()
})
expect(library()).toContain('Neuf')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c', id])
})
it('a reference created, then the draft abandoned, stays in the library uncited', async () => {
it('created, then the draft abandoned: library unchanged, nothing said', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
await user.click(button('Annuler'))
await user.click(button('Abandonner'))
expect(library()).not.toContain('Neuf')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
expect(undos()).toBe(0)
await user.click(button('Fermer'))
await waitFor(() => expect(citations()).toBeUndefined())
await user.click(inFindingDialog().getByRole('button', { name: 'Fermer' }))
await waitFor(() => expect(findingDialog()).toBeNull())
await act(async () => undefined)
expect(live()).not.toContain('sera enregistrée')
})
it('a pending reference is modified, prefilled; nothing is written', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
const pendingId = rows()[2]
await user.click(button(/^Modifier la référence Neuf/))
await waitFor(() => expect(referenceDialog()).toBeDefined())
expect(
inReference().getByRole('heading', { name: 'Nouvelle référence' }),
).toBeVisible()
const citation = inReference().getByLabelText('Citation')
expect(citation).toHaveValue('Neuf')
await user.type(citation, ' 2')
await user.click(inReference().getByRole('button', { name: 'Valider' }))
await waitFor(() => expect(referenceDialog()).toBeUndefined())
expect(rows()).toEqual(['ref-a', 'ref-c', pendingId])
expect(within(pendingRow()).getByText('Neuf 2')).toBeVisible()
expect(library()).not.toContain('Neuf 2')
await user.click(button('Enregistrer'))
expect(library().filter((c) => c.startsWith('Neuf'))).toEqual(['Neuf 2'])
expect(undos()).toBe(1)
})
it('« Retirer » drops a pending reference entirely and says so in the dialog', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
await removeRow(user, /Neuf/)
expect(rows()).toEqual(['ref-a', 'ref-c'])
expect(status()).toBe('Nouvelle référence retirée : Neuf')
await user.click(button('Annuler'))
await waitFor(() => expect(button('Modifier')).toHaveFocus())
expect(library()).not.toContain('Neuf')
expect(undos()).toBe(0)
})
it('a double submit of the creation makes one pending row', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await waitFor(() => expect(referenceDialog()).toBeDefined())
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
await user.type(inReference().getByLabelText('Citation'), 'Neuf')
const form = referenceDialog()!.querySelector('form')!
act(() => {
fireEvent.submit(form)
fireEvent.submit(form)
})
await waitFor(() => expect(referenceDialog()).toBeUndefined())
expect(rows()).toHaveLength(3)
})
it('the creation is dry-run: a refusal shows in its form, nothing cited', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await waitFor(() => expect(referenceDialog()).toBeDefined())
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
await user.type(inReference().getByLabelText('Citation'), 'Neuf')
minted.push('ref-a')
await user.click(
inReference().getByRole('button', { name: 'Ajouter la référence' }),
)
await waitFor(() => expect(referenceDialog()).toBeUndefined())
await user.click(button('Annuler'))
await user.click(button('Abandonner'))
expect(library()).toContain('Neuf')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
expect(inReference().getByRole('alert')).toBeVisible()
expect(referenceDialog()).toBeDefined()
expect(rows()).toEqual(['ref-a', 'ref-c'])
expect(undos()).toBe(0)
})
it('a clashing id at the save is minted again once', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
minted.push('ref-a')
await user.click(button('Enregistrer'))
await waitFor(() => expect(button('Modifier')).toHaveFocus())
const added = doc().references.find(
(r: { citation: string }) => r.citation === 'Neuf',
)
expect(added.id).not.toBe('ref-a')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c', added.id])
expect(undos()).toBe(1)
})
it('a save refused twice keeps the draft and names the pending reference', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
minted.push('ref-a', 'ref-c')
await user.click(button('Enregistrer'))
const alert = inCitations().getByRole('alert')
expect(alert).toHaveTextContent(/Neuf/)
expect(rows()).toHaveLength(3)
expect(library()).not.toContain('Neuf')
expect(undos()).toBe(0)
})
})
describe('a finding draft and a references draft on the same element', () => {