Sub topic 1
Why test behavior instead of implementation
Prerequisites
This chapter assumes you know React function components, props, state, and hooks. You should also be comfortable with basic JavaScript, including async/await, and with a test runner such as Jest or Vitest (test, expect, describe). A working knowledge of HTML roles, labels, and form elements helps, because Testing Library finds elements the way assistive technology does.
The core idea
React Testing Library (RTL) is built on one guiding principle: the more your tests resemble the way your software is used, the more confidence they give you.
A user does not call setCount. They do not check that a component has a piece of state named count. They see text, click buttons, and type into fields. RTL asks you to write tests from that point of view.
Tests that check implementation details look at how a component is built: state variable names, internal method calls, child component props, or CSS class names. Tests that check behavior look at what a user can see and do: text on screen, enabled or disabled buttons, and messages after an action.
Two tests for the same component
Here is a counter component.
import { useState } from 'react';
export function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
</div>
);
}A test tied to implementation might use a shallow renderer to read state.count and call click handler directly. A behavior test looks like this:
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Counter } from './Counter';
test('increments the count when the button is clicked', async () => {
const user = userEvent.setup();
render(<Counter />);
expect(screen.getByText('Count: 0')).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'Increment' }));
expect(screen.getByText('Count: 1')).toBeInTheDocument();
});The test finds the button by its role and accessible name, clicks it like a user, and checks the visible text. It never mentions useState. (toBeInTheDocument comes from jest-dom, which the next chapter sets up.)
Why this matters
Consider two kinds of change to the component:
| Change | Implementation-detail test | Behavior test |
|---|---|---|
| Refactor `useState` to `useReducer`, same UI | Breaks (false failure) | Passes |
| Rename the internal `handleClick` function | Breaks (false failure) | Passes |
| Button stops incrementing because of a bug | May still pass (false pass) | Fails |
Implementation tests fail when nothing is wrong for the user, and they can pass when the feature is broken. Both erode trust. Behavior tests survive refactors and fail when the user experience breaks.
How the pieces fit together
Every test in this tutorial follows this shape. Later chapters cover each step: rendering, queries, interaction, and assertions.
What behavior testing does not mean
- It is not a ban on mocks. You still replace network calls or injected callbacks. You test what the component does with them, such as showing an error, not how it calls them internally.
- It does not require a browser. RTL runs in jsdom, a simulated DOM in Node, so tests stay fast.
- It does not test everything through the full app. You can test a single component, as long as you interact through its public surface: props in, rendered output and callbacks out.
A quick check on accessibility
Querying by role and label has a second benefit. If getByRole('button', { name: 'Increment' }) fails, a screen reader user may also be unable to find that button. Your tests then point out accessibility problems, such as missing labels, as well as logic bugs.
Exercise
A test reads a component's internal state variable isOpen to check that a dropdown opened. Name two problems with this approach and say what the test should check instead.
Show answer
First, it breaks when you refactor state (for example, switching to useReducer or renaming the variable) even though users see no change. Second, it can pass while the dropdown is visually broken, because the state may be true but the options may not render. The test should click the dropdown trigger with user-event and assert that the options are visible on screen, for example with screen.getByRole('listbox') or by finding the option text.
Exercise
Which of these is a behavior assertion? (a) expect(wrapper.state('count')).toBe(1) (b) expect(screen.getByText('Count: 1')).toBeInTheDocument() (c) expect(container.querySelector('.counter-value')).toBeTruthy()
Show answer
(b). It checks what the user sees. Option (a) reads internal state, and option (c) depends on a CSS class name that can change without affecting users.