Files
faunosteo/apps/app/e2e/shell/editor-citations.spec.ts
T
bchanot 2c4c771269 test(editor): e2e on the references dialog instead of the sheet
Helpers citationsDialog, closeCitations by the dialog's ✕. Owner journey
with tap at 375, keyboard and axe; geometry checks now dialog in viewport
with focus inside; refused « Tout afficher » over a dirty form.
2026-11-15 16:15:43 +01:00

558 lines
21 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { fr } from '@/i18n/fr'
import type { Locator, Page, TestInfo } from '@playwright/test'
import {
expect,
expectAccessible,
expectNoHorizontalOverflow,
test,
} from '../support/browser'
import { dropLateReplays, routeToSandbox } from '../support/sandbox'
import {
citationsDialog,
closeCitations,
closeFinding,
editorActor,
expectNoDialog,
findingDialog,
libraryRoot,
openCitations,
openEditor,
openFinding,
openTab,
place,
saveDraft,
seedDraft,
serverDocument,
showCitations,
standardShape,
} from '../support/editor'
// V1-2.6 R2 finish, Rev 5: the citations of a family or a finding node as a modal
// references dialog over the finding dialog. View mode: its contents (no edit control),
// the finding's edit form that cites nothing and keeps the citations, « Tout afficher »,
// every close path with the focus checked after the exit (motion and reduced motion), the
// exiting dialog inert, the keyboard and touch proofs, axe open (desktop, read-only at
// 375 px), Back and Forward over the layers. The spec seeds its own sheet and never
// touches another.
const dict = fr
const isPhone = (info: TestInfo) => info.project.name === 'chromium-375'
/** « Constat A » cites twelve references (its list outgrows the screen), the family none. */
function longShape(document: Record<string, unknown>) {
shape(document)
const refs = Array.from({ length: 12 }, (_, index) => ({
id: `ref-${index}`,
type: 'book',
citation: `Ouvrage numéro ${index + 1}`,
url: null,
doi: null,
}))
document.references = refs
const zone = (document.finding_zones as { categories: unknown[] }[])[0]
const family = zone.categories[0] as {
reference_ids: string[]
children: { reference_ids: string[] }[]
}
family.reference_ids = []
family.children[0].reference_ids = refs.map((ref) => ref.id)
}
/** « Constat A » cites A, B and C; the family cites B; D is cited by nobody. */
function shape(document: Record<string, unknown>) {
standardShape(document)
const reference = (id: string, citation: string, type = 'book') => ({
id,
type,
citation,
url: null,
doi: null,
})
document.references = [
reference('ref-a', 'Référence A'),
reference('ref-b', 'Source B', 'web'),
reference('ref-c', 'Source C', 'journal'),
reference('ref-d', 'Source libre', 'manual'),
]
const zone = (document.finding_zones as { categories: unknown[] }[])[0]
const family = zone.categories[0] as {
reference_ids: string[]
children: { reference_ids: string[] }[]
}
family.reference_ids = ['ref-b']
family.children[0].reference_ids = ['ref-a', 'ref-b', 'ref-c']
}
async function open(page: Page, seedShape = shape) {
const actor = await editorActor('fr')
const seed = await seedDraft(actor, { shape: seedShape })
await openEditor(page, dict, actor, seed.sheetId)
return { actor, seed }
}
const panel = (page: Page) => page.getByTestId('editor-citations-panel')
const heads = (page: Page) =>
panel(page).locator('button[aria-controls][id$="-header"]')
const trigger = (page: Page) => page.locator('[data-citations-trigger]')
const link = (page: Page) =>
panel(page).getByRole('button', {
name: dict['editor.references.panel_show_all'],
})
const modify = (page: Page) =>
findingDialog(page).getByRole('button', {
name: dict['editor.findings.edit'],
})
const saveForm = (page: Page) =>
findingDialog(page).getByRole('button', {
name: dict['editor.findings.save'],
})
type Node = {
reference_ids: string[]
review_status: string
description: { fr?: string }
}
const nodeOf = (document: Record<string, unknown>) =>
(
document.finding_zones as {
categories: { children: Node[] }[]
}[]
)[0].categories[0].children[0]
test.describe('references dialog, view mode', () => {
test.use({ locale: 'fr-FR', actionTimeout: 15_000 })
test.beforeEach(async ({ context }, info) => {
test.skip(
isPhone(info),
'editing needs >= 1 024 px; the read-only twin at 375 px is below',
) // floor-guard: allow C16 editing needs >= 1024 px; read-only twin in this file
await routeToSandbox(context)
})
test.afterEach(async ({ context }) => {
await dropLateReplays(context)
})
test('opens over its finding dialog, read only; the finding form cites nothing and its save keeps them', async ({
page,
requests,
}) => {
test.setTimeout(150_000)
const { actor, seed } = await open(page)
requests.forbid(actor.account.email, actor.account.password)
await openCitations(page, dict, 'Constat A')
await expect(trigger(page)).toHaveAttribute('aria-expanded', 'true')
await expect(
citationsDialog(page).getByRole('heading', { level: 2 }),
).toHaveText(dict['editor.references.dialog_title'])
await expect(panel(page)).toContainText(
'Zone A › Dysfonction A › Constat A',
)
await expect(panel(page)).toContainText(
dict['editor.references.panel_count_other'].replace('{count}', '3'),
)
await expect(heads(page)).toHaveCount(3)
await expect(panel(page).getByText('Source libre')).toHaveCount(0)
await expect(panel(page).getByRole('searchbox')).toHaveCount(0)
await expectAccessible(page, 'the references dialog, closed tiles')
await heads(page).nth(1).click()
const tile = heads(page).nth(1).locator('xpath=ancestor::li[1]')
await expect(
tile.getByText(dict['editor.references.this_finding'], { exact: true }),
).toBeVisible()
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 expectAccessible(page, 'the references dialog, an open tile')
// The finding's « Modifier » edits its properties only: no citation control, and its
// save keeps the citations the document holds.
await closeCitations(page, dict)
await modify(page).click()
await expect(
findingDialog(page).getByText(dict['editor.findings.references'], {
exact: true,
}),
).toBeVisible()
await expect(trigger(page)).toContainText('3')
await expect(
findingDialog(page).getByLabel(dict['editor.findings.reference_add']),
).toHaveCount(0)
await expect(
findingDialog(page).getByRole('button', {
name: new RegExp(`^${dict['editor.action.remove']}`),
}),
).toHaveCount(0)
await findingDialog(page)
.getByLabel(dict['editor.findings.description_fr'])
.fill('Texte')
await saveForm(page).click()
await expect(trigger(page)).toContainText('3')
await closeFinding(page, dict)
await saveDraft(page, dict, seed.revision + 1)
const saved = await serverDocument<Record<string, unknown>>(seed.draftId)
expect(nodeOf(saved).reference_ids).toEqual(['ref-a', 'ref-b', 'ref-c'])
expect(nodeOf(saved).description.fr).toBe('Texte')
expect((saved.references as unknown[]).length).toBe(4)
})
test('a family opens the same dialog with its own citations', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openCitations(page, dict, 'Dysfonction A')
await expect(panel(page)).toContainText('Zone A › Dysfonction A')
await expect(heads(page)).toHaveCount(1)
await expect(heads(page).first()).toContainText('Source B')
await closeCitations(page, dict)
await expect(trigger(page)).toBeFocused()
})
test('a long list scrolls inside the dialog; « 0 référence » opens the empty one', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page, longShape)
await openCitations(page, dict, 'Constat A')
await expect(heads(page)).toHaveCount(12)
expect(await page.evaluate(() => window.scrollY)).toBe(0)
const list = citationsDialog(page).locator('div.overflow-y-auto')
await list.evaluate((node) => {
node.scrollTop = node.scrollHeight
})
const last = heads(page).last()
await expect(last).toBeInViewport({ ratio: 1 })
expect(await page.evaluate(() => window.scrollY)).toBe(0)
await last.click()
await expect(last).toHaveAttribute('aria-expanded', 'true')
await closeCitations(page, dict)
await closeFinding(page, dict)
await openFinding(page, dict, 'Dysfonction A')
await expect(trigger(page)).toHaveText(
dict['editor.findings.references_open_one'].replace('{count}', '0'),
)
await showCitations(page)
await expect(heads(page)).toHaveCount(0)
await expect(panel(page)).toContainText(
dict['editor.references.panel_empty'],
)
})
test('« Tout afficher » closes both layers, clears the search, keeps the sort and the open tiles; Back returns to the dialog', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openTab(page, dict, 'references')
const root = libraryRoot(page)
await root.getByRole('searchbox').fill('Source')
await root
.getByLabel(dict['editor.references.sort_by'])
.selectOption('citation')
await root.getByRole('button', { name: /Source B/ }).click()
await openCitations(page, dict, 'Constat A')
await link(page).click()
await expectNoDialog(page)
await expect(page.locator('#editor-references-title')).toBeFocused()
await expect(root.getByRole('searchbox')).toHaveValue('')
await expect(
root.getByLabel(dict['editor.references.sort_by']),
).toHaveValue('citation')
await expect(
root.getByRole('button', { name: /Source B/ }),
).toHaveAttribute('aria-expanded', 'true')
// Filtered by the finding the dialog belonged to: its three citations (R2F-6).
await expect(
root.locator('button[aria-controls][id$="-header"]'),
).toHaveCount(3)
await expect(root.getByTestId('editor-references-filter')).toContainText(
'Constat A',
)
// The move is an entry of its own: Back returns to the finding, its dialog on top.
await page.goBack()
await expect(place(page, 'citations')).toBeVisible()
await expect(findingDialog(page)).toBeVisible()
await expect(page.locator('dialog[open]')).toHaveCount(2)
await page.goForward()
await expectNoDialog(page)
await expect(place(page, 'references')).toBeVisible()
})
test('the exiting dialog is inert, and a second close finds nothing to close', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openCitations(page, dict, 'Constat A')
await page.evaluate(() => {
const samples: boolean[] = []
Object.assign(window, { samples })
const tick = () => {
const layer = document.querySelector('[data-place="citations"]')
if (layer === null) return
samples.push(layer.closest('dialog')?.hasAttribute('inert') ?? false)
requestAnimationFrame(tick)
}
tick()
})
const closeButton = citationsDialog(page).getByRole('button', {
name: dict['common.close'],
})
await closeButton.dispatchEvent('click')
await closeButton.dispatchEvent('click').catch(() => undefined)
await expect(panel(page)).toHaveCount(0)
const samples = await page.evaluate(
() => (window as unknown as { samples: boolean[] }).samples,
)
expect(samples.some((inert) => inert)).toBe(true)
// The finding dialog under it is still open: the second click closed nothing more.
await expect(findingDialog(page)).toBeVisible()
await expect(page.locator('dialog[open]')).toHaveCount(1)
})
test('keyboard: Enter opens the dialog, Tab reaches each control in order, Escape closes only it', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openFinding(page, dict, 'Constat A')
await trigger(page).focus()
await page.keyboard.press('Enter')
await expect(place(page, 'citations')).toBeVisible()
await expect(page.locator('dialog[open]')).toHaveCount(2)
// The tab order of the dialog, in the document's order, each control tagged.
const count = await panel(page).evaluate((root) => {
const dialog = root.closest('dialog')!
const tabbable = [
...dialog.querySelectorAll<HTMLElement>(
'button:not([disabled]), [href], input:not([disabled]), select, textarea',
),
]
tabbable.forEach((el, index) => el.setAttribute('data-kb', String(index)))
return tabbable.length
})
expect(count).toBeGreaterThanOrEqual(5)
const at = () =>
page.evaluate(() => document.activeElement?.getAttribute('data-kb'))
// On open the first control (✕) holds the focus; Tab then walks the rest.
expect(await at()).toBe('0')
for (let index = 1; index < count; index++) {
await page.keyboard.press('Tab')
expect(await at(), `Tab to control ${index}`).toBe(String(index))
}
await page.keyboard.press('Escape')
await expect(panel(page)).toHaveCount(0)
await expect(findingDialog(page)).toBeVisible()
await expect(trigger(page)).toBeFocused()
})
})
/** 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'}`, () => {
test.use({ locale: 'fr-FR', actionTimeout: 15_000 })
test.beforeEach(async ({ context, page }, info) => {
test.skip(isPhone(info), 'the paths are walked on desktop') // floor-guard: allow C16 editing needs >= 1 024 px
await routeToSandbox(context)
await page.emulateMedia({
reducedMotion: reduced ? 'reduce' : 'no-preference',
})
})
test.afterEach(async ({ context }) => {
await dropLateReplays(context)
})
test('✕, Escape, the backdrop and Back give the focus back to the trigger', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openCitations(page, dict, 'Constat A')
await closeCitations(page, dict)
await expect(trigger(page)).toBeFocused()
await expect(trigger(page)).toHaveAttribute('aria-expanded', 'false')
await showCitations(page)
await page.keyboard.press('Escape')
await expect(panel(page)).toHaveCount(0)
await expect(trigger(page)).toBeFocused()
await showCitations(page)
await citationsDialog(page)
.getByTestId('dialog-backdrop')
.click({ position: { x: 12, y: 40 } })
await expect(panel(page)).toHaveCount(0)
await expect(trigger(page)).toBeFocused()
await showCitations(page)
await page.goBack()
await expect(panel(page)).toHaveCount(0)
await expect(findingDialog(page)).toBeVisible()
await expect(trigger(page)).toBeFocused()
await page.goForward()
await expect(panel(page)).toBeVisible()
await expect(page.locator('dialog[open]')).toHaveCount(2)
})
test('« Tout afficher » and « Aller à » land where the move says', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openCitations(page, dict, 'Constat A')
await link(page).click()
await expectNoDialog(page)
await expect(page.locator('#editor-references-title')).toBeFocused()
await openCitations(page, dict, 'Constat A')
await heads(page).nth(1).click()
await panel(page)
.getByRole('button', { name: dict['editor.references.go_to'] })
.first()
.click()
// The dialog closes and the family opens in the finding dialog (view mode);
// closed, the row holds the focus.
await expect(place(page, 'citations')).toHaveCount(0)
const family = findingDialog(page).getByRole('heading', {
name: 'Dysfonction A',
})
await expect(family).toBeVisible()
// Back returns to the place « Aller à » left: Constat A with its dialog open.
await page.goBack()
await expect(page.locator('dialog[open]')).toHaveCount(2)
await expect(place(page, 'citations')).toBeVisible()
await expect(
findingDialog(page).getByRole('heading', { name: 'Constat A' }),
).toBeVisible()
await page.goForward()
await expect(place(page, 'citations')).toHaveCount(0)
await expect(family).toBeVisible()
await closeFinding(page, dict)
await expect(
page.getByRole('treeitem', { name: /Dysfonction A/ }),
).toBeFocused()
})
test('the dialog scales in only when motion is allowed', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openFinding(page, dict, 'Constat A')
await page.evaluate(() => {
const seen: string[] = []
Object.assign(window, { seen })
new MutationObserver(() => {
const panel = document.querySelector('[data-place="citations"]')
const surface = panel?.closest('dialog > div:last-child')
if (surface instanceof HTMLElement) seen.push(surface.style.transform)
}).observe(document.body, {
subtree: true,
childList: true,
attributes: true,
attributeFilter: ['style'],
})
})
await trigger(page).click()
await expect(place(page, 'citations')).toBeVisible()
await expect(page.locator('dialog[open]')).toHaveCount(2)
const seen = await page.evaluate(
() => (window as unknown as { seen: string[] }).seen,
)
const scaled = seen.some((value) => /scale\(0\.9/.test(value))
expect(scaled).toBe(!reduced)
})
})
}
test.describe('references dialog, read-only at 375 px', () => {
test.use({ locale: 'fr-FR', actionTimeout: 15_000 })
test.beforeEach(async ({ context }, info) => {
test.skip(!isPhone(info), 'the read-only twin runs at 375 px only')
await routeToSandbox(context)
})
test.afterEach(async ({ context }) => {
await dropLateReplays(context)
})
test('inside the viewport through « Plus » then Constats, no edit control, axe open @phone', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openCitations(page, dict, 'Constat A')
const box = await citationsDialog(page)
.locator('div.overflow-y-auto')
.boundingBox()
expect(box!.x).toBeGreaterThanOrEqual(0)
// Sub-pixel layout rounding (375.00001…): the tolerance of editor-shell-layout (3a7ffa2).
expect(box!.x + box!.width).toBeLessThanOrEqual(375 + 0.5)
expect(box!.y).toBeGreaterThanOrEqual(0)
expect(box!.y + box!.height).toBeLessThanOrEqual(667 + 0.5)
await expect(heads(page)).toHaveCount(3)
await expectNoHorizontalOverflow(page, 'the references dialog at 375 px')
await heads(page).first().click()
for (const name of [
dict['editor.references.edit'],
dict['editor.action.remove'],
]) {
await expect(
citationsDialog(page).getByRole('button', { name, exact: true }),
).toHaveCount(0)
}
await expect(panel(page).getByRole('textbox')).toHaveCount(0)
await expectAccessible(page, 'the references dialog, read-only at 375 px')
await closeCitations(page, dict)
await expect(trigger(page)).toBeFocused()
await closeFinding(page, dict)
await expect(place(page, 'findings')).toBeVisible()
})
test('by touch: taps act on the references dialog only, the finding dialog under it stays @phone', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openFinding(page, dict, 'Constat A')
const center = async (target: Locator) => {
const box = await target.boundingBox()
return { x: box!.x + box!.width / 2, y: box!.y + box!.height / 2 }
}
const tap = async (target: Locator) => {
const at = await center(target)
await page.touchscreen.tap(at.x, at.y)
}
await tap(trigger(page))
await expect(place(page, 'citations')).toBeVisible()
// A tile opens by a tap; the finding dialog's controls underneath stay as they were.
await tap(heads(page).first())
await expect(heads(page).first()).toHaveAttribute('aria-expanded', 'true')
await expect(trigger(page)).toHaveAttribute('aria-expanded', 'true')
// Its ✕ closes it only; the trigger holds the focus.
await tap(
citationsDialog(page).getByRole('button', { name: dict['common.close'] }),
)
await expect(panel(page)).toHaveCount(0)
await expect(findingDialog(page)).toBeVisible()
await expect(trigger(page)).toBeFocused()
// A tap on its backdrop, outside its panel, closes it only as well.
await tap(trigger(page))
await expect(place(page, 'citations')).toBeVisible()
await page.touchscreen.tap(4, 4)
await expect(panel(page)).toHaveCount(0)
await expect(findingDialog(page)).toBeVisible()
await expect(trigger(page)).toBeFocused()
await tap(
findingDialog(page).getByRole('button', { name: dict['common.close'] }),
)
await expectNoDialog(page)
})
})