Repository navigation
feat: Add a11y test tooling, LiveRegion and span support for VisuallyHidden #2615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { act, cleanup, render, screen } from "@testing-library/react"; | ||
| import { StrictMode } from "react"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { LiveRegion } from "@/components/live-region"; | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| cleanup(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe("LiveRegion", () => { | ||
| it("renders a polite, atomic status region", () => { | ||
| render(<LiveRegion message="" />); | ||
| const region = screen.getByRole("status"); | ||
|
|
||
| expect(region.getAttribute("aria-live")).toBe("polite"); | ||
| expect(region.getAttribute("aria-atomic")).toBe("true"); | ||
| }); | ||
|
|
||
| it("does not announce the initial message, even in StrictMode", () => { | ||
| render( | ||
| <StrictMode> | ||
| <LiveRegion message="10 results" /> | ||
| </StrictMode> | ||
| ); | ||
| act(() => { | ||
| vi.runAllTimers(); | ||
| }); | ||
|
|
||
| expect(screen.getByRole("status").textContent).toBe(""); | ||
| }); | ||
|
|
||
| it("announces a changed message only after the delay", () => { | ||
| const { rerender } = render( | ||
| <LiveRegion message="10 results" delay={500} /> | ||
| ); | ||
| rerender(<LiveRegion message="3 results" delay={500} />); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(499); | ||
| }); | ||
| expect(screen.getByRole("status").textContent).toBe(""); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(1); | ||
| }); | ||
| expect(screen.getByRole("status").textContent).toBe("3 results"); | ||
| }); | ||
|
|
||
| it("only announces the last message of a quick succession", () => { | ||
| const { rerender } = render(<LiveRegion message="10 results" />); | ||
| rerender(<LiveRegion message="5 results" />); | ||
| act(() => { | ||
| vi.advanceTimersByTime(200); | ||
| }); | ||
| rerender(<LiveRegion message="1 result" />); | ||
| act(() => { | ||
| vi.runAllTimers(); | ||
| }); | ||
|
|
||
| expect(screen.getByRole("status").textContent).toBe("1 result"); | ||
| }); | ||
|
|
||
| it("announces a message that changes back to the initial one", () => { | ||
| const { rerender } = render(<LiveRegion message="A" />); | ||
| rerender(<LiveRegion message="B" />); | ||
| act(() => { | ||
| vi.runAllTimers(); | ||
| }); | ||
| rerender(<LiveRegion message="A" />); | ||
| act(() => { | ||
| vi.runAllTimers(); | ||
| }); | ||
|
|
||
| expect(screen.getByRole("status").textContent).toBe("A"); | ||
| }); | ||
|
|
||
| it("keeps a pending announcement when the delay changes", () => { | ||
| const { rerender } = render(<LiveRegion message="A" delay={500} />); | ||
| rerender(<LiveRegion message="B" delay={500} />); | ||
| rerender(<LiveRegion message="B" delay={300} />); | ||
| act(() => { | ||
| vi.advanceTimersByTime(300); | ||
| }); | ||
|
|
||
| expect(screen.getByRole("status").textContent).toBe("B"); | ||
| }); | ||
|
|
||
| it("cancels a pending announcement on unmount", () => { | ||
| const { rerender, unmount } = render(<LiveRegion message="A" />); | ||
| rerender(<LiveRegion message="B" />); | ||
| unmount(); | ||
|
|
||
| expect(vi.getTimerCount()).toBe(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { useEffect, useRef, useState } from "react"; | ||
|
|
||
| import { VisuallyHidden } from "@/components/visually-hidden"; | ||
|
|
||
| const DEFAULT_DELAY_MS = 500; | ||
|
|
||
| export const LiveRegion = ({ | ||
| message, | ||
| delay = DEFAULT_DELAY_MS, | ||
| }: { | ||
| message: string; | ||
| delay?: number; | ||
| }) => { | ||
| const announcement = useDebouncedAnnouncement({ message, delay }); | ||
|
|
||
| return ( | ||
| <VisuallyHidden | ||
| component="span" | ||
| role="status" | ||
| aria-live="polite" | ||
| aria-atomic="true" | ||
| > | ||
| {announcement} | ||
| </VisuallyHidden> | ||
| ); | ||
| }; | ||
|
|
||
| const useDebouncedAnnouncement = ({ | ||
| message, | ||
| delay, | ||
| }: { | ||
| message: string; | ||
| delay: number; | ||
| }) => { | ||
| const [announcement, setAnnouncement] = useState(""); | ||
| const initialMessageRef = useRef<string | null>(message); | ||
|
|
||
| useEffect(() => { | ||
| if (message === initialMessageRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| initialMessageRef.current = null; | ||
| const timeout = setTimeout(() => setAnnouncement(message), delay); | ||
|
|
||
| return () => clearTimeout(timeout); | ||
| }, [message, delay]); | ||
|
|
||
| return announcement; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { cleanup, render, screen } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { VisuallyHidden } from "@/components/visually-hidden"; | ||
| import { expectNoAxeViolations } from "@/test/axe"; | ||
|
|
||
| afterEach(cleanup); | ||
|
|
||
| describe("VisuallyHidden", () => { | ||
| it("renders a div by default", () => { | ||
| const { container } = render(<VisuallyHidden>Hidden</VisuallyHidden>); | ||
|
|
||
| expect(container.firstElementChild?.tagName).toBe("DIV"); | ||
| }); | ||
|
|
||
| it("renders a span inside a button and keeps its text in the name", async () => { | ||
| const { container } = render( | ||
| <button type="button"> | ||
| Show more | ||
| <VisuallyHidden component="span">: Canton</VisuallyHidden> | ||
| </button> | ||
| ); | ||
|
|
||
| expect(container.querySelector("button > span")).not.toBeNull(); | ||
| expect( | ||
| screen.getByRole("button", { name: /^Show more ?: Canton$/ }) | ||
| ).toBeTruthy(); | ||
| await expectNoAxeViolations(container); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| import { Box } from "@mui/material"; | ||
| import { Box, BoxProps } from "@mui/material"; | ||
| import { visuallyHidden } from "@mui/utils"; | ||
| import { ReactNode } from "react"; | ||
|
|
||
| export const VisuallyHidden = ({ children }: { children: ReactNode }) => { | ||
| // @ts-ignore - I do not know why CSSProperties do not go directly into sx. It works. | ||
| return <Box sx={visuallyHidden}>{children}</Box>; | ||
| type VisuallyHiddenProps = Omit<BoxProps, "sx" | "style">; | ||
|
|
||
| export const VisuallyHidden = (props: VisuallyHiddenProps) => { | ||
| return <Box {...props} style={visuallyHidden} />; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { Result, TagValue } from "axe-core"; | ||
|
|
||
| export const WCAG_TAGS: TagValue[] = [ | ||
| "wcag2a", | ||
| "wcag2aa", | ||
| "wcag21a", | ||
| "wcag21aa", | ||
| ]; | ||
|
|
||
| export const formatViolation = (violation: Result) => { | ||
| const targets = violation.nodes.map((node) => node.target.join(" ")); | ||
|
|
||
| return `${violation.id}: ${violation.help} (${targets.join(", ")})`; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import axe from "axe-core"; | ||
| import { expect } from "vitest"; | ||
|
|
||
| import { formatViolation, WCAG_TAGS } from "@/test/axe-common"; | ||
|
|
||
| export const expectNoAxeViolations = async (container: Element) => { | ||
| const { violations } = await axe.run(container, { | ||
| runOnly: { type: "tag", values: WCAG_TAGS }, | ||
| rules: { | ||
| // axe can't measure contrast in jsdom (no canvas); the e2e scans in e2e/a11y.ts cover it. | ||
| "color-contrast": { enabled: false }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(violations.map(formatViolation)).toEqual([]); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import AxeBuilder from "@axe-core/playwright"; | ||
| import { expect, Page } from "@playwright/test"; | ||
|
|
||
| import { formatViolation, WCAG_TAGS } from "../app/test/axe-common"; | ||
|
|
||
| export const expectNoAxeViolations = async ({ | ||
| page, | ||
| include, | ||
| }: { | ||
| page: Page; | ||
| include?: string; | ||
| }) => { | ||
| const builder = new AxeBuilder({ page }).withTags(WCAG_TAGS); | ||
|
|
||
| if (include) { | ||
| builder.include(include); | ||
| } | ||
|
|
||
| const { violations } = await builder.analyze(); | ||
|
|
||
| expect(violations.map(formatViolation)).toEqual([]); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the color contrast rule disabled?
It is not disbled in e2e/a11y.ts - so e2e and axe tests may differ. Is this intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking a look @mburri!
Yes, it's intentional – in
jsdom,axe'scolor-contrastrule can't produce a result: it measures colors on acanvas(HTMLCanvasElement.getContext), whichjsdomdoesn't implement. I ran the unit axe specs with the rule enabled: 0 violations, every text node either errored into "incomplete" or wasn't checked at all, and even #ddd on white (1.6:1) wasn’t flagged.Contrast is checked in e2e tests in a real browser, I've added a comment to explain 👍