|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import { expect, test, type Page } from '@playwright/test' |
| 9 | + |
| 10 | +// react-aria appends each announcement as a child of a hidden polite live |
| 11 | +// region shared with toasts. CSS locator because there's a second region for |
| 12 | +// assertive announcements and we only want the messages from this one. |
| 13 | +const announcements = (page: Page) => |
| 14 | + page.locator('[data-live-announcer] [aria-live="polite"] > div') |
| 15 | + |
| 16 | +test('route announcer', async ({ page }) => { |
| 17 | + await page.goto('/projects') |
| 18 | + await page.getByRole('heading', { name: 'Projects' }).waitFor() |
| 19 | + |
| 20 | + // nothing on first load — the browser already announced the page |
| 21 | + await expect(announcements(page)).toHaveCount(0) |
| 22 | + |
| 23 | + // nav by a link inside the content, which unmounts along with the page |
| 24 | + await page.getByRole('link', { name: 'mock-project' }).click() |
| 25 | + await expect(announcements(page)).toHaveText(['Instances, mock-project, Projects']) |
| 26 | + // focus goes to the new page's heading, like (better than) a real page load |
| 27 | + await expect(page.getByRole('heading', { name: 'Instances' })).toBeFocused() |
| 28 | + |
| 29 | + // nav by a sidebar link: the link survives the nav, but focus still moves to |
| 30 | + // the top of the new page |
| 31 | + const disksLink = page |
| 32 | + .getByRole('navigation', { name: 'Sidebar navigation' }) |
| 33 | + .getByRole('link', { name: 'Disks' }) |
| 34 | + await disksLink.click() |
| 35 | + await expect(announcements(page)).toHaveText([ |
| 36 | + 'Instances, mock-project, Projects', |
| 37 | + 'Disks, mock-project, Projects', |
| 38 | + ]) |
| 39 | + await expect(page.getByRole('heading', { name: 'Disks' })).toBeFocused() |
| 40 | + |
| 41 | + // a side modal form is its own route, but it opens on top of the page rather |
| 42 | + // than replacing it, so it doesn't announce or take focus from the dialog |
| 43 | + await page.getByRole('link', { name: 'New Disk' }).click() |
| 44 | + await expect(page.getByRole('dialog', { name: 'Create disk' })).toBeVisible() |
| 45 | + await expect(announcements(page)).toHaveCount(2) |
| 46 | + |
| 47 | + // ...and neither does dismissing it |
| 48 | + await page.getByRole('button', { name: 'Cancel' }).click() |
| 49 | + await expect(page.getByRole('dialog', { name: 'Create disk' })).toBeHidden() |
| 50 | + await expect(announcements(page)).toHaveCount(2) |
| 51 | +}) |
| 52 | + |
| 53 | +// tabs that live in the query param rather than the path aren't navigations as |
| 54 | +// far as the router is concerned, so there's nothing to announce |
| 55 | +test('route announcer ignores query param tabs', async ({ page }) => { |
| 56 | + await page.goto('/system/utilization') |
| 57 | + await page.getByRole('tab', { name: 'Metrics' }).click() |
| 58 | + await expect(page).toHaveURL(/tab=metrics/) |
| 59 | + await expect(announcements(page)).toHaveCount(0) |
| 60 | +}) |
| 61 | + |
| 62 | +// VPC tabs, on the other hand, are real routes. Routes like these share a crumb |
| 63 | +// path with their siblings, which is why the announcer keys off the pathname. |
| 64 | +test('route announcer on tab routes', async ({ page }) => { |
| 65 | + await page.goto('/projects/mock-project/vpcs/default/firewall-rules') |
| 66 | + const tab = page.getByRole('tab', { name: 'Routers' }) |
| 67 | + await tab.click() |
| 68 | + await expect(announcements(page)).toHaveText([ |
| 69 | + 'Routers, default, VPCs, mock-project, Projects', |
| 70 | + ]) |
| 71 | + // the tab is still there, so it keeps focus |
| 72 | + await expect(tab).toBeFocused() |
| 73 | +}) |
| 74 | + |
| 75 | +// the image detail side modal isn't a form, but it's still a dialog on top of |
| 76 | +// the list rather than a new page |
| 77 | +test('route announcer ignores detail side modals', async ({ page }) => { |
| 78 | + await page.goto('/images') |
| 79 | + await page.getByRole('link', { name: 'ubuntu-22-04' }).click() |
| 80 | + await expect(page.getByRole('dialog', { name: 'Image details' })).toBeVisible() |
| 81 | + await expect(announcements(page)).toHaveCount(0) |
| 82 | +}) |
0 commit comments