Skip to content

Commit 432d44c

Browse files
committed
Updating docs and type definitions
1 parent 547677f commit 432d44c

File tree

8 files changed

+4558
-1000
lines changed

8 files changed

+4558
-1000
lines changed

docs/introduction/getting-started.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ route: '/'
1717
</a>
1818

1919
<p>
20-
Simple component wrapper and utilities for testing React hooks.
20+
Simple and complete React hooks testing utilities that encourage good testing practices.
2121
</p>
2222

2323
</div>
@@ -26,17 +26,26 @@ route: '/'
2626

2727
## The problem
2828

29-
You're writing an awesome custom hook and you want to test it, but as soon as you call it you see the following error:
29+
You're writing an awesome custom hook and you want to test it, but as soon as you call it you see
30+
the following error:
3031

3132
> Invariant Violation: Hooks can only be called inside the body of a function component.
3233
33-
You don't really want to write a component solely for testing this hook and have to work out how you were going to trigger all the various ways the hook can be updated, especially given the complexities of how you've wired the whole thing together.
34+
You don't really want to write a component solely for testing this hook and have to work out how you
35+
were going to trigger all the various ways the hook can be updated, especially given the
36+
complexities of how you've wired the whole thing together.
3437

3538
## The solution
3639

37-
The `react-hooks-testing-library` allows you to create a simple test harness for React hooks that handles running them within the body of a function component, as well as providing various useful utility functions for updating the inputs and retrieving the outputs of your amazing custom hook. This library aims to provide a testing experience as close as possible to natively using your hook from within a real component.
40+
The `react-hooks-testing-library` allows you to create a simple test harness for React hooks that
41+
handles running them within the body of a function component, as well as providing various useful
42+
utility functions for updating the inputs and retrieving the outputs of your amazing custom hook.
43+
This library aims to provide a testing experience as close as possible to natively using your hook
44+
from within a real component.
3845

39-
Using this library, you do not have to concern yourself with how to construct, render or interact with the react component in order to test your hook. You can just use the hook directly and assert the results.
46+
Using this library, you do not have to concern yourself with how to construct, render or interact
47+
with the react component in order to test your hook. You can just use the hook directly and assert
48+
the results.
4049

4150
### When to use this library
4251

docs/introduction/setup.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ route: '/setup'
88

99
## Installation
1010

