Skip to content

Commit 830af58

Browse files
docs: async docs (#1806)
1 parent 196680b commit 830af58

File tree

7 files changed

+159
-65
lines changed

7 files changed

+159
-65
lines changed

website/docs/13.x/docs/api/events/fire-event.mdx

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Fire Event API
22

3-
```ts
4-
function fireEvent(element: ReactTestInstance, eventName: string, ...data: unknown[]): void;
5-
```
3+
## `fireEvent` {#fire-event}
64

75
:::note
86
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
119
Use Fire Event for cases not supported by User Event and for triggering event handlers on composite components.
1210
:::
1311

12+
```ts
13+
function fireEvent(element: ReactTestInstance, eventName: string, ...data: unknown[]): void;
14+
```
15+
1416
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.
1517

1618
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
5759

5860
### `fireEvent.press` {#press}
5961

60-
```
61-
fireEvent.press: (element: ReactTestInstance, ...data: Array<any>) => void
62-
```
63-
6462
:::note
6563
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.
6664
:::
6765

66+
```tsx
67+
fireEvent.press: (
68+
element: ReactTestInstance,
69+
...data: Array<any>,
70+
) => void
71+
```
72+
6873
Invokes `press` event handler on the element or parent element in the tree.
6974

7075
```jsx
@@ -93,14 +98,17 @@ expect(onPressMock).toHaveBeenCalledWith(eventData);
9398

9499
### `fireEvent.changeText` {#change-text}
95100

96-
```
97-
fireEvent.changeText: (element: ReactTestInstance, ...data: Array<any>) => void
98-
```
99-
100101
:::note
101102
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.
102103
:::
103104

105+
```tsx
106+
fireEvent.changeText: (
107+
element: ReactTestInstance,
108+
...data: Array<any>,
109+
) => void
110+
```
111+
104112
Invokes `changeText` event handler on the element or parent element in the tree.
105113

106114
```jsx
@@ -121,8 +129,15 @@ fireEvent.changeText(screen.getByPlaceholderText('Enter data'), CHANGE_TEXT);
121129

122130
### `fireEvent.scroll` {#scroll}
123131

124-
```
125-
fireEvent.scroll: (element: ReactTestInstance, ...data: Array<any>) => void
132+
:::note
133+
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
126141
```
127142

128143
Invokes `scroll` event handler on the element or parent element in the tree.
@@ -152,12 +167,10 @@ fireEvent.scroll(screen.getByText('scroll-view'), eventData);
152167
```
153168

154169
:::note
155-
156170
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-
158171
:::
159172

160-
## `fireEventAsync`
173+
## `fireEventAsync` {#fire-event-async}
161174

162175
:::info RNTL minimal version
163176

@@ -173,7 +186,7 @@ async function fireEventAsync(
173186
): Promise<unknown>;
174187
```
175188

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.
177190

178191
```jsx
179192
import { renderAsync, screen, fireEventAsync } from '@testing-library/react-native';
@@ -190,24 +203,45 @@ Like `fireEvent`, `fireEventAsync` also provides convenience methods for common
190203

191204
### `fireEventAsync.press` {#async-press}
192205

193-
```
194-
fireEventAsync.press: (element: ReactTestInstance, ...data: Array<any>) => Promise<unknown>
206+
:::note
207+
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>
195215
```
196216

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.
198218

199219
### `fireEventAsync.changeText` {#async-change-text}
200220

201-
```
202-
fireEventAsync.changeText: (element: ReactTestInstance, ...data: Array<any>) => Promise<unknown>
221+
:::note
222+
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>
203230
```
204231

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.
206233

207234
### `fireEventAsync.scroll` {#async-scroll}
208235

209-
```
210-
fireEventAsync.scroll: (element: ReactTestInstance, ...data: Array<any>) => Promise<unknown>
236+
:::note
237+
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>
211245
```
212246

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.

website/docs/13.x/docs/api/events/user-event.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,6 @@ Events will not be emitted if the `editable` prop is set to `false`.
226226
227227
## `scrollTo()` \{#scroll-to}
228228
229-
:::note
230-
`scrollTo` interaction has been introduced in RNTL v12.4.0.
231-
:::
232-
233229
```ts
234230
scrollTo(
235231
element: ReactTestInstance,

website/docs/13.x/docs/api/misc/render-hook.mdx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# `renderHook` function
22

3+
## `renderHook`
4+
35
```ts
46
function renderHook<Result, Props>(
57
hookFn: (props?: Props) => Result,
@@ -41,15 +43,15 @@ Callback is a function that is called each `render` of the test component. This
4143

4244
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.
4345

44-
## `options`
46+
### `options`
4547

4648
A `RenderHookOptions<Props>` object to modify the execution of the `callback` function, containing the following properties:
4749

48-
### `initialProps` {#initial-props}
50+
#### `initialProps` {#initial-props}
4951

5052
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.
5153

52-
### `wrapper`
54+
#### `wrapper`
5355

5456
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`.
5557

@@ -58,7 +60,7 @@ A React component to wrap the test component in when rendering. This is usually
5860
Set to `false` to disable concurrent rendering.
5961
Otherwise, `render` will default to using concurrent rendering used in the React Native New Architecture.
6062

61-
## Result
63+
### Result
6264

6365
```ts
6466
interface RenderHookResult<Result, Props> {
@@ -70,23 +72,23 @@ interface RenderHookResult<Result, Props> {
7072

7173
The `renderHook` function returns an object that has the following properties:
7274

73-
### `result`
75+
#### `result`
7476

7577
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.
7678

77-
### `rerender`
79+
#### `rerender`
7880

7981
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.
8082

81-
### `unmount`
83+
#### `unmount`
8284

8385
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
8486

85-
## Examples
87+
### Examples
8688

8789
Here we present some extra examples of using `renderHook` API.
8890

89-
### With `initialProps`
91+
#### With `initialProps`
9092

9193
```ts
9294
const useCount = (initialCount: number) => {
@@ -117,7 +119,7 @@ it('should increment count', () => {
117119
});
118120
```
119121

120-
### With `wrapper`
122+
#### With `wrapper`
121123

122124
```tsx
123125
it('should use context value', () => {
@@ -150,18 +152,18 @@ Async versions of `renderHook` designed for working with React 19 and React Susp
150152
```ts
151153
interface RenderHookAsyncResult<Result, Props> {
152154
result: { current: Result };
153-
rerender: (props: Props) => Promise<void>;
154-
unmount: () => Promise<void>;
155+
rerenderAsync: (props: Props) => Promise<void>;
156+
unmountAsync: () => Promise<void>;
155157
}
156158
```
157159

158-
The `RenderHookAsyncResult` differs from `RenderHookResult` in that `rerender` and `unmount` are async functions.
160+
The `RenderHookAsyncResult` differs from `RenderHookResult` in that `rerenderAsync` and `unmountAsync` are async functions.
159161

160162
```ts
161163
import { renderHookAsync, act } from '@testing-library/react-native';
162164

163165
test('should handle async hook behavior', async () => {
164-
const { result, rerender } = await renderHookAsync(useAsyncHook);
166+
const { result, rerenderAsync } = await renderHookAsync(useAsyncHook);
165167

166168
// Test initial state
167169
expect(result.current.loading).toBe(true);
@@ -172,7 +174,7 @@ test('should handle async hook behavior', async () => {
172174
});
173175

174176
// Re-render to get updated state
175-
await rerender();
177+
await rerenderAsync();
176178
expect(result.current.loading).toBe(false);
177179
});
178180
```

website/docs/13.x/docs/api/render.mdx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# `render` function
1+
# `render` API
2+
3+
## `render` function {#render}
24

35
```jsx
46
function render(
@@ -20,32 +22,32 @@ test('basic test', () => {
2022

2123
> 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).
2224
23-
## Options
25+
### Options
2426

2527
The behavior of the `render` method can be customized by passing various options as a second argument of the `RenderOptions` type:
2628

27-
### `wrapper` option
29+
#### `wrapper`
2830

2931
```ts
3032
wrapper?: React.ComponentType<any>,
3133
```
3234

3335
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.
3436

35-
### `concurrentRoot` option {#concurrent-root}
37+
#### `concurrentRoot` {#concurrent-root}
3638

3739
Set to `false` to disable concurrent rendering.
3840
Otherwise, `render` will default to using concurrent rendering used in the React Native New Architecture.
3941

40-
### `createNodeMock` option
42+
#### `createNodeMock` {#create-node-mock}
4143

4244
```ts
4345
createNodeMock?: (element: React.Element) => unknown,
4446
```
4547

4648
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).
4749

48-
### `unstable_validateStringsRenderedWithinText` option
50+
#### `unstable_validateStringsRenderedWithinText`
4951

5052
```ts
5153
unstable_validateStringsRenderedWithinText?: boolean;
@@ -59,13 +61,13 @@ This **experimental** option allows you to replicate React Native behavior of th
5961

6062
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.
6163

62-
## Result
64+
### Result
6365

6466
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.
6567

6668
See [this article](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-screen) from Kent C. Dodds for more details.
6769

68-
## `renderAsync` function
70+
## `renderAsync` function {#render-async}
6971

7072
:::info RNTL minimal version
7173

@@ -80,7 +82,7 @@ async function renderAsync(
8082
): Promise<RenderAsyncResult>;
8183
```
8284

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.
8486

8587
```jsx
8688
import { renderAsync, screen } from '@testing-library/react-native';

website/docs/13.x/docs/api/screen.mdx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,22 @@ Re-render the in-memory tree with a new root element. This simulates a React upd
4646
_Also available under `updateAsync` alias_
4747

4848
:::info RNTL minimal version
49-
5049
This API requires RNTL v13.3.0 or later.
51-
5250
:::
5351

5452
```ts
5553
function rerenderAsync(element: React.Element<unknown>): Promise<void>;
5654
```
5755

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.
5957

6058
```jsx
6159
import { renderAsync, screen } from '@testing-library/react-native';
6260

6361
test('async rerender test', async () => {
6462
await renderAsync(<MyComponent initialData="first" />);
6563

66-
// Use async rerender when component has suspense or async behavior
6764
await screen.rerenderAsync(<MyComponent initialData="updated" />);
68-
6965
expect(screen.getByText('updated')).toBeOnTheScreen();
7066
});
7167
```
@@ -87,21 +83,17 @@ Usually you should not need to call `unmount` as it is done automatically if you
8783
### `unmountAsync`
8884

8985
:::info RNTL minimal version
90-
9186
This API requires RNTL v13.3.0 or later.
92-
9387
:::
9488

9589
```ts
9690
function unmountAsync(): Promise<void>;
9791
```
9892

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.
10094

10195
:::note
102-
10396
Usually you should not need to call `unmountAsync` as it is done automatically if your test runner supports `afterEach` hook (like Jest, mocha, Jasmine).
104-
10597
:::
10698

10799
### `debug`

0 commit comments

Comments
 (0)