build.log
Quay lai ghi chu
Testing
Cap nhat 24 thg 1, 2026

Testing React Components Guide

How to test React components effectively using Jest and React Testing Library.

testing
react
jest
rtl

Philosophy Test behavior, not implementation. Write tests from the user's perspective. If you can't describe the test without referencing component internals, rethink the test.

Setup - Jest as the test runner - React Testing Library (RTL) for rendering and querying - user-event for simulating interactions

Query Priority 1. `getByRole` (most accessible) 2. `getByLabelText` (forms) 3. `getByText` (visible text) 4. `getByTestId` (last resort)

Common Patterns - Render, interact, assert - Use `screen.getByRole('button', { name: /submit/i })` - Use `waitFor` for async operations - Mock API calls, not the component itself