Files
faunosteo/apps/app/e2e/shell/editor-references.spec.ts
T

632 lines
22 KiB
TypeScript

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,
historyButton,
libraryRoot,
openEditor,
openTab,
place,
saveDraft,
seedDraft,
serverDocument,
serverRevision,
standardShape,
} from '../support/editor'
// V1-2.6 R2.2: the collapsible reference tiles of the Références tab (boards 23, 24, 27):
// closed, open and edit states with axe on each, the keyboard walk through the tile
// headers, the leave guard of the edit view, 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<string, unknown>) {
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, edit mode', () => {
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, open and edit tiles, 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')
// Edit: the title, the focused citation, « Terminer » only.
await cited
.getByRole('button', { name: dict['editor.references.edit'] })
.click()
await expect(
cited.getByText(dict['editor.references.edit_title']),
).toBeVisible()
await expect(
cited.getByLabel(dict['editor.reference.citation']),
).toBeFocused()
await expect(
cited.getByRole('button', { name: dict['editor.action.move_up'] }),
).toHaveCount(0)
await expectAccessible(page, 'a reference tile in edit')
})
test('« Terminer » keeps the edit view while a field is refused', 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 url = tile.getByLabel(dict['editor.reference.url'])
await url.fill('exemple.org')
await tile
.getByRole('button', {
name: dict['editor.references.done'],
exact: true,
})
.click()
await expect(url).toBeFocused()
await expect(
tile.getByRole('button', {
name: dict['editor.references.done'],
exact: true,
}),
).toBeVisible()
await url.fill('https://exemple.org/page')
await tile
.getByRole('button', {
name: dict['editor.references.done'],
exact: true,
})
.click()
await expect(tile.getByLabel(dict['editor.reference.url'])).toHaveCount(0)
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<string, unknown>) {
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, save it, 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 libraryRoot(page)
.getByRole('button', { name: dict['editor.references.add'] })
.click()
await expect(bar.search).toHaveValue('')
await expect(bar.sort).toHaveValue('sheet')
const citation = libraryRoot(page).getByLabel(
dict['editor.reference.citation'],
)
await expect(citation).toBeFocused()
// The focus scrolled the toolbar under the sticky header: axe judges what is covered.
await libraryRoot(page).getByRole('searchbox').scrollIntoViewIfNeeded()
await expectAccessible(page, 'the draft tile')
await libraryRoot(page)
.getByLabel(dict['editor.reference.type'])
.selectOption('manual')
await citation.fill('Nouvelle source')
await libraryRoot(page)
.getByRole('button', { name: dict['editor.reference.add'] })
.click()
await expect(headers(page)).toHaveCount(5)
const added = headers(page).last()
await expect(added).toContainText('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 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 l'ajout » and Escape drop the 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').isEnabled(),
await historyButton(page, dict, 'redo').isEnabled(),
]
const before = await states()
const draft = root.getByRole('region', {
name: dict['editor.reference.new'],
})
const type = root.getByLabel(dict['editor.reference.type'])
await add.click()
await type.selectOption('manual')
await root.getByLabel(dict['editor.reference.citation']).fill('Brouillon')
await root
.getByRole('button', { name: dict['editor.references.add_cancel'] })
.click()
await expect(draft).toHaveCount(0)
await expect(headers(page)).toHaveCount(count)
await expect(add).toBeFocused()
expect(await states()).toEqual(before)
// A typed draft, then Escape on the type select (not a text target): it cancels.
await add.click()
await root.getByLabel(dict['editor.reference.citation']).fill('Brouillon')
await type.focus()
await page.keyboard.press('Escape')
await expect(draft).toHaveCount(0)
await expect(headers(page)).toHaveCount(count)
await expect(add).toBeFocused()
expect(await states()).toEqual(before)
})
test('a tile in edit keeps its place under a sort and under a 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)
await bar.sort.selectOption('citation')
await headers(page).nth(3).click()
await tileAt(page, 3)
.getByRole('button', { name: dict['editor.references.edit'] })
.click()
await bar.sort.selectOption('count')
await expect(
tileAt(page, 3).getByLabel(dict['editor.reference.citation']),
).toBeVisible()
await bar.search.fill('alpha')
await expect(headers(page)).toHaveCount(2)
await expect(
libraryRoot(page).getByLabel(dict['editor.reference.citation']),
).toBeVisible()
})
test('a tile edited out of the search stays after « Terminer », 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 citation = tile.getByLabel(dict['editor.reference.citation'])
await citation.fill('Autre titre')
await tile
.getByRole('button', {
name: dict['editor.references.done'],
exact: true,
})
.click()
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 refused draft is never dropped silently by a read-only flip', async ({
page,
}) => {
test.setTimeout(150_000)
await open(page)
await openTab(page, dict, 'references')
const bar = toolbar(page)
await headers(page).nth(REF_DOI).click()
const tile = libraryRoot(page)
.locator('button[id$="-ref-doi-header"]')
.locator('xpath=ancestor::li[1]')
await tile
.getByRole('button', { name: dict['editor.references.edit'] })
.click()
await bar.search.fill('libre')
const url = tile.getByLabel(dict['editor.reference.url'])
await url.fill('exemple.org')
const note = dict['editor.references.draft_dropped'].replace(
'{field}',
dict['editor.reference.url'],
)
const size = page.viewportSize() ?? { width: 1280, height: 900 }
await page.setViewportSize({ width: 800, height: size.height })
await expect(tile.getByText(note)).toBeVisible()
await expect(page.locator('p.sr-only[aria-live="polite"]')).toHaveText(note)
await expect(url).toHaveCount(0)
await page.setViewportSize(size)
await expect(tile.getByText(note)).toBeVisible()
await expect(url).toHaveValue(`https://doi.org/${DOI}`)
await tile
.getByRole('button', {
name: dict['editor.references.done'],
exact: true,
})
.click()
await expect(tile.getByText(note)).toHaveCount(0)
})
})
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')
})
})