|
| 1 | +--- |
| 2 | +name: react-native-teting-library-v13 |
| 3 | +description: > |
| 4 | + Write tests using React Native Testing Library (RNTL) v13 (`@testing-library/react-native`). |
| 5 | + Use when writing, reviewing, or fixing React Native component tests. |
| 6 | + Covers: render, screen, queries (getBy/getAllBy/queryBy/findBy), Jest matchers, |
| 7 | + userEvent, fireEvent, waitFor, and async patterns. |
| 8 | + Supports both React 18 (sync render) and React 19 compat (renderAsync/fireEventAsync). |
| 9 | + Triggers on: test files for React Native components, RNTL imports, mentions of |
| 10 | + "testing library", "write tests", "component tests", or "RNTL". |
| 11 | +--- |
| 12 | + |
| 13 | +# RNTL v13 Test Writing Guide |
| 14 | + |
| 15 | +## Core Pattern |
| 16 | + |
| 17 | +```tsx |
| 18 | +import { render, screen, userEvent } from '@testing-library/react-native'; |
| 19 | + |
| 20 | +jest.useFakeTimers(); // recommended when using userEvent |
| 21 | + |
| 22 | +test('description', async () => { |
| 23 | + const user = userEvent.setup(); |
| 24 | + render(<Component />); // sync in v13 (React 18) |
| 25 | + |
| 26 | + const button = screen.getByRole('button', { name: 'Submit' }); |
| 27 | + await user.press(button); |
| 28 | + |
| 29 | + expect(screen.getByText('Done')).toBeOnTheScreen(); |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +## Query Priority |
| 34 | + |
| 35 | +Use in this order: `getByRole` > `getByLabelText` > `getByPlaceholderText` > `getByText` > `getByDisplayValue` > `getByTestId` (last resort). |
| 36 | + |
| 37 | +## Query Variants |
| 38 | + |
| 39 | +| Variant | Use case | Returns | Async | |
| 40 | +|-------------|---------------------------|---------------------------------|-------| |
| 41 | +| `getBy*` | Element must exist | `ReactTestInstance` (throws) | No | |
| 42 | +| `getAllBy*` | Multiple must exist | `ReactTestInstance[]` (throws) | No | |
| 43 | +| `queryBy*` | Check non-existence ONLY | `ReactTestInstance \| null` | No | |
| 44 | +| `queryAllBy*`| Count elements | `ReactTestInstance[]` | No | |
| 45 | +| `findBy*` | Wait for element | `Promise<ReactTestInstance>` | Yes | |
| 46 | +| `findAllBy*` | Wait for multiple | `Promise<ReactTestInstance[]>` | Yes | |
| 47 | + |
| 48 | +## Interactions |
| 49 | + |
| 50 | +Prefer `userEvent` over `fireEvent`. userEvent is always async. |
| 51 | + |
| 52 | +```tsx |
| 53 | +const user = userEvent.setup(); |
| 54 | +await user.press(element); // full press sequence |
| 55 | +await user.longPress(element, { duration: 800 }); // long press |
| 56 | +await user.type(textInput, 'Hello'); // char-by-char typing |
| 57 | +await user.clear(textInput); // clear TextInput |
| 58 | +await user.paste(textInput, 'pasted text'); // paste into TextInput |
| 59 | +await user.scrollTo(scrollView, { y: 100 }); // scroll |
| 60 | +``` |
| 61 | + |
| 62 | +Use `fireEvent` only when `userEvent` doesn't support the event: |
| 63 | +```tsx |
| 64 | +fireEvent.press(element); // sync, onPress only |
| 65 | +fireEvent.changeText(textInput, 'new text'); // sync, onChangeText only |
| 66 | +fireEvent(element, 'blur'); // any event by name |
| 67 | +``` |
| 68 | + |
| 69 | +## Assertions (Jest Matchers) |
| 70 | + |
| 71 | +Available automatically with any `@testing-library/react-native` import. |
| 72 | + |
| 73 | +| Matcher | Use for | |
| 74 | +|---------------------------------|----------------------------------------------| |
| 75 | +| `toBeOnTheScreen()` | Element exists in tree | |
| 76 | +| `toBeVisible()` | Element visible (not hidden/display:none) | |
| 77 | +| `toBeEnabled()` / `toBeDisabled()` | Disabled state via `aria-disabled` | |
| 78 | +| `toBeChecked()` / `toBePartiallyChecked()` | Checked state | |
| 79 | +| `toBeSelected()` | Selected state | |
| 80 | +| `toBeExpanded()` / `toBeCollapsed()` | Expanded state | |
| 81 | +| `toBeBusy()` | Busy state | |
| 82 | +| `toHaveTextContent(text)` | Text content match | |
| 83 | +| `toHaveDisplayValue(value)` | TextInput display value | |
| 84 | +| `toHaveAccessibleName(name)` | Accessible name | |
| 85 | +| `toHaveAccessibilityValue(val)` | Accessibility value | |
| 86 | +| `toHaveStyle(style)` | Style match | |
| 87 | +| `toHaveProp(name, value?)` | Prop check (last resort) | |
| 88 | +| `toContainElement(el)` | Contains child element | |
| 89 | +| `toBeEmptyElement()` | No children | |
| 90 | + |
| 91 | +## Rules |
| 92 | + |
| 93 | +1. **Use `screen`** for queries, not destructuring from `render()` |
| 94 | +2. **Use `getByRole` first** with `{ name: '...' }` option |
| 95 | +3. **Use `queryBy*` ONLY** for `.not.toBeOnTheScreen()` checks |
| 96 | +4. **Use `findBy*`** for async elements, NOT `waitFor` + `getBy*` |
| 97 | +5. **Never put side-effects in `waitFor`** (no `fireEvent`/`userEvent` inside) |
| 98 | +6. **One assertion per `waitFor`** |
| 99 | +7. **Never pass empty callbacks to `waitFor`** |
| 100 | +8. **Don't wrap in `act()`** - `render`, `fireEvent`, `userEvent` handle it |
| 101 | +9. **Don't call `cleanup()`** - automatic after each test |
| 102 | +10. **Prefer ARIA props** (`role`, `aria-label`, `aria-disabled`) over legacy `accessibility*` props |
| 103 | +11. **Use RNTL matchers** over raw prop assertions |
| 104 | + |
| 105 | +## React 19 Compatibility (v13.3+) |
| 106 | + |
| 107 | +For React 19 or Suspense, use async variants: |
| 108 | + |
| 109 | +```tsx |
| 110 | +import { renderAsync, screen, fireEventAsync } from '@testing-library/react-native'; |
| 111 | + |
| 112 | +test('async component', async () => { |
| 113 | + await renderAsync(<SuspenseComponent />); |
| 114 | + await fireEventAsync.press(screen.getByRole('button')); |
| 115 | + expect(screen.getByText('Result')).toBeOnTheScreen(); |
| 116 | +}); |
| 117 | +``` |
| 118 | + |
| 119 | +Use `rerenderAsync`/`unmountAsync` instead of `rerender`/`unmount` when using `renderAsync`. |
| 120 | + |
| 121 | +## `*ByRole` Quick Reference |
| 122 | + |
| 123 | +Common roles: `button`, `text`, `heading` (alias: `header`), `searchbox`, `switch`, `checkbox`, `radio`, `img`, `link`, `alert`, `menu`, `menuitem`, `tab`, `tablist`, `progressbar`, `slider`, `spinbutton`, `timer`, `toolbar`. |
| 124 | + |
| 125 | +`getByRole` options: `{ name, disabled, selected, checked, busy, expanded, value: { min, max, now, text } }`. |
| 126 | + |
| 127 | +For `*ByRole` to match, the element must be an accessibility element: |
| 128 | +- `Text`, `TextInput`, `Switch` are by default |
| 129 | +- `View` needs `accessible={true}` (or use `Pressable`/`TouchableOpacity`) |
| 130 | + |
| 131 | +## API Reference |
| 132 | + |
| 133 | +See [references/api-reference.md](references/api-reference.md) for complete API signatures and options for render, screen, queries, userEvent, fireEvent, Jest matchers, waitFor, renderHook, configuration, and accessibility helpers. |
| 134 | + |
| 135 | +## Anti-Patterns Reference |
| 136 | + |
| 137 | +See [references/anti-patterns.md](references/anti-patterns.md) for detailed examples of what NOT to do. |
| 138 | + |
| 139 | +## waitFor |
| 140 | + |
| 141 | +```tsx |
| 142 | +// Correct: action first, then wait for result |
| 143 | +fireEvent.press(button); |
| 144 | +await waitFor(() => { |
| 145 | + expect(screen.getByText('Result')).toBeOnTheScreen(); |
| 146 | +}); |
| 147 | + |
| 148 | +// Better: use findBy* instead |
| 149 | +fireEvent.press(button); |
| 150 | +expect(await screen.findByText('Result')).toBeOnTheScreen(); |
| 151 | +``` |
| 152 | + |
| 153 | +Options: `waitFor(cb, { timeout: 1000, interval: 50 })`. Works with Jest fake timers automatically. |
| 154 | + |
| 155 | +## Fake Timers |
| 156 | + |
| 157 | +Recommended with `userEvent` (press/longPress involve real durations): |
| 158 | + |
| 159 | +```tsx |
| 160 | +jest.useFakeTimers(); |
| 161 | + |
| 162 | +test('with fake timers', async () => { |
| 163 | + const user = userEvent.setup(); |
| 164 | + render(<Component />); |
| 165 | + await user.press(screen.getByRole('button')); |
| 166 | + // ... |
| 167 | +}); |
| 168 | +``` |
| 169 | + |
| 170 | +## Custom Render |
| 171 | + |
| 172 | +Wrap providers using `wrapper` option: |
| 173 | + |
| 174 | +```tsx |
| 175 | +function renderWithProviders(ui: React.ReactElement) { |
| 176 | + return render(ui, { |
| 177 | + wrapper: ({ children }) => ( |
| 178 | + <ThemeProvider><AuthProvider>{children}</AuthProvider></ThemeProvider> |
| 179 | + ), |
| 180 | + }); |
| 181 | +} |
| 182 | +``` |
0 commit comments