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 { editorActor, expectNoDialog, historyButton, libraryRoot, openEditor, openTab, place, referenceDialog, referenceSettled, saveDraft, seedDraft, serverDocument, serverRevision, standardShape, startReference, } from '../support/editor' // V1-2.6 R2.2 and R2F-5: the collapsible reference tiles of the Références tab (boards 23, // 24, 27): closed and open states with axe on each, the reference dialog (« Modifier », // « Ajouter ») with axe, the keyboard walk through the tile headers, the dialog's draft // and its read-only flip, and the read-only twin at 375 px. The spec seeds its own sheet // and never touches another. const dict = fr const isPhone = (info: TestInfo) => info.project.name === 'chromium-375' const DOI = '10.3389/fvets.2022.898096' /** « Référence A » (cited by « Constat A ») plus one cited by a family and one uncited. */ function shape(document: Record) { standardShape(document) document.references = [ { id: 'ref-a', type: 'book', citation: 'Référence A', url: null, doi: null, }, { id: 'ref-doi', type: 'journal', citation: 'Long-term outcome of horses undergoing surgery, Front Vet Sci 2022', url: `https://doi.org/${DOI}`, doi: DOI, }, { id: 'ref-free', type: 'web', citation: 'Source libre', url: null, doi: null, }, ] const zone = (document.finding_zones as { categories: unknown[] }[])[0] const family = zone.categories[0] as { reference_ids: string[] } family.reference_ids = ['ref-doi'] } 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 headers = (page: Page) => libraryRoot(page).locator('button[aria-controls][id$="-header"]') /** The tile at a position of the seeded list (header ids stay put while it is in edit). */ const tileAt = (page: Page, position: number): Locator => headers(page).nth(position).locator('xpath=ancestor::li[1]') const REF_DOI = 1 const REF_A = 0 const REF_FREE = 2 test.describe('reference tiles and the reference dialog', () => { test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) test.beforeEach(async ({ context }, info) => { test.skip( isPhone(info), 'editing needs >= 1 024 px; the phone twin 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('closed and open tiles, then the reference dialog, accessible in each state', async ({ page, requests, }) => { test.setTimeout(120_000) const { actor } = await open(page) requests.forbid(actor.account.email, actor.account.password) await openTab(page, dict, 'references') await expect(headers(page)).toHaveCount(3) await expect(headers(page).first()).toHaveAttribute( 'aria-expanded', 'false', ) await expect( libraryRoot(page).getByText(dict['editor.references.uncited']), ).toHaveCount(1) await expectNoHorizontalOverflow(page, 'the closed tiles') await expectAccessible(page, 'the closed reference tiles') // Open: facts as safe links, the citers, the hint on a cited reference. await headers(page).nth(1).click() const cited = tileAt(page, REF_DOI) await expect(headers(page).nth(1)).toHaveAttribute('aria-expanded', 'true') const doi = cited .getByRole('link', { name: new RegExp(DOI.replace('.', '\\.')) }) .first() await expect(doi).toHaveAttribute('href', `https://doi.org/${DOI}`) await expect(doi).toHaveAttribute('target', '_blank') await expect(doi).toHaveAttribute('rel', 'noopener noreferrer') await expect(cited.getByText('Dysfonction A')).toBeVisible() await expect( cited.getByText(dict['editor.references.family'], { exact: true }), ).toBeVisible() await expect( cited.getByRole('button', { name: dict['editor.action.delete'] }), ).toHaveCount(0) await expect( cited.getByText(dict['editor.references.delete_hint']), ).toBeVisible() await headers(page).nth(2).click() await expect( tileAt(page, REF_FREE).getByRole('button', { name: dict['editor.action.delete'], }), ).toBeVisible() await expectAccessible(page, 'the open reference tiles') // « Modifier » opens the dialog: named, over the page, accessible; the tile has no field. const modify = cited.getByRole('button', { name: dict['editor.references.edit'], }) await modify.click() const form = referenceDialog(page) await expect(form).toBeVisible() await expect( form.getByRole('heading', { name: dict['editor.references.edit_title'] }), ).toBeVisible() await expect(form.locator(':focus')).toHaveCount(1) await expect( cited.getByLabel(dict['editor.reference.citation']), ).toHaveCount(0) await expectAccessible(page, 'the reference dialog') await form .getByRole('button', { name: dict['editor.findings.cancel'] }) .click() await expectNoDialog(page) await referenceSettled(page) await expect(modify).toBeFocused() }) test('« Enregistrer » refuses a bad address on its field, then saves one command', async ({ page, }) => { test.setTimeout(120_000) await open(page) await openTab(page, dict, 'references') await headers(page).nth(2).click() const tile = tileAt(page, REF_FREE) await tile .getByRole('button', { name: dict['editor.references.edit'] }) .click() const form = referenceDialog(page) const url = form.getByLabel(dict['editor.reference.url']) await url.fill('exemple.org') await form .getByRole('button', { name: dict['editor.findings.save'] }) .click() await expect(url).toBeFocused() await expect(url).toHaveAccessibleDescription( dict['editor.reference.url_invalid'], ) await expect(historyButton(page, dict, 'undo')).toHaveAttribute( 'aria-disabled', 'true', ) await url.fill('https://exemple.org/page') await form .getByRole('button', { name: dict['editor.findings.save'] }) .click() await expectNoDialog(page) await referenceSettled(page) await expect( tile.getByRole('link', { name: /exemple\.org/ }), ).toHaveAttribute('href', 'https://exemple.org/page') }) test('the tile headers are a keyboard walk: Tab between them, Enter and Space open', async ({ page, }) => { test.setTimeout(120_000) await open(page) await openTab(page, dict, 'references') // The toolbar's last stop (search, sort, add) precedes the first header. await page .getByRole('button', { name: dict['editor.references.add'] }) .focus() for (let at = 0; at < 3; at++) { await page.keyboard.press('Tab') await expect(headers(page).nth(at)).toBeFocused() } // Focus is on the third header: one Shift+Tab back reaches the second. await page.keyboard.press('Shift+Tab') await expect(headers(page).nth(1)).toBeFocused() await page.keyboard.press('Enter') await expect(headers(page).nth(1)).toHaveAttribute('aria-expanded', 'true') // Open, the tile's own content is the next tab stop: its DOI link, then back. await page.keyboard.press('Tab') await expect(tileAt(page, REF_DOI).getByRole('link').first()).toBeFocused() await page.keyboard.press('Shift+Tab') await expect(headers(page).nth(1)).toBeFocused() await page.keyboard.press('Space') await expect(headers(page).nth(1)).toHaveAttribute('aria-expanded', 'false') }) test('« Aller à » on a citer opens the Constats tab on it', async ({ page, }) => { test.setTimeout(120_000) await open(page) await openTab(page, dict, 'references') await headers(page).nth(0).click() await tileAt(page, REF_A) .getByRole('button', { name: dict['editor.references.go_to'] }) .click() await expect(place(page, 'findings')).toBeVisible() await expect(place(page, 'findings').locator(':focus')).toHaveCount(1) }) }) /** * Four references with distinct orders. Stored: Zèbre 10, zebre 9, Alpha, Étude. * Citation A → Z: Alpha, Étude, zebre 9, Zèbre 10. Citations: zebre 9 two (family and * node), Alpha one, the others none. Types: book, journal, legal, web. */ function sortShape(document: Record) { standardShape(document) const reference = (id: string, type: string, citation: string) => ({ id, type, citation, url: null, doi: null, }) document.references = [ reference('ref-z', 'web', 'Zèbre 10'), reference('ref-y', 'journal', 'zebre 9'), reference('ref-a', 'book', 'Alpha'), reference('ref-e', 'legal', 'Étude'), ] 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-y'] family.children[0].reference_ids = ['ref-y', 'ref-a'] } const SORTED = { sheet: ['Zèbre 10', 'zebre 9', 'Alpha', 'Étude'], citation: ['Alpha', 'Étude', 'zebre 9', 'Zèbre 10'], count: ['zebre 9', 'Alpha', 'Zèbre 10', 'Étude'], type: ['Alpha', 'zebre 9', 'Étude', 'Zèbre 10'], } as const /** The citations of the tiles in display order (the header's first text node). */ const shownCitations = (page: Page) => headers(page).evaluateAll((buttons) => buttons.map((button) => button.querySelector('span')?.textContent ?? ''), ) test.describe('reference tiles, search, sort and add', () => { test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) test.beforeEach(async ({ context }, info) => { test.skip( isPhone(info), 'editing needs >= 1 024 px; the phone twin 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) }) const toolbar = (page: Page) => ({ search: libraryRoot(page).getByRole('searchbox', { name: dict['editor.references.search'], }), sort: libraryRoot(page).getByLabel(dict['editor.references.sort_by']), clear: libraryRoot(page).getByRole('button', { name: dict['editor.references.clear_search'], }), }) test('four sorts, search, clear, no result, move buttons; the server never changes', async ({ page, requests, }) => { test.setTimeout(150_000) const { actor, seed } = await open(page, sortShape) requests.forbid(actor.account.email, actor.account.password) const before = await serverDocument(seed.draftId) const revision = await serverRevision(seed.draftId) await openTab(page, dict, 'references') const bar = toolbar(page) await expect( libraryRoot(page).getByText('4 références', { exact: true }), ).toBeVisible() for (const [sort, expected] of Object.entries(SORTED)) { await bar.sort.selectOption(sort) await expect.poll(() => shownCitations(page)).toEqual(expected) } await expectAccessible(page, 'the type sort') // Search: count line, hint instead of the move buttons, clear, Escape. await bar.sort.selectOption('sheet') await headers(page).first().click() const down = libraryRoot(page).getByRole('button', { name: dict['editor.action.move_down'], }) await expect(down).toBeVisible() await bar.search.fill('zebre') await expect( libraryRoot(page).getByText('2 références sur 4', { exact: true }), ).toBeVisible() await expect(down).toHaveCount(0) await expect( libraryRoot(page).getByText(dict['editor.references.reorder_hint']), ).toBeVisible() await expectAccessible(page, 'a search in progress') await bar.clear.click() await expect(bar.search).toHaveValue('') await expect(bar.search).toBeFocused() await expect(down).toBeVisible() await bar.search.fill('alpha') await page.keyboard.press('Escape') await expect(bar.search).toHaveValue('') // No result, then the notice's own clear button. await bar.search.fill('radiographie') const notice = libraryRoot(page).getByTestId('editor-references-no-result') await expect(notice).toContainText('radiographie') await expect(notice).toContainText('Aucune référence ne contient') await expectAccessible(page, 'the no result notice') await notice .getByRole('button', { name: dict['editor.references.clear_search'] }) .click() await expect(bar.search).toBeFocused() await expect(headers(page)).toHaveCount(4) // Display only: nothing was saved, the server holds the same document and revision. expect(await serverDocument(seed.draftId)).toEqual(before) expect(await serverRevision(seed.draftId)).toBe(revision) }) test('add a reference: the list stays, a notice offers « Afficher »; then delete an uncited one', async ({ page, requests, }) => { test.setTimeout(150_000) const { actor, seed } = await open(page, sortShape) requests.forbid(actor.account.email, actor.account.password) await openTab(page, dict, 'references') const bar = toolbar(page) await bar.sort.selectOption('count') await bar.search.fill('alpha') await startReference(page, dict) // The dialog is a layer over the page: the search and the sort are untouched. await expect(bar.search).toHaveValue('alpha') await expect(bar.sort).toHaveValue('count') const form = referenceDialog(page) await expectAccessible(page, 'the creation dialog') await form.locator('#reference-form-type').selectOption('manual') await form.locator('#reference-form-citation').fill('Nouvelle source') await form .getByRole('button', { name: dict['editor.reference.add'] }) .click() await expectNoDialog(page) await referenceSettled(page) await expect(headers(page)).toHaveCount(1) const notice = libraryRoot(page).getByText( dict['editor.references.added'].replace('{citation}', 'Nouvelle source'), ) await expect(notice).toBeVisible() await expect( libraryRoot(page).getByRole('button', { name: dict['editor.references.add'], }), ).toBeFocused() await libraryRoot(page) .getByRole('button', { name: dict['editor.references.added_show'] }) .click() await expect(notice).toHaveCount(0) await expect(bar.search).toHaveValue('') await expect(headers(page)).toHaveCount(5) const added = headers(page).filter({ hasText: 'Nouvelle source' }) await expect(added).toHaveAttribute('aria-expanded', 'true') await expect(added).toBeFocused() await saveDraft(page, dict, seed.revision + 1) const saved = await serverDocument<{ references: { citation: string }[] }>( seed.draftId, ) expect(saved.references.map((r) => r.citation)).toEqual([ ...SORTED.sheet, 'Nouvelle source', ]) // Delete an uncited reference (Étude): the dialog confirms, the tile goes. await bar.sort.selectOption('sheet') await headers(page).nth(3).click() await tileAt(page, 3) .getByRole('button', { name: dict['editor.action.delete'] }) .click() await page .getByRole('dialog') .getByRole('button', { name: dict['editor.delete.confirm'] }) .click() await expect(headers(page)).toHaveCount(4) await expect(libraryRoot(page).getByText('Étude')).toHaveCount(0) }) test('« Annuler » and Escape ask before they drop a typed draft, and write nothing', async ({ page, requests, }) => { test.setTimeout(150_000) const { actor } = await open(page, sortShape) requests.forbid(actor.account.email, actor.account.password) await openTab(page, dict, 'references') const root = libraryRoot(page) const add = root.getByRole('button', { name: dict['editor.references.add'], }) const count = await headers(page).count() const states = async () => [ await historyButton(page, dict, 'undo').getAttribute('aria-disabled'), await historyButton(page, dict, 'redo').getAttribute('aria-disabled'), ] const before = await states() await startReference(page, dict) const form = referenceDialog(page) await form.locator('#reference-form-type').selectOption('manual') await form.locator('#reference-form-citation').fill('Brouillon') await form .getByRole('button', { name: dict['editor.findings.cancel'] }) .click() await expect( form.getByText(dict['editor.findings.discard_ask']), ).toBeVisible() await form .getByRole('button', { name: dict['editor.findings.keep_typing'] }) .click() await expect(form.locator('#reference-form-citation')).toHaveValue( 'Brouillon', ) await page.keyboard.press('Escape') await expect( form.getByText(dict['editor.findings.discard_ask']), ).toBeVisible() await form .getByRole('button', { name: dict['editor.findings.discard'] }) .click() await expectNoDialog(page) await referenceSettled(page) await expect(headers(page)).toHaveCount(count) await expect(add).toBeFocused() expect(await states()).toEqual(before) }) test('a tile edited out of the search stays after « Enregistrer », with its line, until the next search', async ({ page, requests, }) => { test.setTimeout(150_000) const { actor } = await open(page, sortShape) requests.forbid(actor.account.email, actor.account.password) await openTab(page, dict, 'references') const bar = toolbar(page) const notice = libraryRoot(page).getByTestId('editor-references-no-result') await bar.search.fill('alpha') await expect(headers(page)).toHaveCount(1) await headers(page).first().click() const tile = tileAt(page, 0) await tile .getByRole('button', { name: dict['editor.references.edit'] }) .click() const form = referenceDialog(page) await form.getByLabel(dict['editor.reference.citation']).fill('Autre titre') await form .getByRole('button', { name: dict['editor.findings.save'] }) .click() await expectNoDialog(page) await expect( tile.getByText(dict['editor.references.outside_search']), ).toBeVisible() await expect(notice).toHaveCount(0) await expect(bar.clear).toBeVisible() await bar.search.fill('alphab') await expect(notice).toBeVisible() await expect(headers(page)).toHaveCount(0) }) test('a draft is never dropped by a read-only flip: the dialog keeps it, read-only, with its notice', async ({ page, }) => { test.setTimeout(150_000) await open(page) await openTab(page, dict, 'references') await headers(page).nth(REF_DOI).click() await tileAt(page, REF_DOI) .getByRole('button', { name: dict['editor.references.edit'] }) .click() const form = referenceDialog(page) const url = form.getByLabel(dict['editor.reference.url']) await url.fill('exemple.org') const size = page.viewportSize() ?? { width: 1280, height: 900 } await page.setViewportSize({ width: 800, height: size.height }) await expect(form).toBeVisible() await expect(url).toHaveAttribute('readonly', '') await expect(url).toHaveValue('exemple.org') await expect( form.getByText(dict['editor.findings.flip_narrow']), ).toBeVisible() await expect( form.getByRole('button', { name: dict['editor.findings.save'] }), ).toBeDisabled() await page.setViewportSize(size) await expect(url).not.toHaveAttribute('readonly', '') await expect(url).toHaveValue('exemple.org') await form .getByRole('button', { name: dict['editor.findings.cancel'] }) .click() await form .getByRole('button', { name: dict['editor.findings.discard'] }) .click() await expectNoDialog(page) await expect( tileAt(page, REF_DOI) .getByRole('link', { name: new RegExp(DOI) }) .first(), ).toBeVisible() }) }) test.describe('reference tiles, 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('« Plus » lists Références; tiles keep DOI, URL and « Aller à », no edit control @phone', async ({ page, }) => { test.setTimeout(120_000) await open(page) await openTab(page, dict, 'references') await expect(headers(page)).toHaveCount(3) const box = await headers(page).first().boundingBox() expect(box?.height ?? 0).toBeGreaterThanOrEqual(44) // Board 27: search and sort are 44 px targets, there is no « Ajouter » and no hint. const root = libraryRoot(page) const search = root.getByRole('searchbox') const sort = root.getByLabel(dict['editor.references.sort_by']) for (const control of [search, sort]) { expect((await control.boundingBox())?.height ?? 0).toBeGreaterThanOrEqual( 44, ) } await expect( root.getByRole('button', { name: dict['editor.references.add'] }), ).toHaveCount(0) await sort.selectOption('citation') await search.fill('source') await expect(headers(page)).toHaveCount(1) await expect( root.getByText(dict['editor.references.reorder_hint']), ).toHaveCount(0) await root .getByRole('button', { name: dict['editor.references.clear_search'] }) .click() await sort.selectOption('sheet') await expect(headers(page)).toHaveCount(3) await expectNoHorizontalOverflow(page, 'the closed tiles at 375 px') await expectAccessible(page, 'the closed tiles at 375 px') await headers(page).nth(1).click() const cited = tileAt(page, REF_DOI) await expect(cited.getByRole('link').first()).toHaveAttribute( 'target', '_blank', ) const goTo = cited.getByRole('button', { name: dict['editor.references.go_to'], }) expect((await goTo.boundingBox())?.height ?? 0).toBeGreaterThanOrEqual(44) for (const name of [ dict['editor.references.edit'], dict['editor.action.delete'], dict['editor.action.move_up'], ]) { await expect(libraryRoot(page).getByRole('button', { name })).toHaveCount( 0, ) } await expectNoHorizontalOverflow(page, 'an open tile at 375 px') await expectAccessible(page, 'an open tile at 375 px') }) })