11-
This module is distributed via [npm](https://www.npmjs.com/) which is bundled with [node](https://nodejs.org) and
12-
should be installed as one of your project's `devDependencies`:
11+
This module is distributed via [npm](https://www.npmjs.com/) which is bundled with
12+
[node](https://nodejs.org) and should be installed as one of your project's `devDependencies`:
1313

1414
```sh
15-
npm install --save-dev react-hooks-testing-library
15+
npm install --save-dev @testing-library/react-hooks
1616
```
1717

1818
## Testing Framework
1919

20-
In order to run tests, you will probably want to be using a test framework. If you have not already got one, we recommend using [jest](https://jestjs.io/), but this library should work without issues with any of the alternatives.
20+
In order to run tests, you will probably want to be using a test framework. If you have not already
21+
got one, we recommend using [jest](https://jestjs.io/), but this library should work without issues
22+
with any of the alternatives.

docs/reference/api.md

+21-14
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ function renderHook(
2222
): RenderHookResult
2323
```
2424

25-
Renders a test component that will call the provided `callback`, including any hooks it calls, every time it renders.
25+
Renders a test component that will call the provided `callback`, including any hooks it calls, every
26+
time it renders.
2627

2728
The `renderHook` function accept the following arguments:
2829

2930
### `callback`
3031

31-
The function that is called each `render` of the test component. This function should call one or more hooks for testing.
32+
The function that is called each `render` of the test component. This function should call one or
33+
more hooks for testing.
3234

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.
35+
The `props` passed into the callback will be the `initialProps` provided in the `options` to
36+
`renderHook`, unless new props are provided by a subsequent `rerender` call.
3437

3538
### `options`
3639

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-
39-
> _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._
40-
>
41-
> **_You should update any usages of `testHook` to use `renderHook` instead._**
40+
An options object to modify the execution of the `callback` function. See the
41+
[`renderHook` Options](/reference/api#renderhook-options) section for more details.
4242

4343
## `renderHook` Options
4444

@@ -50,7 +50,8 @@ The initial values to pass as `props` to the `callback` function of `renderHook.
5050

5151
### `wrapper`
5252

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`.
53+
A React component to wrap the test component in when rendering. This is usually used to add context
54+
providers from `React.createContext` for the hook to access with `useContext`.
5455

5556
## `renderHook` Result
5657

@@ -65,34 +66,40 @@ The `renderHook` method returns an object that has a following properties:
6566
}
6667
```
6768
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+
The `current` value or the `result` will reflect whatever is returned from the `callback` passed to
70+
`renderHook`. Any thrown values will be reflected in the `error` value of the `result`.
6971
7072
### `waitForNextUpdate`
7173
7274
```js
7375
function waitForNextUpdate(): Promise<void>
7476
```
7577

76-
- `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
78+
- `waitForNextUpdate` (`function`) - returns a `Promise` that resolves the next time the hook
79+
renders, commonly when state is updated as the result of a asynchronous action
7780

7881
### `rerender`
7982

8083
```js
8184
function rerender(newProps?: any): void
8285
```
8386

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.
87+
A function to rerender the test component, causing any hooks to be recalculated. If `newProps` are
88+
passed, the will replace the `initialProps` passed to the `callback` function for the rerender any
89+
subsequent renders.
8590

8691
### `unmount`
8792

8893
```js
8994
function unmount(): void
9095
```
9196

92-
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
97+
A function to unmount the test component. This is commonly used to trigger cleanup effects for
98+
`useEffect` hooks.
9399

94100
---
95101

96102
## `act`
97103

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

docs/usage/advanced-hooks.md

+41-16
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ route: '/usage/advanced-hooks'
88

99
## Context
1010

11-
Often, a hook is going to need a value out of context. The `useContext` hook is really good for this, but it will ofter required a `Provider` to be wrapped around the component using the hook. We can use the `wrapper` option for `renderHook` to do just that.
11+
Often, a hook is going to need a value out of context. The `useContext` hook is really good for
12+
this, but it will ofter required a `Provider` to be wrapped around the component using the hook. We
13+
can use the `wrapper` option for `renderHook` to do just that.
1214

13-
Let's change the `useCounter` example from the [Basic Hooks section](/usage/basic-hooks) to get a `step` value from context and build a `CounterStepProvider` that allows us to set the value:
15+
Let's change the `useCounter` example from the [Basic Hooks section](/usage/basic-hooks) to get a
16+
`step` value from context and build a `CounterStepProvider` that allows us to set the value:
1417

1518
```js
1619
import React, { useState, useContext, useCallback } from 'react'
@@ -33,7 +36,7 @@ export function useCounter(initialValue = 0) {
3336
In our test, we simply use `CounterStepProvider` as the `wrapper` when rendering the hook:
3437

3538
```js
36-
import { renderHook } from 'react-hooks-testing-library'
39+
import { renderHook } from '@testing-library/react-hooks'
3740
import { CounterStepProvider, useCounter } from './counter'
3841

3942
test('should use custom step when incrementing', () => {
@@ -48,20 +51,28 @@ test('should use custom step when incrementing', () => {
4851
})
4952
```
5053

51-
The `wrapper` option will accept any React component, but it **must** render `children` in order for the test component to render and the hook to execute.
54+
The `wrapper` option will accept any React component, but it **must** render `children` in order for
55+
the test component to render and the hook to execute.
5256

5357
### ESLint Warning
5458

55-
It can be very tempting to try to inline the `wrapper` variable into the `renderHook` line, and there is nothing technically wrong with doing that, but if you are using [`eslint`](https://eslint.org/) and [`eslint-plugin-react`](https://github.com/yannickcr/eslint-plugin-react), you will see a linting error that says:
59+
It can be very tempting to try to inline the `wrapper` variable into the `renderHook` line, and
60+
there is nothing technically wrong with doing that, but if you are using
61+
[`eslint`](https://eslint.org/) and
62+
[`eslint-plugin-react`](https://github.com/yannickcr/eslint-plugin-react), you will see a linting
63+
error that says:
5664

5765
> Component definition is missing display name
5866
59-
This is caused by the `react/display-name` rule and although it's unlikely to cause you any issues, it's best to take steps to remove it. If you feel strongly about not having a seperate `wrapper` variable, you can disable the error for the test file but adding a special comment to the top of the file:
67+
This is caused by the `react/display-name` rule and although it's unlikely to cause you any issues,
68+
it's best to take steps to remove it. If you feel strongly about not having a seperate `wrapper`
69+
variable, you can disable the error for the test file but adding a special comment to the top of the
70+
file:
6071

6172
```js
6273
/* eslint-disable react/display-name */
6374

64-
import { renderHook } from 'react-hooks-testing-library'
75+
import { renderHook } from '@testing-library/react-hooks'
6576
import { CounterStepProvider, useCounter } from './counter'
6677

6778
test('should use custom step when incrementing', () => {
@@ -77,13 +88,19 @@ test('should use custom step when incrementing', () => {
7788
})
7889
```
7990

80-
Similar techniques can be used to disable the error for just the specific line, or for the whole project, but please take the time to understand the impact that disabling linting rules will have on you, your team, and your project.
91+
Similar techniques can be used to disable the error for just the specific line, or for the whole
92+
project, but please take the time to understand the impact that disabling linting rules will have on
93+
you, your team, and your project.
8194

8295
## Async
8396

84-
Sometimes, a hook can trigger asynchronous updates that will not be immediately reflected in the `result.current` value. Luckily, `renderHook` returns a utility that allows the test to wait for the hook to update using `async/await` (or just promise callbacks if you prefer) called `waitForNextUpdate`.
97+
Sometimes, a hook can trigger asynchronous updates that will not be immediately reflected in the
98+
`result.current` value. Luckily, `renderHook` returns a utility that allows the test to wait for the
99+
hook to update using `async/await` (or just promise callbacks if you prefer) called
100+
`waitForNextUpdate`.
85101

86-
Let's further extend `useCounter` to have an `incrementAsync` callback that will update the `count` after `100ms`:
102+
Let's further extend `useCounter` to have an `incrementAsync` callback that will update the `count`
103+
after `100ms`:
87104

88105
```js
89106
import React, { useState, useContext, useCallback } from 'react'
@@ -101,7 +118,7 @@ export function useCounter(initialValue = 0) {
101118
To test `incrementAsync` we need to `await waitForNextUpdate()` before the make our assertions:
102119

103120
```js
104-
import { renderHook, act } from 'react-hooks-testing-library'
121+
import { renderHook, act } from '@testing-library/react-hooks'
105122
import { useCounter } from './counter'
106123

107124
test('should increment counter after delay', async () => {
@@ -117,14 +134,20 @@ test('should increment counter after delay', async () => {
117134

118135
### Suspense
119136

120-
`waitForNextUpdate` will also wait for hooks that suspends using [React's `Suspense`](https://reactjs.org/docs/code-splitting.html#suspense) functionality finish rendering.
137+
`waitForNextUpdate` will also wait for hooks that suspends using
138+
[React's `Suspense`](https://reactjs.org/docs/code-splitting.html#suspense) functionality finish
139+
rendering.
121140

122141
### `act` Warning
123142

124-
When testing async hooks, you will likely see a warning from React that tells you to wrap the update in `act(() => {...})`, but you can't because the update is internal to the hook code, not the test code. This is a [known issue](https://github.com/mpeyper/react-hooks-testing-library/issues/14) and should have a fix when React `v16.9.0` is released, but until then, you can either just ignore the warning, or suppress the output:
143+
When testing async hooks, you will likely see a warning from React that tells you to wrap the update
144+
in `act(() => {...})`, but you can't because the update is internal to the hook code, not the test
145+
code. This is a [known issue](https://github.com/mpeyper/react-hooks-testing-library/issues/14) and
146+
should have a fix when React `v16.9.0` is released, but until then, you can either just ignore the
147+
warning, or suppress the output:
125148

126149
```js
127-
import { renderHook } from 'react-hooks-testing-library'
150+
import { renderHook } from '@testing-library/react-hooks'
128151
import { useCounter } from './counter'
129152

130153
it('should increment counter after delay', async () => {
@@ -147,7 +170,9 @@ it('should increment counter after delay', async () => {
147170

148171
## Errors
149172

150-
If you need to test that a hook throws the errors you expect it to, you can use `result.error` to access an error that may have been thrown in the previous render. For example, we could make the `useCounter` hook threw an error if the count reached a specific value:
173+
If you need to test that a hook throws the errors you expect it to, you can use `result.error` to
174+
access an error that may have been thrown in the previous render. For example, we could make the
175+
`useCounter` hook threw an error if the count reached a specific value:
151176

152177
```js
153178
import React, { useState, useContext, useCallback } from 'react'
@@ -168,7 +193,7 @@ export function useCounter(initialValue = 0) {
168193
```
169194

170195
```js
171-
import { renderHook, act } from 'react-hooks-testing-library'
196+
import { renderHook, act } from '@testing-library/react-hooks'
172197
import { useCounter } from './counter'
173198

174199
it('should throw when over 9000', () => {

0 commit comments

Comments
 (0)