Skip to content

Release/5.0.0 docs #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 12, 2021
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [The problem](#the-problem)
- [The solution](#the-solution)
- [When to use this library](#when-to-use-this-library)
Expand Down Expand Up @@ -141,17 +142,21 @@ npm install --save-dev @testing-library/react-hooks
### Peer Dependencies

`react-hooks-testing-library` does not come bundled with a version of
[`react`](https://www.npmjs.com/package/react) or
[`react-test-renderer`](https://www.npmjs.com/package/react-test-renderer) to allow you to install
the specific version you want to test against. Generally, the installed versions for `react` and
`react-test-renderer` should have matching versions:
[`react`](https://www.npmjs.com/package/react) to allow you to install the specific version you want
to test against. It also does not come installed with a specific renderer, we currently support
[`react-test-renderer`](https://www.npmjs.com/package/react-test-renderer) and
[`react-dom`](https://www.npmjs.com/package/react-dom), you only need to install one of them.
However,you can install both, but `react-test-renderer` is the default, for more information see the
[installation docs](https://react-hooks-testing-library.com/#installation). Generally, the installed
versions for `react` and the selected renderer should have matching versions:

```sh
npm install react@^16.9.0
npm install --save-dev react-test-renderer@^16.9.0
```

> **NOTE: The minimum supported version of `react` and `react-test-renderer` is `^16.9.0`.**
> **NOTE: The minimum supported version of `react`, `react-test-renderer` and `react-dom` is
> `^16.9.0`.**

## API

Expand Down
77 changes: 47 additions & 30 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ route: '/reference/api'
- [`act`](/reference/api#act)
- [`cleanup`](/reference/api#cleanup)
- [`addCleanup`](/reference/api#addcleanup)
- [`removeCleanup`](/reference/api#removecleanup)

---

## `renderHook`

```js
```ts
function renderHook(
callback: function(props?: any): any,
callback: (props?: any): any,
options?: RenderHookOptions
): RenderHookResult
```
Expand Down Expand Up @@ -61,7 +62,7 @@ The `renderHook` function returns an object that has the following properties:

### `result`

```js
```ts
{
all: Array<any>
current: any,
Expand All @@ -77,7 +78,7 @@ returned at the time.

### `rerender`

```js
```ts
function rerender(newProps?: any): void
```

Expand All @@ -86,13 +87,24 @@ passed, they will replace the `callback` function's `initialProps` for subsequen

### `unmount`

```js
```ts
function unmount(): void
```

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

### `hydrate`

```ts
function hydrate(): void
```

> This is only used when using the `server` module. See [Rendering](/renderer) for more information

A function to hydrate the component. This is commonly used before `act` calls and after `rerender`
calls.

### `...asyncUtils`

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

## `act`

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

---

## `cleanup`

```js
function cleanup: Promise<void>
```ts
function cleanup(): Promise<void>
```

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

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

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

## `addCleanup`

```js
function addCleanup(callback: function(): void|Promise<void>): function(): void
```ts
function addCleanup(callback: () => void | Promise<void>): (): void
```

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

## `removeCleanup`

```js
function removeCleanup(callback: function(): void|Promise<void>): void
```ts
function removeCleanup(callback: () => void | Promise<void>): void
```

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

### `waitForNextUpdate`

```js
function waitForNextUpdate(options?: {
timeout?: number
}): Promise<void>
```ts
function waitForNextUpdate(options?: { timeout?: number }): Promise<void>
```

Returns a `Promise` that resolves the next time the hook renders, commonly when state is updated as
Expand All @@ -202,12 +213,15 @@ The maximum amount of time in milliseconds (ms) to wait. By default, no timeout

### `waitFor`

```js
function waitFor(callback: function(): boolean|void, options?: {
interval?: number,
timeout?: number,
suppressErrors?: boolean
}): Promise<void>
```ts
function waitFor(
callback: () => boolean | void,
options?: {
interval?: number
timeout?: number
suppressErrors?: boolean
}
): Promise<void>
```

Returns a `Promise` that resolves if the provided callback executes without exception and returns a
Expand All @@ -232,12 +246,15 @@ rejected. By default, errors are suppressed for this utility.

### `waitForValueToChange`

```js
function waitForValueToChange(selector: function(): any, options?: {
interval?: number,
timeout?: number,
suppressErrors?: boolean
}): Promise<void>
```ts
function waitForValueToChange(
selector: () => any,
options?: {
interval?: number
timeout?: number
suppressErrors?: boolean
}
): Promise<void>
```

Returns a `Promise` that resolves if the value returned from the provided selector changes. It is
Expand Down
14 changes: 9 additions & 5 deletions docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,21 @@ npm install --save-dev @testing-library/react-hooks
### Peer Dependencies

`react-hooks-testing-library` does not come bundled with a version of
[`react`](https://www.npmjs.com/package/react) or
[`react-test-renderer`](https://www.npmjs.com/package/react-test-renderer) to allow you to install
the specific version you want to test against. Generally, the installed versions for `react` and
`react-test-renderer` should have matching versions:
[`react`](https://www.npmjs.com/package/react) to allow you to install the specific version you want
to test against. It also does not come installed with a specific renderer, we currently support
[`react-test-renderer`](https://www.npmjs.com/package/react-test-renderer) and
[`react-dom`](https://www.npmjs.com/package/react-dom), you only need to install one of them.
However,you can install both, but `react-test-renderer` is the default, for more information see the
[installation docs](https://react-hooks-testing-library.com/#installation). Generally, the installed
versions for `react` and the selected renderer should have matching versions:

```sh
npm install react@^16.9.0
npm install --save-dev react-test-renderer@^16.9.0
```

> **NOTE: The minimum supported version of `react` and `react-test-renderer` is `^16.9.0`.**
> **NOTE: The minimum supported version of `react`, `react-test-renderer` and `react-dom` is
> `^16.9.0`.**

## Testing Framework

Expand Down
66 changes: 66 additions & 0 deletions docs/renderer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
name: Renderer
route: '/renderer'
---

# Render Engine

## Overview

React requires a rendering engine, typically when creating an application people use `react-dom`.
When running tests, React still requires an engine. We currently support two different engines –
`react-test-renderer` & `react-dom`. If you have both installed in your project, by default we will
use `react-test-renderer`, assuming you're using the default imports:

```js
import { renderHook } from '@testing-library/react-hooks'
```

It does this because the library runs a check for available renderers, in the order:

- `react-test-renderer`
- `react-dom`

If neither are available you will see an error asking you to check that one of the above is
installed. If only one is installed, then it will use that renderer.

## Being specific

If, however, for certain tests you want to use a specific renderer (e.g. you want to use `react-dom`
for SSR) you can import the server module directly:

```ts
import { renderHook } from '@testing-library/react-hooks/server`
```

We have the following exports available:

```ts
import { renderHook, act } from '@testing-library/react-hooks' // will try to auto-detect

import { renderHook, act } from '@testing-library/react-hooks/dom' // will use react-dom

import { renderHook, act } from '@testing-library/react-hooks/native' // will use react-test-renderer

import { renderHook, act } from '@testing-library/react-hooks/server' // will use react-dom
```

## Caveats

## Auto detect

The auto detection function may not work if tests are bundled to run in the browser.

### SSR

While calling `renderHook` from `@testing-library/react-hooks/native` and
`@testing-library/react-hooks/dom` will return the same `RenderHookResult` as documented
[here](/reference/api#renderhook-result), using `@testing-library/react-hooks/server` will return an
additional function:

```ts
function hydrate(): void
```

Remember, state will not update with SSR unless `hydrate` is called. For more information on
`hydrate` see the [API documentation](/reference/api#hydrate).
1 change: 1 addition & 0 deletions doczrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default {
menu: [
{ name: 'Introduction' },
{ name: 'Usage', menu: ['Basic Hooks', 'Advanced Hooks'] },
{ name: 'Renderer' },
{ name: 'API Reference' }
]
}