You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/13.x/docs/api/events/fire-event.mdx
+60-26Lines changed: 60 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# Fire Event API
2
2
3
-
```ts
4
-
function fireEvent(element:ReactTestInstance, eventName:string, ...data:unknown[]):void;
5
-
```
3
+
## `fireEvent`{#fire-event}
6
4
7
5
:::note
8
6
For common events like `press` or `type` it's recommended to use [User Event API](docs/api/events/user-event) as it offers
@@ -11,6 +9,10 @@ more realistic event simulation by emitting a sequence of events with proper eve
11
9
Use Fire Event for cases not supported by User Event and for triggering event handlers on composite components.
12
10
:::
13
11
12
+
```ts
13
+
function fireEvent(element:ReactTestInstance, eventName:string, ...data:unknown[]):void;
14
+
```
15
+
14
16
The `fireEvent` API allows you to trigger all kinds of event handlers on both host and composite components. It will try to invoke a single event handler traversing the component tree bottom-up from passed element and trying to find enabled event handler named `onXxx` when `xxx` is the name of the event passed.
15
17
16
18
Unlike User Event, this API does not automatically pass event object to event handler, this is responsibility of the user to construct such object.
@@ -57,14 +59,17 @@ FireEvent exposes convenience methods for common events like: `press`, `changeTe
It is recommended to use the User Event [`press()`](docs/api/events/user-event#press) helper instead as it offers more realistic simulation of press interaction, including pressable support.
66
64
:::
67
65
66
+
```tsx
67
+
fireEvent.press: (
68
+
element: ReactTestInstance,
69
+
...data: Array<any>,
70
+
) =>void
71
+
```
72
+
68
73
Invokes `press` event handler on the element or parent element in the tree.
It is recommended to use the User Event [`type()`](docs/api/events/user-event#type) helper instead as it offers more realistic simulation of text change interaction, including key-by-key typing, element focus, and other editing events.
102
103
:::
103
104
105
+
```tsx
106
+
fireEvent.changeText: (
107
+
element: ReactTestInstance,
108
+
...data: Array<any>,
109
+
) =>void
110
+
```
111
+
104
112
Invokes `changeText` event handler on the element or parent element in the tree.
Prefer using [`user.scrollTo`](docs/api/events/user-event#scrollto) over `fireEvent.scroll` for `ScrollView`, `FlatList`, and `SectionList` components. User Event provides a more realistic event simulation based on React Native runtime behavior.
134
+
:::
135
+
136
+
```tsx
137
+
fireEvent.scroll: (
138
+
element: ReactTestInstance,
139
+
...data: Array<any>,
140
+
) =>void
126
141
```
127
142
128
143
Invokes `scroll` event handler on the element or parent element in the tree.
Prefer using [`user.scrollTo`](docs/api/events/user-event#scrollto) over `fireEvent.scroll` for `ScrollView`, `FlatList`, and `SectionList` components. User Event provides a more realistic event simulation based on React Native runtime behavior.
157
-
158
171
:::
159
172
160
-
## `fireEventAsync`
173
+
## `fireEventAsync`{#fire-event-async}
161
174
162
175
:::info RNTL minimal version
163
176
@@ -173,7 +186,7 @@ async function fireEventAsync(
173
186
):Promise<unknown>;
174
187
```
175
188
176
-
The `fireEventAsync` function is the async version of `fireEvent` designed for working with React 19 and React Suspense. This function uses async `act` function internally to ensure all pending React updates are executed during event handling.
189
+
The `fireEventAsync` function is the async version of [`fireEvent`](#fire-event) designed for working with React 19 and React Suspense. This function uses async `act` function internally to ensure all pending React updates are executed during event handling.
It is recommended to use the User Event [`press()`](docs/api/events/user-event#press) helper instead as it offers more realistic simulation of press interaction, including pressable support.
208
+
:::
209
+
210
+
```tsx
211
+
fireEventAsync.press: (
212
+
element: ReactTestInstance,
213
+
...data: Array<any>,
214
+
) =>Promise<unknown>
195
215
```
196
216
197
-
Async version of `fireEvent.press` designed for React 19 and React Suspense. Use when press event handlers trigger suspense boundaries.
217
+
Async version of [`fireEvent.press`](#press) designed for React 19 and React Suspense. Use when `press` event handlers trigger suspense boundaries.
It is recommended to use the User Event [`type()`](docs/api/events/user-event#type) helper instead as it offers more realistic simulation of text change interaction, including key-by-key typing, element focus, and other editing events.
223
+
:::
224
+
225
+
```tsx
226
+
fireEventAsync.changeText: (
227
+
element: ReactTestInstance,
228
+
...data: Array<any>,
229
+
) =>Promise<unknown>
203
230
```
204
231
205
-
Async version of `fireEvent.changeText` designed for React 19 and React Suspense. Use when changeText event handlers trigger suspense boundaries.
232
+
Async version of [`fireEvent.changeText`](#change-text) designed for React 19 and React Suspense. Use when `changeText` event handlers trigger suspense boundaries.
Prefer using [`user.scrollTo`](docs/api/events/user-event#scrollto) over `fireEventAsync.scroll` for `ScrollView`, `FlatList`, and `SectionList` components. User Event provides a more realistic event simulation based on React Native runtime behavior.
238
+
:::
239
+
240
+
```tsx
241
+
fireEventAsync.scroll: (
242
+
element: ReactTestInstance,
243
+
...data: Array<any>,
244
+
) =>Promise<unknown>
211
245
```
212
246
213
-
Async version of `fireEvent.scroll` designed for React 19 and React Suspense. Use when scroll event handlers trigger suspense boundaries.
247
+
Async version of [`fireEvent.scroll`](#scroll) designed for React 19 and React Suspense. Use when `scroll` event handlers trigger suspense boundaries.
Copy file name to clipboardExpand all lines: website/docs/13.x/docs/api/misc/render-hook.mdx
+17-15Lines changed: 17 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# `renderHook` function
2
2
3
+
## `renderHook`
4
+
3
5
```ts
4
6
function renderHook<Result, Props>(
5
7
hookFn: (props?:Props) =>Result,
@@ -41,15 +43,15 @@ Callback is a function that is called each `render` of the test component. This
41
43
42
44
The `props` passed into the callback will be the `initialProps` provided in the `options` to `renderHook`, unless new props are provided by a subsequent `rerender` call.
43
45
44
-
## `options`
46
+
###`options`
45
47
46
48
A `RenderHookOptions<Props>` object to modify the execution of the `callback` function, containing the following properties:
47
49
48
-
### `initialProps`{#initial-props}
50
+
####`initialProps`{#initial-props}
49
51
50
52
The initial values to pass as `props` to the `callback` function of `renderHook`. The `Props` type is determined by the type passed to or inferred by the `renderHook` call.
51
53
52
-
### `wrapper`
54
+
####`wrapper`
53
55
54
56
A React component to wrap the test component in when rendering. This is usually used to add context providers from `React.createContext` for the hook to access with `useContext`.
55
57
@@ -58,7 +60,7 @@ A React component to wrap the test component in when rendering. This is usually
58
60
Set to `false` to disable concurrent rendering.
59
61
Otherwise, `render` will default to using concurrent rendering used in the React Native New Architecture.
The `renderHook` function returns an object that has the following properties:
72
74
73
-
### `result`
75
+
####`result`
74
76
75
77
The `current` value of the `result` will reflect the latest of whatever is returned from the `callback` passed to `renderHook`. The `Result` type is determined by the type passed to or inferred by the `renderHook` call.
76
78
77
-
### `rerender`
79
+
####`rerender`
78
80
79
81
A function to rerender the test component, causing any hooks to be recalculated. If `newProps` are passed, they will replace the `callback` function's `initialProps` for subsequent rerenders. The `Props` type is determined by the type passed to or inferred by the `renderHook` call.
80
82
81
-
### `unmount`
83
+
####`unmount`
82
84
83
85
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
84
86
85
-
## Examples
87
+
###Examples
86
88
87
89
Here we present some extra examples of using `renderHook` API.
Copy file name to clipboardExpand all lines: website/docs/13.x/docs/api/render.mdx
+11-9Lines changed: 11 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,6 @@
1
-
# `render` function
1
+
# `render` API
2
+
3
+
## `render` function {#render}
2
4
3
5
```jsx
4
6
functionrender(
@@ -20,32 +22,32 @@ test('basic test', () => {
20
22
21
23
> When using React context providers, like Redux Provider, you'll likely want to wrap rendered component with them. In such cases, it's convenient to create your own custom `render` method. [Follow this great guide on how to set this up](https://testing-library.com/docs/react-testing-library/setup#custom-render).
22
24
23
-
## Options
25
+
###Options
24
26
25
27
The behavior of the `render` method can be customized by passing various options as a second argument of the `RenderOptions` type:
26
28
27
-
### `wrapper` option
29
+
####`wrapper`
28
30
29
31
```ts
30
32
wrapper?:React.ComponentType<any>,
31
33
```
32
34
33
35
This option allows you to wrap the tested component, passed as the first option to the `render()` function, in an additional wrapper component. This is useful for creating reusable custom render functions for common React Context providers.
34
36
35
-
### `concurrentRoot` option{#concurrent-root}
37
+
####`concurrentRoot`{#concurrent-root}
36
38
37
39
Set to `false` to disable concurrent rendering.
38
40
Otherwise, `render` will default to using concurrent rendering used in the React Native New Architecture.
This option allows you to pass `createNodeMock` option to `ReactTestRenderer.create()` method in order to allow for custom mock refs. You can learn more about this option from [React Test Renderer documentation](https://reactjs.org/docs/test-renderer.html#ideas).
@@ -59,13 +61,13 @@ This **experimental** option allows you to replicate React Native behavior of th
59
61
60
62
React Test Renderer does not enforce this check; hence, by default, React Native Testing Library also does not check this. That might result in runtime errors when running your code on a device, while the code works without errors in tests.
61
63
62
-
## Result
64
+
###Result
63
65
64
66
The `render` function returns the same queries and utilities as the [`screen`](docs/api/screen) object. We recommended using the `screen` object as more developer-friendly way.
65
67
66
68
See [this article](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-screen) from Kent C. Dodds for more details.
67
69
68
-
## `renderAsync` function
70
+
## `renderAsync` function{#render-async}
69
71
70
72
:::info RNTL minimal version
71
73
@@ -80,7 +82,7 @@ async function renderAsync(
80
82
):Promise<RenderAsyncResult>;
81
83
```
82
84
83
-
The `renderAsync` function is the async version of `render` designed for working with React 19 and React Suspense. This function uses async `act` function internally to ensure all pending React updates are executed during rendering.
85
+
The `renderAsync` function is the async version of [`render`](#render) designed for working with React 19 and React Suspense. This function uses async `act` function internally to ensure all pending React updates are executed during rendering.
Copy file name to clipboardExpand all lines: website/docs/13.x/docs/api/screen.mdx
+2-10Lines changed: 2 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,26 +46,22 @@ Re-render the in-memory tree with a new root element. This simulates a React upd
46
46
_Also available under `updateAsync` alias_
47
47
48
48
:::info RNTL minimal version
49
-
50
49
This API requires RNTL v13.3.0 or later.
51
-
52
50
:::
53
51
54
52
```ts
55
53
function rerenderAsync(element:React.Element<unknown>):Promise<void>;
56
54
```
57
55
58
-
Async versions of `rerender` designed for working with React 19 and React Suspense. This method uses async `act` function internally to ensure all pending React updates are executed during updating.
56
+
Async versions of [`rerender`](#rerender) designed for working with React 19 and React Suspense. This method uses async `act` function internally to ensure all pending React updates are executed during updating.
@@ -87,21 +83,17 @@ Usually you should not need to call `unmount` as it is done automatically if you
87
83
### `unmountAsync`
88
84
89
85
:::info RNTL minimal version
90
-
91
86
This API requires RNTL v13.3.0 or later.
92
-
93
87
:::
94
88
95
89
```ts
96
90
function unmountAsync():Promise<void>;
97
91
```
98
92
99
-
Async version of `unmount` designed for working with React 19 and React Suspense. This method uses async `act` function internally to ensure all pending React updates are executed during unmounting.
93
+
Async version of [`unmount`](#unmount) designed for working with React 19 and React Suspense. This method uses async `act` function internally to ensure all pending React updates are executed during unmounting.
100
94
101
95
:::note
102
-
103
96
Usually you should not need to call `unmountAsync` as it is done automatically if your test runner supports `afterEach` hook (like Jest, mocha, Jasmine).
0 commit comments