@@ -10,26 +10,26 @@ Actionable guidelines for writing tests with React Native Testing Library (RNTL)
1010const result = render (<Component />, options ? ); // Sync in v13
1111` ` `
1212
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 |
21- | ` unmount ()` | Unmount the rendered component |
22- | ` toJSON ()` | Get JSON representation for snapshots |
23- | ` debug (options ? )` | Print the component tree to console |
24- | ` root ` | Root element of the rendered component |
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 |
21+ | ` unmount ()` | Unmount the rendered component |
22+ | ` toJSON ()` | Get JSON representation for snapshots |
23+ | ` debug (options ? )` | Print the component tree to console |
24+ | ` root ` | Root element of the rendered component |
2525
2626### screen
2727
2828**Prefer ` screen ` ** over destructuring from ` render ()` . Provides all query methods after ` render ()` is called.
2929
3030` ` ` tsx
3131render (<Component />);
32- screen .getByRole (' button' ); // Access queries via screen
32+ screen .getByRole (' button' ); // Access queries via screen
3333` ` `
3434
3535### renderHook
@@ -38,16 +38,16 @@ screen.getByRole('button'); // Access queries via screen
3838const { result , rerender , unmount } = renderHook (() => useMyHook (), options ? ); // Sync in v13
3939` ` `
4040
41- | Option | Description |
42- |--------| -------------|
43- | ` initialProps ` | Initial props passed to the hook |
44- | ` wrapper ` | React component to wrap the hook (e.g., providers) |
41+ | Option | Description |
42+ | -------------- | -------------------------------------------------- |
43+ | ` initialProps ` | Initial props passed to the hook |
44+ | ` wrapper ` | React component to wrap the hook (e.g., providers) |
4545
46- | Return | Description |
47- |--------| -------------|
48- | ` result .current ` | Current return value of the hook |
49- | ` rerender (props ? )` | Re-render hook with new props |
50- | ` unmount ()` | Unmount the hook |
46+ | Return | Description |
47+ | ------------------ | -------------------------------- |
48+ | ` result .current ` | Current return value of the hook |
49+ | ` rerender (props ? )` | Re-render hook with new props |
50+ | ` unmount ()` | Unmount the hook |
5151
5252## Query Selection
5353
@@ -67,26 +67,26 @@ const { result, rerender, unmount } = renderHook(() => useMyHook(), options?);
6767
6868## Jest Matchers Reference
6969
70- | Matcher | Description |
71- |---------| -------------|
72- | ` toBeOnTheScreen ()` | Element is present in the element tree |
73- | ` toBeVisible ()` | Element is visible (checks style, ` aria - hidden ` , ` accessibilityElementsHidden ` , ancestors) |
74- | ` toBeEmptyElement ()` | Element has no children or text content |
75- | ` toContainElement (element )` | Element contains another element |
76- | ` toBeEnabled ()` | Element is not disabled (checks ` aria - disabled ` , ` accessibilityState ` , ancestors) |
77- | ` toBeDisabled ()` | Element has ` aria - disabled ` or ` accessibilityState = {{ disabled : true }}` (checks ancestors) |
78- | ` toBeBusy ()` | Element has ` aria - busy ` or ` accessibilityState = {{ busy : true }}` |
79- | ` toBeChecked ()` | Element has ` aria - checked ` or ` accessibilityState = {{ checked : true }}` |
80- | ` toBePartiallyChecked ()` | Element has ` aria - checked = " mixed" ` or ` accessibilityState = {{ checked : ' mixed' }}` |
81- | ` toBeSelected ()` | Element has ` aria - selected ` or ` accessibilityState = {{ selected : true }}` |
82- | ` toBeExpanded ()` | Element has ` aria - expanded ` or ` accessibilityState = {{ expanded : true }}` |
83- | ` toBeCollapsed ()` | Element has ` aria - expanded = {false }` or ` accessibilityState = {{ expanded : false }}` |
84- | ` toHaveTextContent (text )` | Element has matching text content |
85- | ` toHaveDisplayValue (value )` | TextInput has matching display value |
86- | ` toHaveAccessibleName (name ? )` | Element has matching ` aria - label ` , ` accessibilityLabel ` , or text content |
87- | ` toHaveAccessibilityValue (value )` | Element has matching ` aria - value * ` or ` accessibilityValue ` |
88- | ` toHaveStyle (style )` | Element has matching style |
89- | ` toHaveProp (name , value ? )` | Element has prop (use semantic matchers when possible) |
70+ | Matcher | Description |
71+ | --------------------------------- | ------------------------------------------------------------------------------------------- |
72+ | ` toBeOnTheScreen ()` | Element is present in the element tree |
73+ | ` toBeVisible ()` | Element is visible (checks style, ` aria - hidden ` , ` accessibilityElementsHidden ` , ancestors) |
74+ | ` toBeEmptyElement ()` | Element has no children or text content |
75+ | ` toContainElement (element )` | Element contains another element |
76+ | ` toBeEnabled ()` | Element is not disabled (checks ` aria - disabled ` , ` accessibilityState ` , ancestors) |
77+ | ` toBeDisabled ()` | Element has ` aria - disabled ` or ` accessibilityState = {{ disabled : true }}` (checks ancestors) |
78+ | ` toBeBusy ()` | Element has ` aria - busy ` or ` accessibilityState = {{ busy : true }}` |
79+ | ` toBeChecked ()` | Element has ` aria - checked ` or ` accessibilityState = {{ checked : true }}` |
80+ | ` toBePartiallyChecked ()` | Element has ` aria - checked = " mixed" ` or ` accessibilityState = {{ checked : ' mixed' }}` |
81+ | ` toBeSelected ()` | Element has ` aria - selected ` or ` accessibilityState = {{ selected : true }}` |
82+ | ` toBeExpanded ()` | Element has ` aria - expanded ` or ` accessibilityState = {{ expanded : true }}` |
83+ | ` toBeCollapsed ()` | Element has ` aria - expanded = {false }` or ` accessibilityState = {{ expanded : false }}` |
84+ | ` toHaveTextContent (text )` | Element has matching text content |
85+ | ` toHaveDisplayValue (value )` | TextInput has matching display value |
86+ | ` toHaveAccessibleName (name ? )` | Element has matching ` aria - label ` , ` accessibilityLabel ` , or text content |
87+ | ` toHaveAccessibilityValue (value )` | Element has matching ` aria - value * ` or ` accessibilityValue ` |
88+ | ` toHaveStyle (style )` | Element has matching style |
89+ | ` toHaveProp (name , value ? )` | Element has prop (use semantic matchers when possible) |
9090
9191## User Interactions
9292
@@ -98,25 +98,25 @@ const { result, rerender, unmount } = renderHook(() => useMyHook(), options?);
9898const user = userEvent .setup ();
9999` ` `
100100
101- | Method | Description |
102- |--------| -------------|
103- | ` await user .press (element )` | Press an element (triggers ` pressIn ` , ` pressOut ` , ` press ` ) |
104- | ` await user .longPress (element , options ? )` | Long press with optional ` { duration }` |
101+ | Method | Description |
102+ | ------------------------------------------ | ----------------------------------------------------------------------------------- |
103+ | ` await user .press (element )` | Press an element (triggers ` pressIn ` , ` pressOut ` , ` press ` ) |
104+ | ` await user .longPress (element , options ? )` | Long press with optional ` { duration }` |
105105| ` await user .type (element , text , options ? )` | Type into TextInput (triggers ` focus ` , ` keyPress ` , ` change ` , ` changeText ` per char) |
106- | ` await user .clear (element )` | Clear TextInput (select all + backspace) |
107- | ` await user .paste (element , text )` | Paste text into TextInput |
108- | ` await user .scrollTo (element , options )` | Scroll a ScrollView with ` { y }` or ` { x }` offset |
106+ | ` await user .clear (element )` | Clear TextInput (select all + backspace) |
107+ | ` await user .paste (element , text )` | Paste text into TextInput |
108+ | ` await user .scrollTo (element , options )` | Scroll a ScrollView with ` { y }` or ` { x }` offset |
109109
110110### fireEvent (Low-level, Sync in v13)
111111
112112Use only when ` userEvent ` doesn't support the event or when you need direct control.
113113
114- | Method | Description |
115- |--------| -------------|
116- | ` fireEvent (element , eventName , ... data )` | Fire any event by name |
117- | ` fireEvent .press (element )` | Fire ` onPress ` only (no ` pressIn ` / ` pressOut ` ) |
118- | ` fireEvent .changeText (element , text )` | Fire ` onChangeText ` directly |
119- | ` fireEvent .scroll (element , eventData )` | Fire ` onScroll ` with event data |
114+ | Method | Description |
115+ | ---------------------------------------- | --------------------------------------------- |
116+ | ` fireEvent (element , eventName , ... data )` | Fire any event by name |
117+ | ` fireEvent .press (element )` | Fire ` onPress ` only (no ` pressIn ` / ` pressOut ` ) |
118+ | ` fireEvent .changeText (element , text )` | Fire ` onChangeText ` directly |
119+ | ` fireEvent .scroll (element , eventData )` | Fire ` onScroll ` with event data |
120120
121121## Sync vs Async APIs (v13)
122122
@@ -167,17 +167,8 @@ test('user can submit form', async () => {
167167
168168 return (
169169 <View >
170- <TextInput
171- role = " textbox"
172- aria-label = " Name"
173- value = { name }
174- onChangeText = { setName }
175- />
176- <Pressable
177- role = " button"
178- aria-label = " Submit"
179- onPress = { () => setSubmitted (true )}
180- >
170+ <TextInput role = " textbox" aria-label = " Name" value = { name } onChangeText = { setName } />
171+ <Pressable role = " button" aria-label = " Submit" onPress = { () => setSubmitted (true )} >
181172 <Text >Submit</Text >
182173 </Pressable >
183174 { submitted && <Text role = " alert" >Form submitted!</Text >}
@@ -222,7 +213,7 @@ await waitFor(async () => {
222213});
223214
224215// accessibility* props instead of ARIA
225- <Pressable accessibilityRole = " button" accessibilityLabel = " Submit" />
216+ <Pressable accessibilityRole = " button" accessibilityLabel = " Submit" />;
226217
227218// Destructuring from render
228219const { getByText } = render (<Component />);
0 commit comments