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: README.md
+14-27
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,3 @@
1
-
---
2
-
name: Getting Started
3
-
route: '/'
4
-
---
5
-
6
1
<divalign="center">
7
2
<h1>react-hooks-testing-library</h1>
8
3
@@ -15,10 +10,11 @@ route: '/'
15
10
/>
16
11
</a>
17
12
18
-
<p>
19
-
Simple component wrapper and utilities for testing React hooks.
20
-
</p>
13
+
<p>Simple component wrapper and utilities for testing React hooks.</p>
21
14
15
+
<br />
16
+
<ahref="https://react-hooks-testing-library.netlify.com/"><strong>Read The Docs</strong></a>
17
+
<br />
22
18
</div>
23
19
24
20
<hr />
@@ -50,42 +46,41 @@ You don't really want to write a component solely for testing this hook and have
50
46
51
47
## The solution
52
48
53
-
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.
54
-
55
-
Similarly to [`react-testing-library`](http://npm.im/react-testing-library), which this library draws much of it's inspiration from, it aims to provide a testing experience as close as possible to natively using your hook from within a real component.
49
+
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.
56
50
57
51
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.
58
52
59
-
###When to use this library
53
+
## When to use this library
60
54
61
55
1. You're writing a library with one or more custom hooks that are not directly tied a component
62
56
2. You have a complex hook that is difficult to test through component interactions
63
57
64
-
###When not to use this library
58
+
## When not to use this library
65
59
66
60
1. Your hook is defined along side a component and is only used there
67
61
2. Your hook is easy to test by just testing the components using it
68
62
69
63
## Example
70
64
65
+
### `useCounter.js`
66
+
71
67
```js
72
-
// useCounter.js
73
68
import { useState, useCallback } from'react'
74
69
75
70
functionuseCounter() {
76
71
const [count, setCount] =useState(0)
77
72
78
73
constincrement=useCallback(() =>setCount((x) => x +1), [])
79
-
constdecrement=useCallback(() =>setCount((x) => x -1), [])
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
116
+
This project follows the [all-contributors](https://allcontributors.org/) specification. Contributions of any kind welcome!
Simple component wrapper and utilities for testing React hooks.
21
+
</p>
22
+
23
+
</div>
24
+
25
+
<hr />
26
+
27
+
## The problem
28
+
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:
30
+
31
+
> Invariant Violation: Hooks can only be called inside the body of a function component.
32
+
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
+
35
+
## The solution
36
+
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.
38
+
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.
40
+
41
+
### When to use this library
42
+
43
+
1. You're writing a library with one or more custom hooks that are not directly tied a component
44
+
2. You have a complex hook that is difficult to test through component interactions
45
+
46
+
### When not to use this library
47
+
48
+
1. Your hook is defined along side a component and is only used there
49
+
2. Your hook is easy to test by just testing the components using it
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.
0 commit comments