microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
docs/docusaurus/e2e/skip-link.spec.ts
36lines · modecode
| 1 | import { test, expect } from '@playwright/test'; |
| 2 | import AxeBuilder from '@axe-core/playwright'; |
| 3 | |
| 4 | const WCAG_TAGS = ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa']; |
| 5 | |
| 6 | // WCAG 2.4.1 Bypass Blocks: the "Skip to main content" link must be |
| 7 | // keyboard-reachable and move focus to the main content region. |
| 8 | test.describe('Skip-to-content link', () => { |
| 9 | test('is reachable by keyboard and moves focus to main content', async ({ page }) => { |
| 10 | await page.goto('/hve-core/'); |
| 11 | |
| 12 | // The skip link is the first focusable element in the DOM. |
| 13 | await page.keyboard.press('Tab'); |
| 14 | |
| 15 | const skipLink = page.getByRole('link', { name: /skip to main content/i }); |
| 16 | await expect(skipLink).toBeFocused(); |
| 17 | |
| 18 | await skipLink.press('Enter'); |
| 19 | |
| 20 | // Activating the bypass link targets the main-content region. Docusaurus |
| 21 | // manages focus transiently (it sets tabindex="-1", focuses the container, |
| 22 | // then removes the attribute) and does not write a URL hash, so focus |
| 23 | // reverts to <body>. Assert the bypass target is present and visible rather |
| 24 | // than relying on a racy focus or URL check. |
| 25 | await expect(page.locator('#__docusaurus_skipToContent_fallback')).toBeVisible(); |
| 26 | }); |
| 27 | |
| 28 | test('post-activation DOM passes an axe scan', async ({ page }) => { |
| 29 | await page.goto('/hve-core/'); |
| 30 | await page.keyboard.press('Tab'); |
| 31 | await page.getByRole('link', { name: /skip to main content/i }).press('Enter'); |
| 32 | |
| 33 | const results = await new AxeBuilder({ page }).withTags(WCAG_TAGS).analyze(); |
| 34 | expect(results.violations).toEqual([]); |
| 35 | }); |
| 36 | }); |
| 37 | |