Loading Depth Study
Preparing your workspace
React component tests verify component behavior through rendered output, user interaction, and accessible elements.
Good component tests focus on user-visible behavior rather than internal implementation details. A test can render a component, find a button or input, simulate a user action, and check the resulting UI. This approach creates tests that remain useful even when the internal component implementation changes. Testing libraries such as React Testing Library encourage queries based on accessible roles, labels, and visible text.
A login form test can type an email and password, submit the form, and verify that a success or validation message appears.
render(<Button />);
const button = screen.getByRole('button', {
name: 'Start Course'
});
await user.click(button);
expect(onStart).toHaveBeenCalled();The test finds the button through its accessible role and verifies that clicking it triggers the expected behavior.