Sub topic 14
Testing keyboard and screen reader behavior
This chapter assumes you can write React components, have Vitest and React Testing Library installed, and know the Radix Dialog from earlier chapters. Radix handles focus and ARIA for you, but your styling, asChild usage, and edits to copied components can still break it. Tests catch that.
What to test
Automated tests cover the contract that assistive technology depends on: roles, accessible names, focus, and key handling. They do not replace listening to a real screen reader.
| Behavior | How to check it |
|---|---|
| The element has the right role | `getByRole("dialog")` |
| The element has an accessible name | `getByRole("button", { name: "Delete item" })` |
| Keys work | `user.keyboard("{Enter}")`, `user.tab()` |
| Focus moves and returns | `toHaveFocus()` |
| Description is linked | `toHaveAccessibleDescription()` |
Queries by role are the main tool. If getByRole cannot find your element, a screen reader probably cannot identify it either.
Testing the dialog
This test drives the ConfirmDialog from the Radix chapter using only the keyboard. It checks that the dialog has a name and description, and that Escape closes it and returns focus to the trigger.
import "@testing-library/jest-dom/vitest"
import { render, screen, waitFor } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { describe, expect, it } from "vitest"
import { ConfirmDialog } from "./ConfirmDialog"
describe("ConfirmDialog", () => {
it("opens with the keyboard and returns focus on Escape", async () => {
const user = userEvent.setup()
render(<ConfirmDialog />)
await user.tab()
const trigger = screen.getByRole("button", { name: "Delete item" })
expect(trigger).toHaveFocus()
await user.keyboard("{Enter}")
const dialog = await screen.findByRole("dialog", {
name: "Delete this item?",
})
expect(dialog).toHaveAccessibleDescription("This action cannot be undone.")
await user.keyboard("{Escape}")
await waitFor(() =>
expect(screen.queryByRole("dialog")).not.toBeInTheDocument()
)
await waitFor(() => expect(trigger).toHaveFocus())
})
})Use userEvent rather than fireEvent. It dispatches the full sequence of events a real key press produces, so Radix's handlers run as they would in a browser.
Adding an automated audit
The jest-axe package runs the axe rule set on rendered markup. It finds missing labels, invalid ARIA attributes, and similar faults. Call axe(container) on the rendered output and assert with toHaveNoViolations(). Axe finds roughly the machine-checkable portion of problems. It cannot tell you whether focus order makes sense or whether a label is meaningful.
Common failures in copied components
- Icon-only buttons with no name. Add
aria-labelor a visually hidden text span. The role query withnamefails until you do. - Removed focus styles. Stripping
focus-visible:ring-2while customizing makes keyboard position invisible. Tests will not notice, so check by tabbing through the page. asChildon a non-interactive element. Rendering adivas a trigger loses button semantics and Enter or Space handling. Pass a realaorbutton.- Dialog without a title. Radix warns in the console when
Dialog.Titleis missing. Treat that warning as a failing test.
Manual pass
Before shipping a new screen, do a short manual check:
- Unplug the mouse and complete the main task with Tab, Shift+Tab, Enter, Space, Escape, and arrow keys.
- Confirm that the focus ring is always visible and that focus never lands on hidden content.
- Turn on VoiceOver (macOS) or NVDA (Windows) and listen to each control. Each one should announce a name, a role, and its state.
Exercise
A test uses screen.getByRole("button", { name: "Save" }) and fails, although a visible pencil icon button exists on the page. What is the likely cause, and how do you fix it?
Show answer
The button has no accessible name, because it contains only an icon. Add aria-label="Save" to the button, or include visually hidden text inside it. Then the role query with the name matches.