diff --git a/apps/app/src/components/editor/references/CitationsDialog.tsx b/apps/app/src/components/editor/references/CitationsDialog.tsx index 75147b57..c7d0d0ea 100644 --- a/apps/app/src/components/editor/references/CitationsDialog.tsx +++ b/apps/app/src/components/editor/references/CitationsDialog.tsx @@ -17,8 +17,11 @@ import { CITATIONS_TITLE_ID } from './useCitationsPanel' export interface CitationsDialogProps { /** The family or finding node whose citations it shows; null while it is closed. */ citedBy: ElementKey | null - /** A close: a user one (✕, Escape, the backdrop) is refused by a draft; forced goes. */ - onClose: (forced?: boolean) => boolean + /** + * A close: a user one (✕, Escape, the backdrop) is refused by a draft; forced goes; + * `discarded` after the user's « Abandonner » (nothing said). + */ + onClose: (forced?: boolean, discarded?: boolean) => boolean /** « Tout afficher dans l'onglet Références ». */ onShowAll: () => void /** « Aller à » a citing finding or a reference. */ @@ -67,7 +70,7 @@ function EditBody({ citedBy, mode, props }: BodyProps) { guards={props.guards} references={props.references} onDone={mode.done} - onDiscardClose={() => props.onClose(true)} + onDiscardClose={() => props.onClose(true, true)} /> ) diff --git a/apps/app/src/editor/history/useLeaveConfirm.ts b/apps/app/src/editor/history/useLeaveConfirm.ts index 4921cb9a..e5ad6dd6 100644 --- a/apps/app/src/editor/history/useLeaveConfirm.ts +++ b/apps/app/src/editor/history/useLeaveConfirm.ts @@ -125,7 +125,8 @@ function clearerOf(latest: { current: LeaveNav }): (layer: LayerName) => void { const above = layersAbove(layer) if (above.includes('reference') && state.reference) references.close(true) const cited = above.includes('citations') && citationsOfView(state) - if (cited) closeCitations(true) + // Clean, or a draft the user just answered « Abandonner » for: nothing to say. + if (cited) closeCitations(true, true) } } diff --git a/apps/app/src/editor/useEditorNavigation.ts b/apps/app/src/editor/useEditorNavigation.ts index 6cf4b1e5..d17f23fc 100644 --- a/apps/app/src/editor/useEditorNavigation.ts +++ b/apps/app/src/editor/useEditorNavigation.ts @@ -138,9 +138,10 @@ export interface EditorNavigation { openCitations: (citedBy: ElementKey, opener?: Opener) => void /** * A close of the citations dialog: a user one is refused by its draft (it asks first), - * a forced one goes (the draft is dropped, and said so). + * a forced one goes (the draft is dropped, and said so). `discarded`: the user already + * answered « Abandonner », the forced close says nothing (as the other forms). */ - closeCitations: (forced?: boolean) => boolean + closeCitations: (forced?: boolean, discarded?: boolean) => boolean /** « X références », where the focus returns once the citations dialog is closed. */ citationsTrigger: () => HTMLElement | null /** @@ -405,11 +406,11 @@ function useCitationsLayer(ctx: LayerContext, openers: Openers) { [openers, dispatch, mark], ) const closeCitations = useCallback( - (forced = false) => { + (forced = false, discarded = false) => { const answer = guards.ask('citations', forced) if (!answer.allowed) return false - // A forced close of a draft says it was dropped, as the other forced closes. - if (answer.note) announce(answer.note) + // A draft dropped by a move the user did not answer is said; an « Abandonner » is not. + if (answer.note && !discarded) announce(answer.note) mark() dispatch({ type: 'close_citations' }) return true diff --git a/apps/app/tests/unit/editor-citations-edit.test.tsx b/apps/app/tests/unit/editor-citations-edit.test.tsx index 823f072f..a65c363b 100644 --- a/apps/app/tests/unit/editor-citations-edit.test.tsx +++ b/apps/app/tests/unit/editor-citations-edit.test.tsx @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { act, cleanup, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import type { Location, NavigateFunction } from 'react-router-dom' +import { flushAfterModals } from '@/components/ui/afterModalsClosed' import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry' import { forgetSessionTag, @@ -286,7 +287,34 @@ describe('the draft', () => { }) }) +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() + await editCitations(user) + await removeRow(user, /Barone/) + await user.click(button('Fermer')) + await user.click(button('Abandonner')) + 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('Saisie abandonnée') + }) + + it('a system close of the draft (the finding vanished) says it was dropped', async () => { + const user = await mount() + await editCitations(user) + await removeRow(user, /Barone/) + act(() => { + store.getState().commit({ op: 'remove', key: nodeKey }) + }) + await waitFor(() => expect(citations()).toBeUndefined()) + await waitFor(() => expect(live()).toContain('Saisie abandonnée')) + }) + it('« Annuler » asks; « Abandonner » goes back to the view, nothing written', async () => { const user = await mount() await editCitations(user) @@ -545,6 +573,33 @@ describe('a reference over the references dialog of a dirty finding (criterion 2 expect(api.traverseTo).toHaveBeenCalledWith('list') }) + it('the cascade after the user answered « Abandonner » says nothing', async () => { + const user = await mount() + await openFinding(user, /Node A/) + await user.click( + inFindingDialog().getByRole('button', { name: 'Modifier' }), + ) + await user.type(inFindingDialog().getByLabelText('Nom (FR)'), ' bis') + await user.click( + inFindingDialog().getByRole('button', { name: '2 références' }), + ) + await waitFor(() => expect(citations()).toBeDefined()) + await user.click(button('Modifier')) + await removeRow(user, /Barone/) + expect(jump().defaultPrevented).toBe(true) + await user.click(button('Abandonner')) + await waitFor(() => expect(citations()).toBeUndefined()) + const finding = within(findingDialog()!) + await user.click(finding.getByRole('button', { name: 'Abandonner' })) + expect(api.traverseTo).toHaveBeenCalledWith('list') + // The jump is a stub: the page stays. Its dialogs go, so what waited is said. + await act(async () => { + document.querySelectorAll('dialog[open]').forEach((d) => d.remove()) + flushAfterModals() + }) + expect(live()).not.toContain('Saisie abandonnée') + }) + it('« Continuer la saisie » there keeps the finding draft and drops the jump', async () => { const user = await mount() await stacked(user)