import { en } from '@/i18n/en' import { fr } from '@/i18n/fr' import { expect, expectAccessible, expectNoHorizontalOverflow, focusOwner, focusedName, test, } from '../support/browser' import type { Page } from '@playwright/test' // The real WorkingCopyAction (components/admin) on its test page // (e2e/harness/working-copy.tsx): the dialog's accessibility, layout and // keyboard checks that e2e/shell/admin.spec.ts cannot make on the historical // sheet, where the single working-copy slot is shared, mutable database state. const SLUG_FIELD = 'working-copy-slug' for (const [lang, dict] of [ ['fr', fr], ['en', en], ] as const) { const create = dict['admin.working_copy.create'] const free = (page: Page) => page.getByRole('region', { name: 'Slot free' }) const taken = (page: Page) => page.getByRole('region', { name: 'Slot taken' }) /** Opens the dialog from the keyboard, as the spec's own opener. */ async function openDialog(page: Page) { await free(page).getByRole('button', { name: create }).focus() await page.keyboard.press('Enter') const dialog = page.getByRole('dialog', { name: create }) await expect(dialog).toBeVisible() return dialog } /** What the focus is on: the slug field by its id, else its accessible name. */ async function focusStep(page: Page) { const owner = await focusOwner(page) if (owner !== 'dialog') return owner const onField = await page.evaluate( (id) => document.activeElement?.id === id, SLUG_FIELD, ) return onField ? SLUG_FIELD : await focusedName(page) } async function walk(page: Page, key: 'Tab' | 'Shift+Tab', count: number) { const visited: string[] = [] for (let i = 0; i < count; i++) { await page.keyboard.press(key) visited.push(await focusStep(page)) } return visited } test.describe(lang, () => { test.beforeEach(async ({ page }) => { await page.goto(`/e2e/harness/working-copy.html?lang=${lang}`) await expect( page.getByRole('heading', { name: 'Working copy harness' }), ).toBeVisible() }) test('the page passes axe with both states, no sideways scroll', async ({ page, }) => { await expectAccessible(page, `the working copy page (${lang})`) await expectNoHorizontalOverflow(page, `the working copy page (${lang})`) }) test('slot taken: the button is disabled and gives its reason', async ({ page, }) => { const button = taken(page).getByRole('button', { name: create }) await expect(button).toBeDisabled() await expect(button).toHaveAccessibleDescription( dict['admin.working_copy.active'], ) await expect(free(page).getByRole('button')).toBeEnabled() }) test('opens from the keyboard, named, described, slug proposed', async ({ page, }) => { const dialog = await openDialog(page) await expect(dialog).toHaveAccessibleDescription( dict['admin.working_copy.body'], ) await expect(dialog.locator(`#${SLUG_FIELD}`)).toHaveValue( 'faunosteo-cheval-copie-de-travail', ) expect(await focusOwner(page)).toBe('dialog') expect(await focusedName(page)).toBe(dict['common.close']) }) // Past either end the focus visits the browser's interface (one step) and // comes back into the dialog; it never reaches the page behind. test('Tab and Shift+Tab cycle through the dialog, never the page', async ({ page, }) => { await openDialog(page) const order = [ SLUG_FIELD, dict['common.cancel'], dict['admin.working_copy.confirm'], 'browser', dict['common.close'], ] expect(await walk(page, 'Tab', 10)).toEqual([...order, ...order]) const back = [ 'browser', dict['admin.working_copy.confirm'], dict['common.cancel'], SLUG_FIELD, dict['common.close'], ] expect(await walk(page, 'Shift+Tab', 10)).toEqual([...back, ...back]) }) test('the open dialog passes axe and fits the viewport', async ({ page, }) => { const dialog = await openDialog(page) await expectAccessible(page, `the working copy dialog (${lang})`) await expectNoHorizontalOverflow( page, `the working copy dialog (${lang})`, ) const panel = dialog.locator('header').locator('..') const box = await panel.boundingBox() const viewport = page.viewportSize() expect( box && viewport && box.x >= 0 && box.x + box.width <= viewport.width && box.y >= 0 && box.y + box.height <= viewport.height, 'the panel inside the viewport', ).toBe(true) const { scrollWidth, clientWidth } = await panel.evaluate((p) => ({ scrollWidth: p.scrollWidth, clientWidth: p.clientWidth, })) expect(scrollWidth, 'the panel scrolls sideways').toBeLessThanOrEqual( clientWidth, ) }) test('Escape closes and focus returns to the opener', async ({ page }) => { const dialog = await openDialog(page) await page.keyboard.press('Escape') await expect(dialog).toBeHidden() expect(await focusedName(page)).toBe(create) }) }) }