import { en } from '@/i18n/en' import { fr } from '@/i18n/fr' import type { Page } from '@playwright/test' import { expect, expectAccessible, expectNoHorizontalOverflow, focusOwner, focusedName, test, } from '../support/browser' import { createAccount, dropLateReplays, routeToSandbox, } from '../support/sandbox' import { signIn } from '../support/shell' // The authenticated shell on the isolated stack: sign-in, dashboard, the // account photo confirmation and the mobile drawer, through the shared modal. const LANGUAGES = [ ['fr', fr], ['en', en], ] as const /** Focus steps of `count` Tab presses, the browser's interface written « browser ». */ async function tabWalk(page: Page, count: number): Promise { const steps: string[] = [] for (let i = 0; i < count; i++) { await page.keyboard.press('Tab') const owner = await focusOwner(page) steps.push(owner === 'dialog' ? await focusedName(page) : owner) } return steps } for (const [locale, dict] of LANGUAGES) { test.describe(`shell (${locale})`, () => { test.use({ locale: locale === 'fr' ? 'fr-FR' : 'en-GB' }) test.beforeEach(async ({ context }) => { await routeToSandbox(context) }) test.afterEach(async ({ context }) => { await dropLateReplays(context) }) test('sign-in, dashboard, then the photo confirmation of the account', async ({ page, requests, }) => { const account = await createAccount(locale, true) requests.forbid(account.email, account.password, account.displayName) await signIn(page, account, dict) await expectAccessible(page, 'dashboard') await expectNoHorizontalOverflow(page, 'dashboard') await page.getByTestId('topbar-account').click() await expect(page).toHaveURL(/\/app\/account$/) const remove = page.getByRole('button', { name: dict['account.avatar.remove'], }) await expect(remove).toBeVisible() await expect( page .getByTestId('avatar-section') .getByRole('img', { name: dict['account.avatar.alt'] }), ).toBeVisible() await expectAccessible(page, 'account') await expectNoHorizontalOverflow(page, 'account') await remove.focus() await page.keyboard.press('Enter') const dialog = page.getByRole('dialog', { name: dict['account.avatar.remove_title'], }) await expect(dialog).toBeVisible() await expect(dialog).toHaveAccessibleDescription( dict['account.avatar.remove_body'], ) expect(await focusedName(page)).toBe(dict['common.close']) expect(await tabWalk(page, 4)).toEqual([ dict['common.cancel'], dict['account.avatar.remove_confirm'], 'browser', dict['common.close'], ]) await expectAccessible(page, 'the photo confirmation') await expectNoHorizontalOverflow(page, 'the photo confirmation') await page.keyboard.press('Escape') await expect(dialog).toBeHidden() await expect(remove).toBeFocused() // A slow removal: the confirmation must stay open until it settles, or the // focus would return to a button about to vanish. let settled = false await page.route('**/rest/v1/rpc/remove_avatar', async (route) => { await new Promise((resolve) => setTimeout(resolve, 800)) settled = true await route.fallback() }) await page.keyboard.press('Enter') await expect(dialog).toBeVisible() await dialog .getByRole('button', { name: dict['account.avatar.remove_confirm'], exact: true, }) .focus() await page.keyboard.press('Enter') await page.waitForTimeout(400) expect(settled).toBe(false) await expect(dialog).toBeVisible() await page.keyboard.press('Escape') await expect(dialog).toBeVisible() await expect(dialog).toBeHidden() expect(settled).toBe(true) await expect(page.getByText(dict['account.avatar.removed'])).toBeVisible() await expect(remove).toHaveCount(0) // The visible button, not the file input it opens (out of the tab order). const change = page .getByTestId('avatar-section') .locator('button', { hasText: dict['account.avatar.change'] }) await expect(change).toBeFocused() }) test('mobile drawer: contained focus, Escape, return to the menu button @phone', async ({ page, requests, }) => { const account = await createAccount(locale, false) requests.forbid(account.email, account.password, account.displayName) await signIn(page, account, dict) const menu = page.getByRole('button', { name: dict['shell.open_menu'] }) await menu.focus() await page.keyboard.press('Enter') const drawer = page.getByRole('dialog', { name: dict['shell.nav_label'] }) await expect(drawer).toBeVisible() expect(await focusOwner(page)).toBe('dialog') const steps = await tabWalk(page, 12) expect( steps.filter((step) => step.startsWith('outside') || step === 'body'), ).toEqual([]) expect(steps).toContain(dict['shell.sign_out']) expect(steps).toContain('browser') await expectAccessible(page, 'the open drawer') await expectNoHorizontalOverflow(page, 'the open drawer') await page.keyboard.press('Escape') await expect(drawer).toBeHidden() await expect(menu).toBeFocused() await page.keyboard.press('Enter') await drawer .getByRole('link', { name: dict['shell.nav.account'] }) .click() await expect(drawer).toBeHidden() await expect(page).toHaveURL(/\/app\/account$/) // Widening past the sidebar breakpoint closes the drawer and frees the page. await menu.click() await expect(drawer).toBeVisible() await page.setViewportSize({ width: 1280, height: 800 }) await expect(drawer).toBeHidden() await page .getByRole('link', { name: dict['shell.nav.dashboard'] }) .first() .click() await expect(page).toHaveURL(/\/app\/?$/) }) }) }