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/API.md
+136
Original file line number
Diff line number
Diff line change
@@ -465,3 +465,139 @@ expect(submitButtons).toHaveLength(3); // expect 3 elements
465
465
## `act`
466
466
467
467
Useful function to help testing components that use hooks API. By default any `render`, `update`, `fireEvent`, and `waitFor` calls are wrapped by this function, so there is no need to wrap it manually. This method is re-exported from [`react-test-renderer`](https://github.com/facebook/react/blob/main/packages/react-test-renderer/src/ReactTestRenderer.js#L567]).
468
+
469
+
## `renderHook`
470
+
471
+
Defined as:
472
+
473
+
```ts
474
+
function renderHook(
475
+
callback: (props?:any) =>any,
476
+
options?:RenderHookOptions
477
+
):RenderHookResult;
478
+
```
479
+
480
+
Renders a test component that will call the provided `callback`, including any hooks it calls, every time it renders. Returns [`RenderHookResult`](#renderhookresult-object) object, which you can interact with.
// Note that you should wrap the calls to functions your hook returns with `act` if they trigger an update of your hook's state to ensure pending useEffects are run before your next assertion.
The `renderHook` function accepts the following arguments:
509
+
510
+
### `callback`
511
+
512
+
The function that is called each `render` of the test component. This function should call one or more hooks for testing.
513
+
514
+
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.
515
+
516
+
### `options` (Optional)
517
+
518
+
A `RenderHookOptions` object to modify the execution of the `callback` function, containing the following properties:
519
+
520
+
#### `initialProps`
521
+
522
+
The initial values to pass as `props` to the `callback` function of `renderHook`.
523
+
524
+
#### `wrapper`
525
+
526
+
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`. `initialProps` and props subsequently set by `rerender` will be provided to the wrapper.
527
+
528
+
### `RenderHookResult` object
529
+
530
+
The `renderHook` function returns an object that has the following properties:
531
+
532
+
#### `result`
533
+
534
+
```jsx
535
+
{
536
+
all:Array<any>
537
+
current: any,
538
+
error:Error
539
+
}
540
+
```
541
+
542
+
The `current` value of the `result` will reflect the latest of whatever is returned from the `callback` passed to `renderHook`. Any thrown values from the latest call will be reflected in the `error` value of the `result`. The `all` value is an array containing all the returns (including the most recent) from the callback. These could be `result` or an `error` depending on what the callback returned at the time.
543
+
544
+
#### `rerender`
545
+
546
+
function rerender(newProps?: any): void
547
+
548
+
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.
549
+
550
+
#### `unmount`
551
+
552
+
function unmount(): void
553
+
554
+
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
555
+
556
+
### Examples
557
+
558
+
Here we present some extra examples of using `renderHook` API.
0 commit comments