Close the earlier lessons and work from the brief. This is Day 5's combobox, with the same app around it.
npm install and npm run dev. The brief
Download the project, set it up as in the first lesson, and write
src/Combobox.test.jsx. Render Combobox on its own, with your own options
and an onSelect you can check. Cover:
- typing filters the options, and the list is expanded;
- Down and Up move the active option,
aria-activedescendantpoints at it, and focus stays in the input; - Enter chooses the active option, calls
onSelectwith it, and closes the list; - Escape closes the list and keeps the text;
- clicking an option chooses it, and focus stays in the input.
Then prove two of your tests can fail: delete
onMouseDown={(e) => e.preventDefault()} from Combobox.jsx, and check a test
fails; then put it back.
Hint 1: keys
await user.keyboard("{ArrowDown}{Enter}") presses keys on whatever has
focus. After user.type(input, ...), that's the input.
Hint 2: the active option
The active option has aria-selected="true", so
screen.getByRole("option", { selected: true }) finds it. Compare its id
with the input's aria-activedescendant.
Hint 3: a setup function
Five tests render the same thing. A setup() that renders and returns
{ user, onSelect, input } keeps each test short.
Show one solution
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { expect, test, vi } from "vitest";
import Combobox from "./Combobox.jsx";
const options = ["Selectors", "State machines", "Suspense", "Windowing"];
function setup() {
const onSelect = vi.fn();
const user = userEvent.setup();
render(<Combobox label="Topic" options={options} onSelect={onSelect} />);
return { user, onSelect, input: screen.getByRole("combobox", { name: "Topic" }) };
}
test("typing filters the options", async () => {
const { user, input } = setup();
await user.type(input, "st");
expect(screen.getAllByRole("option").map((o) => o.textContent)).toEqual(["State machines"]);
expect(input.getAttribute("aria-expanded")).toBe("true");
});
test("the arrow keys move the active option, and focus stays in the input", async () => {
const { user, input } = setup();
await user.type(input, "s");
await user.keyboard("{ArrowDown}{ArrowDown}");
const active = screen.getByRole("option", { selected: true });
expect(active.textContent).toBe("State machines");
expect(input.getAttribute("aria-activedescendant")).toBe(active.id);
expect(document.activeElement).toBe(input);
});
test("Enter chooses the active option and closes the list", async () => {
const { user, input, onSelect } = setup();
await user.type(input, "su");
await user.keyboard("{ArrowDown}{Enter}");
expect(onSelect).toHaveBeenCalledWith("Suspense");
expect(input.value).toBe("Suspense");
expect(screen.queryByRole("listbox")).toBeNull();
});
test("Escape closes the list and keeps the text", async () => {
const { user, input, onSelect } = setup();
await user.type(input, "s");
await user.keyboard("{Escape}");
expect(screen.queryByRole("listbox")).toBeNull();
expect(input.value).toBe("s");
expect(onSelect).not.toHaveBeenCalled();
});
test("clicking an option chooses it, and focus stays in the input", async () => {
const { user, input, onSelect } = setup();
await user.type(input, "w");
await user.click(screen.getByRole("option", { name: "Windowing" }));
expect(onSelect).toHaveBeenCalledWith("Windowing");
expect(document.activeElement).toBe(input);
});Without the onMouseDown line, clicking an option chooses it, and focus stays
in the input fails: pressing the mouse moves focus to the option first.
Check yourself
- Why is
result.currentread after each step instead of saved once? - Why does Vitest's fake clock need
shouldAdvanceTimewith Testing Library? - When is mocking a module the right choice, and when is it testing the mocks?
Completion is saved in this browser only. It does not measure mastery.