@@ -21,7 +21,7 @@ jest.useFakeTimers(); // recommended when using userEvent
2121
2222test (' description' , async () => {
2323 const user = userEvent .setup ();
24- render (<Component />); // sync in v13 (React 18)
24+ render (<Component />); // sync in v13 (React 18)
2525
2626 const button = screen .getByRole (' button' , { name: ' Submit' });
2727 await user .press (button );
@@ -36,57 +36,58 @@ Use in this order: `getByRole` > `getByLabelText` > `getByPlaceholderText` > `ge
3636
3737## Query Variants
3838
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 |
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 |
4747
4848## Interactions
4949
5050Prefer ` userEvent ` over ` fireEvent ` . userEvent is always async.
5151
5252``` tsx
5353const 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
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
6060```
6161
6262Use ` fireEvent ` only when ` userEvent ` doesn't support the event:
63+
6364``` 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
65+ fireEvent .press (element ); // sync, onPress only
66+ fireEvent .changeText (textInput , ' new text' ); // sync, onChangeText only
67+ fireEvent (element , ' blur' ); // any event by name
6768```
6869
6970## Assertions (Jest Matchers)
7071
7172Available automatically with any ` @testing-library/react-native ` import.
7273
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 |
74+ | Matcher | Use for |
75+ | ------------------------------------------ | ----------------------------------------- |
76+ | ` toBeOnTheScreen() ` | Element exists in tree |
77+ | ` toBeVisible() ` | Element visible (not hidden/display: none ) |
78+ | ` toBeEnabled() ` / ` toBeDisabled() ` | Disabled state via ` aria-disabled ` |
79+ | ` toBeChecked() ` / ` toBePartiallyChecked() ` | Checked state |
80+ | ` toBeSelected() ` | Selected state |
81+ | ` toBeExpanded() ` / ` toBeCollapsed() ` | Expanded state |
82+ | ` toBeBusy() ` | Busy state |
83+ | ` toHaveTextContent(text) ` | Text content match |
84+ | ` toHaveDisplayValue(value) ` | TextInput display value |
85+ | ` toHaveAccessibleName(name) ` | Accessible name |
86+ | ` toHaveAccessibilityValue(val) ` | Accessibility value |
87+ | ` toHaveStyle(style) ` | Style match |
88+ | ` toHaveProp(name, value?) ` | Prop check (last resort) |
89+ | ` toContainElement(el) ` | Contains child element |
90+ | ` toBeEmptyElement() ` | No children |
9091
9192## Rules
9293
@@ -125,6 +126,7 @@ Common roles: `button`, `text`, `heading` (alias: `header`), `searchbox`, `switc
125126` getByRole ` options: ` { name, disabled, selected, checked, busy, expanded, value: { min, max, now, text } } ` .
126127
127128For ` *ByRole ` to match, the element must be an accessibility element:
129+
128130- ` Text ` , ` TextInput ` , ` Switch ` are by default
129131- ` View ` needs ` accessible={true} ` (or use ` Pressable ` /` TouchableOpacity ` )
130132
@@ -175,7 +177,9 @@ Wrap providers using `wrapper` option:
175177function renderWithProviders(ui : React .ReactElement ) {
176178 return render (ui , {
177179 wrapper : ({ children }) => (
178- <ThemeProvider ><AuthProvider >{ children } </AuthProvider ></ThemeProvider >
180+ <ThemeProvider >
181+ <AuthProvider >{ children } </AuthProvider >
182+ </ThemeProvider >
179183 ),
180184 });
181185}
0 commit comments