React in Practice · 22 min · 180 XP

Test behavior, not implementation

Turn a behavior contract into tests that survive refactors and describe the interface a person uses.

A valuable component test describes observable behavior: what is on screen, what a person does, and what changes. It should not care whether the component uses one state value, a reducer, or a custom hook.

jsx
await render(<QuantityPicker stock={2} onChange={record} />);
await click(getByRole("button", { name: "Increase quantity" }));
expect(record).toHaveBeenCalledWith(2);

Query controls by role, accessible name or label. Those queries match how assistive technology understands the interface, so a test failure can reveal a real usability problem. A selector such as .blue-button:nth-child(2) only proves the markup has not moved.

Test boundaries and meaningful states, not every line. For a quantity picker, zero, maximum stock, and the transitions immediately beside them carry more information than repeating the same middle-value click ten times.

Loading your workspace…