Skip to content

Commit 8abd2f0

Browse files
committed
docs: update api-reference
Also update examples to be more (imo) TS like
1 parent 684b2f1 commit 8abd2f0

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

docs/api-reference.md

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ route: '/reference/api'
1111
- [`act`](/reference/api#act)
1212
- [`cleanup`](/reference/api#cleanup)
1313
- [`addCleanup`](/reference/api#addcleanup)
14+
- [`removeCleanup`](/reference/api#removecleanup)
1415

1516
---
1617

1718
## `renderHook`
1819

19-
```js
20-
function renderHook(
21-
callback: function(props?: any): any,
20+
```ts
21+
renderHook: (
22+
callback: (props?: any): any,
2223
options?: RenderHookOptions
23-
): RenderHookResult
24+
) => RenderHookResult
2425
```
2526

2627
Renders a test component that will call the provided `callback`, including any hooks it calls, every
@@ -61,7 +62,7 @@ The `renderHook` function returns an object that has the following properties:
6162

6263
### `result`
6364

64-
```js
65+
```ts
6566
{
6667
all: Array<any>
6768
current: any,
@@ -77,22 +78,33 @@ returned at the time.
7778

7879
### `rerender`
7980

80-
```js
81-
function rerender(newProps?: any): void
81+
```ts
82+
rerender: (newProps?: any) => void
8283
```
8384

8485
A function to rerender the test component, causing any hooks to be recalculated. If `newProps` are
8586
passed, they will replace the `callback` function's `initialProps` for subsequent rerenders.
8687

8788
### `unmount`
8889

89-
```js
90-
function unmount(): void
90+
```ts
91+
unmount: () => void
9192
```
9293

9394
A function to unmount the test component. This is commonly used to trigger cleanup effects for
9495
`useEffect` hooks.
9596

97+
### `hydrate`
98+
99+
```ts
100+
hydrate: () => void
101+
```
102+
103+
> This is only used when using the `server` module. See [Rendering](/renderer) for more information
104+
105+
A function to hydrate the component. This is commonly used before `act` calls and after `rerender`
106+
calls.
107+
96108
### `...asyncUtils`
97109

98110
Utilities to assist with testing asynchronous behaviour. See the
@@ -102,15 +114,15 @@ Utilities to assist with testing asynchronous behaviour. See the
102114

103115
## `act`
104116

105-
This is the same [`act` function](https://reactjs.org/docs/test-utils.html#act) that is exported by
106-
`react-test-renderer`.
117+
This is the same [`act` function](https://reactjs.org/docs/test-utils.html#act) function that is
118+
exported from your chosen renderer. Although, they both work the same.
107119

108120
---
109121

110122
## `cleanup`
111123

112-
```js
113-
function cleanup: Promise<void>
124+
```ts
125+
cleanup: () => Promise<void>
114126
```
115127

116128
Unmounts any rendered hooks rendered with `renderHook`, ensuring all effects have been flushed. Any
@@ -142,7 +154,8 @@ module.exports = {
142154
```
143155

144156
Alternatively, you can change your test to import from `@testing-library/react-hooks/pure` instead
145-
of the regular imports.
157+
of the regular imports. This applys to any of our export methods documented in
158+
[Rendering](/rendering#being-specific).
146159

147160
```diff
148161
- import { renderHook, cleanup, act } from '@testing-library/react-hooks'
@@ -156,8 +169,8 @@ variable to `true` before importing `@testing-library/react-hooks` will also dis
156169

157170
## `addCleanup`
158171

159-
```js
160-
function addCleanup(callback: function(): void|Promise<void>): function(): void
172+
```ts
173+
addCleanup: (callback: () => void | Promise<void>) => () => void
161174
```
162175

163176
Add a callback to be called during [`cleanup`](/reference/api#cleanup), returning a function to
@@ -173,8 +186,8 @@ be resolved before moving onto the next cleanup callback.
173186
174187
## `removeCleanup`
175188

176-
```js
177-
function removeCleanup(callback: function(): void|Promise<void>): void
189+
```ts
190+
removeCleanup: (callback: () => void | Promise<void>) => void
178191
```
179192

180193
Removes a cleanup callback previously added with [`addCleanup`](/reference/api#addCleanup). Once
@@ -187,10 +200,8 @@ removed, the provided callback will no longer execute as part of running
187200

188201
### `waitForNextUpdate`
189202

190-
```js
191-
function waitForNextUpdate(options?: {
192-
timeout?: number
193-
}): Promise<void>
203+
```ts
204+
waitForNextUpdate: (options?: { timeout?: number }) => Promise<void>
194205
```
195206

196207
Returns a `Promise` that resolves the next time the hook renders, commonly when state is updated as
@@ -203,11 +214,11 @@ The maximum amount of time in milliseconds (ms) to wait. By default, no timeout
203214
### `waitFor`
204215

205216
```js
206-
function waitFor(callback: function(): boolean|void, options?: {
217+
waitFor: (callback: () => boolean | void, options?: {
207218
interval?: number,
208219
timeout?: number,
209220
suppressErrors?: boolean
210-
}): Promise<void>
221+
}) => Promise<void>
211222
```
212223

213224
Returns a `Promise` that resolves if the provided callback executes without exception and returns a
@@ -233,11 +244,11 @@ rejected. By default, errors are suppressed for this utility.
233244
### `waitForValueToChange`
234245

235246
```js
236-
function waitForValueToChange(selector: function(): any, options?: {
247+
waitForValueToChange: (selector: () => any, options?: {
237248
interval?: number,
238249
timeout?: number,
239250
suppressErrors?: boolean
240-
}): Promise<void>
251+
}) => Promise<void>
241252
```
242253

243254
Returns a `Promise` that resolves if the value returned from the provided selector changes. It is

0 commit comments

Comments
 (0)