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 D13 contract of components/ui/Dialog on its test page (e2e/harness/dialog.tsx). async function openScenario(page: Page, opener: string, title: string) { await page.getByRole('button', { name: opener }).focus() await page.keyboard.press('Enter') const dialog = page.getByRole('dialog', { name: title }) await expect(dialog).toBeVisible() return dialog } /** Names of the elements the focus visits over `count` presses of `key`. */ async function focusWalk(page: Page, key: 'Tab' | 'Shift+Tab', count: number) { const visited: { owner: string; name: string }[] = [] for (let i = 0; i < count; i++) { await page.keyboard.press(key) visited.push({ owner: await focusOwner(page), name: await focusedName(page), }) } return visited } test.beforeEach(async ({ page }) => { await page.goto('/e2e/harness/dialog.html') await expect( page.getByRole('heading', { name: 'Dialog harness' }), ).toBeVisible() }) test('is a named, described modal dialog and moves focus into it', async ({ page, }) => { const dialog = await openScenario(page, 'Open basic', 'Basic dialog') await expect(dialog).toHaveAccessibleDescription('Three controls follow.') expect( await dialog.evaluate((d) => d.tagName === 'DIALOG' && d.matches(':modal')), ).toBe(true) expect(await focusOwner(page)).toBe('dialog') expect(await focusedName(page)).toBe(fr['common.close']) }) // Past either end the focus visits the browser's interface (one step, the page // holds no focus) and comes back into the dialog; it never reaches the page. test('Tab and Shift+Tab cycle through the dialog, never the page', async ({ page, }) => { await openScenario(page, 'Open basic', 'Basic dialog') const step = (f: { owner: string; name: string }) => f.owner === 'dialog' ? f.name : f.owner const forward = await focusWalk(page, 'Tab', 10) expect(forward.map(step)).toEqual([ 'First', 'Second', 'Done', 'browser', fr['common.close'], 'First', 'Second', 'Done', 'browser', fr['common.close'], ]) const backward = await focusWalk(page, 'Shift+Tab', 10) expect(backward.map(step)).toEqual([ 'browser', 'Done', 'Second', 'First', fr['common.close'], 'browser', 'Done', 'Second', 'First', fr['common.close'], ]) }) test('the page behind is inert: no focus, no click, no scroll', async ({ page, }) => { const dialog = await openScenario(page, 'Open guarded', 'Guarded dialog') const background = page.getByRole('button', { name: 'Background action', includeHidden: true, }) const box = await background.boundingBox() if (!box) throw new Error('background button not laid out') await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2) await background.evaluate((b) => (b as HTMLElement).focus()) expect(await focusOwner(page)).toBe('dialog') await page.mouse.move(box.x + 5, box.y + 5) await page.mouse.wheel(0, 800) await page.waitForTimeout(200) expect(await page.evaluate(() => window.scrollY)).toBe(0) await expect(dialog).toBeVisible() await expect(page.getByTestId('background-clicks')).toHaveText('0') }) test('Escape closes when allowed and focus returns to the opener', async ({ page, }) => { const dialog = await openScenario(page, 'Open basic', 'Basic dialog') await page.keyboard.press('Escape') await expect(dialog).toBeHidden() expect(await focusedName(page)).toBe('Open basic') await expect( page.getByRole('button', { name: 'Background action' }), ).toBeEnabled() }) test('Escape is only a request: refused twice, the dialog stays open', async ({ page, }) => { const dialog = await openScenario(page, 'Open guarded', 'Guarded dialog') await page.keyboard.press('Escape') await page.keyboard.press('Escape') await expect(dialog).toBeVisible() expect(await dialog.evaluate((d) => (d as HTMLDialogElement).open)).toBe(true) expect(await focusOwner(page)).toBe('dialog') await page.getByRole('checkbox', { name: 'Busy' }).uncheck() await page.keyboard.press('Escape') await expect(dialog).toBeHidden() expect(await focusedName(page)).toBe('Open guarded') }) test('the close button closes from the keyboard and restores focus', async ({ page, }) => { const dialog = await openScenario(page, 'Open basic', 'Basic dialog') expect(await focusedName(page)).toBe(fr['common.close']) await page.keyboard.press('Enter') await expect(dialog).toBeHidden() expect(await focusedName(page)).toBe('Open basic') }) test('an opener gone on close hands focus to its container’s first control', async ({ page, }) => { const dialog = await openScenario(page, 'Remove item', 'Remove the item?') await page.getByRole('button', { name: 'Confirm removal' }).click() await expect(dialog).toBeHidden() await expect(page.getByRole('button', { name: 'Remove item' })).toHaveCount(0) expect(await focusedName(page)).toBe('Add item') }) test('long content scrolls inside the panel, the page stays put', async ({ page, }) => { const dialog = await openScenario(page, 'Open long', 'Long dialog') const panel = dialog.locator('header').locator('..') const { scrollHeight, clientHeight } = await panel.evaluate((p) => ({ scrollHeight: p.scrollHeight, clientHeight: p.clientHeight, })) expect(scrollHeight).toBeGreaterThan(clientHeight) expect(await focusedName(page)).toBe(fr['common.close']) await page.keyboard.press('Tab') const end = page.getByRole('button', { name: 'End of content' }) await expect(end).toBeFocused() await expect(end).toBeInViewport() expect(await page.evaluate(() => window.scrollY)).toBe(0) const box = await panel.boundingBox() const viewport = page.viewportSize() expect( box && viewport && box.y >= 0 && box.y + box.height <= viewport.height, ).toBe(true) }) test('fits the viewport without horizontal page scroll', async ({ page }) => { await expectNoHorizontalOverflow(page, 'the page') for (const [opener, title] of [ ['Open basic', 'Basic dialog'], ['Open long', 'Long dialog'], ]) { const dialog = await openScenario(page, opener, title) const box = await dialog.locator('header').locator('..').boundingBox() const width = page.viewportSize()?.width ?? 0 expect( box && box.x >= 0 && box.x + box.width <= width, `${title} inside the viewport`, ).toBe(true) await expectNoHorizontalOverflow(page, title) await page.keyboard.press('Escape') await expect(dialog).toBeHidden() } }) for (const [lang, dict] of [ ['fr', fr], ['en', en], ] as const) { test(`axe finds no serious violation, close control named in ${lang}`, async ({ page, }) => { await page.goto(`/e2e/harness/dialog.html?lang=${lang}`) await expectAccessible(page, `the harness (${lang})`) await openScenario(page, 'Open basic', 'Basic dialog') await expect( page.getByRole('button', { name: dict['common.close'] }), ).toBeFocused() await expectAccessible(page, `the open dialog (${lang})`) }) }