diff --git a/apps/app/e2e/shell/editor-citations.spec.ts b/apps/app/e2e/shell/editor-citations.spec.ts index 78381e4e..f7d098ab 100644 --- a/apps/app/e2e/shell/editor-citations.spec.ts +++ b/apps/app/e2e/shell/editor-citations.spec.ts @@ -14,12 +14,14 @@ import { editorActor, expectNoDialog, findingDialog, + historyButton, libraryRoot, openCitations, openEditor, openFinding, openTab, place, + referenceDialog, saveDraft, seedDraft, serverDocument, @@ -162,16 +164,23 @@ test.describe('references dialog, view mode', () => { await expect( tile.getByText(dict['editor.references.this_finding'], { exact: true }), ).toBeVisible() + // The tiles are read only; the dialog's own « Modifier » is the way to its edit mode. for (const name of [ dict['editor.references.edit'], dict['editor.action.delete'], dict['editor.action.move_up'], dict['editor.action.remove'], ]) { - await expect( - citationsDialog(page).getByRole('button', { name, exact: true }), - ).toHaveCount(0) + await expect(tile.getByRole('button', { name, exact: true })).toHaveCount( + 0, + ) } + await expect( + citationsDialog(page).getByRole('button', { + name: dict['editor.action.remove'], + exact: true, + }), + ).toHaveCount(0) await expectAccessible(page, 'the references dialog, an open tile') // The finding's « Modifier » edits its properties only: no citation control, and its @@ -382,6 +391,176 @@ test.describe('references dialog, view mode', () => { }) }) +const editButton = (page: Page) => + citationsDialog(page).getByRole('button', { + name: dict['editor.citations.edit'], + exact: true, + }) +const inDialog = (page: Page, name: string | RegExp) => + citationsDialog(page).getByRole('button', { + name, + exact: typeof name === 'string', + }) +const draftRows = (page: Page) => + citationsDialog(page).locator('[data-citation-row]') +const confirmText = dict['editor.findings.discard_ask'] + +/** Constat A's references dialog in its edit mode. */ +async function editCitations(page: Page) { + await openCitations(page, dict, 'Constat A') + await editButton(page).click() + await expect( + citationsDialog(page).getByRole('heading', { level: 2 }), + ).toHaveText(dict['editor.citations.edit_title']) +} + +const removeRow = (page: Page, citation: string) => + inDialog( + page, + new RegExp(`^${dict['editor.action.remove']} ${citation}`), + ).click() + +test.describe('references dialog, edit mode', () => { + test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) + test.beforeEach(async ({ context }, info) => { + test.skip(isPhone(info), 'editing needs >= 1 024 px') // floor-guard: allow C16 editing needs >= 1024 px; read-only twin at 375 px below + await routeToSandbox(context) + }) + test.afterEach(async ({ context }) => { + await dropLateReplays(context) + }) + + test('cite and remove, one save, one header « Défaire » restores; axe on the edit mode', async ({ + page, + }) => { + test.setTimeout(150_000) + const { seed } = await open(page) + await editCitations(page) + await expectAccessible(page, 'the references dialog, edit mode') + await removeRow(page, 'Référence A') + await citationsDialog(page) + .getByLabel(dict['editor.findings.reference_add']) + .selectOption('ref-d') + await expect(draftRows(page)).toHaveCount(3) + await inDialog(page, dict['editor.findings.save']).click() + await expect(editButton(page)).toBeFocused() + await expect(citationsDialog(page).getByRole('status')).toHaveText( + dict['editor.findings.saved'], + ) + await expect( + citationsDialog(page).getByRole('button', { + name: dict['editor.references.panel_show_all'], + }), + ).toBeVisible() + await closeCitations(page, dict) + await closeFinding(page, dict) + await saveDraft(page, dict, seed.revision + 1) + let saved = await serverDocument>(seed.draftId) + expect(nodeOf(saved).reference_ids).toEqual(['ref-b', 'ref-c', 'ref-d']) + const undo = historyButton(page, dict, 'undo') + await undo.click() + await expect(undo).toHaveAttribute('aria-disabled', 'true') + await saveDraft(page, dict, seed.revision + 2) + saved = await serverDocument>(seed.draftId) + expect(nodeOf(saved).reference_ids).toEqual(['ref-a', 'ref-b', 'ref-c']) + }) + + test('a dirty draft: ✕ and Back ask, « Abandonner » writes nothing', async ({ + page, + }) => { + test.setTimeout(150_000) + await open(page) + await editCitations(page) + await removeRow(page, 'Référence A') + await inDialog(page, dict['common.close']).click() + await expect(citationsDialog(page).getByText(confirmText)).toBeVisible() + await expect(page.locator('dialog[open]')).toHaveCount(2) + await inDialog(page, dict['editor.findings.keep_typing']).click() + await page.goBack() + await expect(citationsDialog(page).getByText(confirmText)).toBeVisible() + await expect(page.locator('dialog[open]')).toHaveCount(2) + await expect(draftRows(page)).toHaveCount(2) + await inDialog(page, dict['editor.findings.discard']).click() + await expect(page.locator('dialog[open]')).toHaveCount(1) + await expect(trigger(page)).toContainText('3') + await closeFinding(page, dict) + await expect(historyButton(page, dict, 'undo')).toHaveAttribute( + 'aria-disabled', + 'true', + ) + }) + + test('« Modifier la référence » stacks the reference dialog; closed, the draft is back', async ({ + page, + }) => { + test.setTimeout(150_000) + const { seed } = await open(page) + await editCitations(page) + await removeRow(page, 'Source C') + const opener = inDialog( + page, + new RegExp(`^${dict['editor.citations.edit_reference']} Référence A`), + ) + await opener.click() + await expect(referenceDialog(page)).toBeVisible() + await expect(page.locator('dialog[open]')).toHaveCount(3) + await referenceDialog(page) + .locator('#reference-form-citation') + .fill('Référence A2') + await referenceDialog(page) + .getByRole('button', { name: dict['editor.findings.save'] }) + .click() + await expect(page.locator('dialog[open]')).toHaveCount(2) + await expect(opener).toBeFocused() + await expect(draftRows(page)).toHaveCount(2) + await expect(draftRows(page).first()).toContainText('Référence A2') + await inDialog(page, dict['editor.findings.save']).click() + await expect(editButton(page)).toBeFocused() + await closeCitations(page, dict) + await closeFinding(page, dict) + await saveDraft(page, dict, seed.revision + 1) + const saved = await serverDocument>(seed.draftId) + expect(nodeOf(saved).reference_ids).toEqual(['ref-a', 'ref-b']) + }) + + test('« Nouvelle référence » is created over the dialog and cited by the save', async ({ + page, + }) => { + test.setTimeout(150_000) + const { seed } = await open(page) + await editCitations(page) + const create = inDialog(page, dict['editor.citations.new_reference']) + await create.click() + const form = referenceDialog(page) + await expect(form).toBeVisible() + await expect(page.locator('dialog[open]')).toHaveCount(3) + await form.locator('#reference-form-type').selectOption('manual') + await form.locator('#reference-form-citation').fill('Source nouvelle') + await form + .getByRole('button', { name: dict['editor.reference.add'] }) + .click() + await expect(page.locator('dialog[open]')).toHaveCount(2) + await expect(create).toBeFocused() + await expect(draftRows(page)).toHaveCount(4) + await expect(draftRows(page).last()).toContainText('Source nouvelle') + await inDialog(page, dict['editor.findings.save']).click() + await expect(editButton(page)).toBeFocused() + await closeCitations(page, dict) + await closeFinding(page, dict) + await saveDraft(page, dict, seed.revision + 1) + const saved = await serverDocument>(seed.draftId) + const refs = saved.references as { id: string; citation: string }[] + const added = refs.find((ref) => ref.citation === 'Source nouvelle') + expect(added).toBeDefined() + expect(nodeOf(saved).reference_ids).toEqual([ + 'ref-a', + 'ref-b', + 'ref-c', + added!.id, + ]) + }) +}) + /** Every way out of the dialog, the focus read from the document after the exit. */ for (const reduced of [false, true]) { test.describe(`references dialog closes, ${reduced ? 'reduced motion' : 'motion'}`, () => { diff --git a/apps/app/e2e/shell/editor-leave-confirm.spec.ts b/apps/app/e2e/shell/editor-leave-confirm.spec.ts index c3130082..4b63dcbf 100644 --- a/apps/app/e2e/shell/editor-leave-confirm.spec.ts +++ b/apps/app/e2e/shell/editor-leave-confirm.spec.ts @@ -11,6 +11,7 @@ import { findingDialog, openFinding, openTab, + citationsDialog, place, referenceDialog, seedDraft, @@ -230,4 +231,48 @@ test.describe('leaving the editor by a history jump', () => { await expect(name).toHaveValue('Constat brouillon') expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) }) + + test('a dirty references draft asks before the jump goes', async ({ + page, + context, + }) => { + test.setTimeout(150_000) + const sheetId = await open(page) + await openFinding(page, dict, 'Constat A') + await showCitations(page) + const dialog = citationsDialog(page) + await dialog + .getByRole('button', { name: dict['editor.citations.edit'], exact: true }) + .click() + await dialog + .getByRole('button', { + name: new RegExp(`^${dict['editor.action.remove']} Référence A`), + }) + .click() + const rows = dialog.locator('[data-citation-row]') + await expect(rows).toHaveCount(0) + + await jumpToOverview(context, page, sheetId) + await expect(dialog.getByText(confirmText)).toBeVisible() + expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) + const keep = dialog.getByRole('button', { + name: dict['editor.findings.keep_typing'], + }) + await expect(keep).toBeFocused() + await keep.click() + await expect(rows).toHaveCount(0) + expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) + + await jumpToOverview(context, page, sheetId) + await expect(dialog.getByText(confirmText)).toBeVisible() + await dialog + .getByRole('button', { name: dict['editor.findings.discard'] }) + .click() + await expect(page).toHaveURL( + (url) => url.pathname === overviewPath(sheetId), + ) + expect(await leftFlags(page)).toBe(0) + await reopenDraft(page) + await expect(page.getByText(leftNotice)).toHaveCount(0) + }) })