Skip to content

Commit 5c61bf7

Browse files
committed
Updated API docs to use more words. Modified typings to seperate concepts to help docs
1 parent fd0ce2f commit 5c61bf7

File tree

4 files changed

+101
-36
lines changed

4 files changed

+101
-36
lines changed

docs/reference/api.md

+77-17
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,93 @@ route: '/reference/api'
66

77
# API
88

9-
## `renderHook(callback[, options])`
9+
`react-hooks-testing-library` exports the following methods:
10+
11+
- [`renderHook`](/reference/api#renderhook)
12+
- [`act`](/reference/api#act)
13+
14+
---
15+
16+
## `renderHook`
17+
18+
```js
19+
function renderHook(
20+
callback: function(props?: any): any,
21+
options?: RenderHookOptions
22+
): RenderHookResult
23+
```
1024

1125
Renders a test component that will call the provided `callback`, including any hooks it calls, every time it renders.
1226

27+
The `renderHook` function accept the following arguments:
28+
29+
### `callback`
30+
31+
The function that is called each `render` of the test component. This function should call one or more hooks for testing.
32+
33+
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.
34+
35+
### `options`
36+
37+
An options object to modify the execution of the `callback` function. See the [`renderHook` Options](/reference/api#renderhook-options) section for more details.
38+
1339
> _Note: `testHook` has been renamed to `renderHook`. `testHook` will continue work in the current version with a deprecation warning, but will be removed in a future version._
1440
>
1541
> **_You should update any usages of `testHook` to use `renderHook` instead._**
1642

17-
### Arguments
43+
## `renderHook` Options
44+
45+
The `renderHook` function accepts the following options as the second parameter:
46+
47+
### `initialProps`
48+
49+
The initial values to pass as `props` to the `callback` function of `renderHook.
50+
51+
### `wrapper`
1852

19-
- `callback` (`function([props])`) - function to call each render. This function should call one or more hooks for testing
20-
- The `props` passed into the callback will be the `initialProps` provided in the `options` until new props are provided by a `rerender` call
21-
- `options` (`object`)
22-
- `initialProps` (`object`) - the initial values to pass to the `callback` function
23-
- `wrapper` (`component`) - pass a React component to wrap the test component
24-
- This is usually used to add context providers from `React.createContext` for the hook access with `useContext`
53+
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`.
2554

26-
### Returns
55+
## `renderHook` Result
56+
57+
The `renderHook` method returns an object that has a following properties:
58+
59+
### `result`
60+
61+
```js
62+
{
63+
current: any,
64+
error: Error
65+
}
66+
```
67+
68+
The `current` value or the `result` will reflect whatever is returned from the `callback` passed to `renderHook`. Any thrown values will be reflected in the `error` value of the `result`.
69+
70+
### `waitForNextUpdate`
71+
72+
```js
73+
function waitForNextUpdate(): Promise<void>
74+
```
2775

28-
- `result` (`object`)
29-
- `current` (`any`) - the return value of the `callback` function
30-
- `error` (`Error`) - the error that was thrown if the `callback` function threw an error during rendering
3176
- `waitForNextUpdate` (`function`) - returns a `Promise` that resolves the next time the hook renders, commonly when state is updated as the result of a asynchronous action
32-
- `rerender` (`function([newProps])`) - function to rerender the test component including any hooks called in the `callback` function
33-
- If `newProps` are passed, the will replace the `initialProps` passed the the `callback` function for future renders
34-
- `unmount` (`function()`) - function to unmount the test component, commonly used to trigger cleanup effects for `useEffect` hooks
3577

36-
## `act(callback)`
78+
### `rerender`
79+
80+
```js
81+
function rerender(newProps?: any): void
82+
```
83+
84+
A function to rerender the test component, causing any hooks to be recalculated. If `newProps` are passed, the will replace the `initialProps` passed to the `callback` function for the rerender any subsequent renders.
85+
86+
### `unmount`
87+
88+
```js
89+
function unmount(): void
90+
```
91+
92+
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
93+
94+
---
95+
96+
## `act`
3797

38-
This is the same [`act` function](https://testing-library.com/docs/react-testing-library/api#act) that is exported by `react-testing-library`.
98+
This is the same [`act` function](https://reactjs.org/docs/test-utils.html#act) that is exported by `react-test-renderer`.

package-lock.json

+2-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"contributors:add": "all-contributors add"
3131
},
3232
"dependencies": {
33-
"@babel/runtime": "^7.4.2"
33+
"@babel/runtime": "^7.4.2",
34+
"@types/react": "16.8.14"
3435
},
3536
"devDependencies": {
3637
"@babel/cli": "7.4.3",
@@ -40,7 +41,6 @@
4041
"@babel/plugin-transform-runtime": "7.4.3",
4142
"@babel/preset-env": "7.4.3",
4243
"@babel/preset-react": "7.0.0",
43-
"@types/react": "16.8.14",
4444
"all-contributors-cli": "6.3.1",
4545
"babel-eslint": "10.0.1",
4646
"babel-plugin-module-resolver": "3.2.0",

typings/index.d.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
export function renderHook<P, R>(
2-
callback: (props: P) => R,
3-
options?: {
4-
initialProps?: P,
5-
wrapper?: React.ComponentType
6-
}
7-
): {
8-
readonly result: {
9-
readonly current: R,
10-
readonly error: Error
11-
}
1+
import * as React from 'react'
2+
3+
interface RenderHookOptions<P> {
4+
initialProps?: P
5+
wrapper?: React.ComponentType
6+
}
7+
8+
interface HookResult<R> {
9+
readonly current: R
10+
readonly error: Error
11+
}
12+
13+
interface RenderHookResult<P, R> {
14+
readonly result: HookResult<R>
1215
readonly waitForNextUpdate: () => Promise<void>
1316
readonly unmount: () => boolean
14-
readonly rerender: (hookProps?: P) => void
17+
readonly rerender: (newProps?: P) => void
1518
}
1619

20+
export function renderHook<P, R>(
21+
callback: (props: P) => R,
22+
options?: RenderHookOptions<P>
23+
): RenderHookResult<P, R>
24+
1725
export const testHook: typeof renderHook
1826

1927
export function act(callback: () => void): void

0 commit comments

Comments
 (0)