Chapter 1 of 4

What to Test

The guiding principle behind React Testing Library, and what not to bother with.

React Testing Library is built on one idea, stated by its author: the more your tests resemble the way your software is used, the more confidence they give you.

In practice that means testing what a user can observe - text on the screen, what happens when they click - rather than the internals of the component.

Test behaviour, not implementation

// Implementation: breaks the moment you rename state or restructure
expect(wrapper.state("isOpen")).toBe(true);
expect(wrapper.find("Dropdown").props().items).toHaveLength(3);

// Behaviour: survives any refactor that keeps the UI the same
await user.click(screen.getByRole("button", { name: /options/i }));
expect(screen.getByRole("listbox")).toBeVisible();
expect(screen.getAllByRole("option")).toHaveLength(3);

The second version would still pass if you swapped useState for useReducer, renamed every variable, or replaced the component with a different implementation entirely. That is the point.

What is worth testing

  • The component renders the right thing for its props.
  • Interactions produce the expected result.
  • Conditional paths: loading, empty, error, success.
  • Accessibility basics: labels, roles, focus behaviour.

What is not

  • Internal state values and hook call counts.
  • That a child component received a particular prop.
  • Exact class names or the DOM structure.
  • Third-party libraries, which have their own tests.