|
2 | 2 |
|
3 | 3 | Actionable guidelines for writing tests with React Native Testing Library (RNTL) v14. |
4 | 4 |
|
| 5 | +## Core APIs |
| 6 | + |
| 7 | +### render |
| 8 | + |
| 9 | +```tsx |
| 10 | +const result = await render(<Component />, options?); |
| 11 | +``` |
| 12 | +
|
| 13 | +| Option | Description | |
| 14 | +|--------|-------------| |
| 15 | +| `wrapper` | React component to wrap the rendered component (e.g., providers) | |
| 16 | +| `createNodeMock` | Function to create mock refs | |
| 17 | +
|
| 18 | +| Return | Description | |
| 19 | +|--------|-------------| |
| 20 | +| `rerender(component)` | Re-render with a new component (async) | |
| 21 | +| `unmount()` | Unmount the rendered component (async) | |
| 22 | +| `toJSON()` | Get JSON representation for snapshots | |
| 23 | +| `debug(options?)` | Print the component tree to console | |
| 24 | +| `container` | Root host element of the rendered tree | |
| 25 | +| `root` | First child host element (your component's root) | |
| 26 | +
|
| 27 | +### screen |
| 28 | +
|
| 29 | +**Prefer `screen`** over destructuring from `render()`. Provides all query methods after `render()` is called. |
| 30 | +
|
| 31 | +```tsx |
| 32 | +await render(<Component />); |
| 33 | +screen.getByRole('button'); // Access queries via screen |
| 34 | +``` |
| 35 | +
|
| 36 | +### renderHook |
| 37 | +
|
| 38 | +```tsx |
| 39 | +const { result, rerender, unmount } = await renderHook(() => useMyHook(), options?); |
| 40 | +``` |
| 41 | +
|
| 42 | +| Option | Description | |
| 43 | +|--------|-------------| |
| 44 | +| `initialProps` | Initial props passed to the hook | |
| 45 | +| `wrapper` | React component to wrap the hook (e.g., providers) | |
| 46 | +
|
| 47 | +| Return | Description | |
| 48 | +|--------|-------------| |
| 49 | +| `result.current` | Current return value of the hook | |
| 50 | +| `rerender(props?)` | Re-render hook with new props (async) | |
| 51 | +| `unmount()` | Unmount the hook (async) | |
| 52 | +
|
5 | 53 | ## Query Selection |
6 | 54 |
|
7 | 55 | - **Prefer `getByRole`** as first choice for querying elements |
|
0 commit comments