Skip to content

Commit 7bbc577

Browse files
committed
Added introduction pages to docs and link to docs in README
1 parent ca4015f commit 7bbc577

File tree

4 files changed

+85
-28
lines changed

4 files changed

+85
-28
lines changed

README.md

+14-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
---
2-
name: Getting Started
3-
route: '/'
4-
---
5-
61
<div align="center">
72
<h1>react-hooks-testing-library</h1>
83

@@ -15,10 +10,11 @@ route: '/'
1510
/>
1611
</a>
1712

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>
2114

15+
<br />
16+
<a href="https://react-hooks-testing-library.netlify.com/"><strong>Read The Docs</strong></a>
17+
<br />
2218
</div>
2319

2420
<hr />
@@ -50,42 +46,41 @@ You don't really want to write a component solely for testing this hook and have
5046

5147
## The solution
5248

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.
5650

5751
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.
5852

59-
### When to use this library
53+
## When to use this library
6054

6155
1. You're writing a library with one or more custom hooks that are not directly tied a component
6256
2. You have a complex hook that is difficult to test through component interactions
6357

64-
### When not to use this library
58+
## When not to use this library
6559

6660
1. Your hook is defined along side a component and is only used there
6761
2. Your hook is easy to test by just testing the components using it
6862

6963
## Example
7064

65+
### `useCounter.js`
66+
7167
```js
72-
// useCounter.js
7368
import { useState, useCallback } from 'react'
7469

7570
function useCounter() {
7671
const [count, setCount] = useState(0)
7772

7873
const increment = useCallback(() => setCount((x) => x + 1), [])
79-
const decrement = useCallback(() => setCount((x) => x - 1), [])
8074

81-
return { count, increment, decrement }
75+
return { count, increment }
8276
}
8377

8478
export default useCounter
8579
```
8680

81+
### `useCounter.test.js`
82+
8783
```js
88-
// useCounter.test.js
8984
import { renderHook, act } from 'react-hooks-testing-library'
9085
import useCounter from './useCounter'
9186

@@ -96,14 +91,6 @@ test('should increment counter', () => {
9691

9792
expect(result.current.count).toBe(1)
9893
})
99-
100-
test('should decrement counter', () => {
101-
const { result } = renderHook(() => useCounter())
102-
103-
act(() => result.current.decrement())
104-
105-
expect(result.current.count).toBe(-1)
106-
})
10794
```
10895

10996
## Installation
@@ -118,15 +105,15 @@ See the [API documentation](/docs/reference/api.mdx)
118105

119106
## Contributors
120107

121-
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
108+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
122109

123110
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
124111
<!-- prettier-ignore -->
125112
<table><tr><td align="center"><a href="https://github.com/mpeyper"><img src="https://avatars0.githubusercontent.com/u/23029903?v=4" width="100px;" alt="Michael Peyper"/><br /><sub><b>Michael Peyper</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper" title="Code">💻</a> <a href="#design-mpeyper" title="Design">🎨</a> <a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper" title="Documentation">📖</a> <a href="#ideas-mpeyper" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-mpeyper" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#platform-mpeyper" title="Packaging/porting to new platform">📦</a> <a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper" title="Tests">⚠️</a> <a href="#tool-mpeyper" title="Tools">🔧</a></td><td align="center"><a href="https://github.com/otofu-square"><img src="https://avatars0.githubusercontent.com/u/10118235?v=4" width="100px;" alt="otofu-square"/><br /><sub><b>otofu-square</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=otofu-square" title="Code">💻</a></td><td align="center"><a href="https://github.com/ab18556"><img src="https://avatars2.githubusercontent.com/u/988696?v=4" width="100px;" alt="Patrick P. Henley"/><br /><sub><b>Patrick P. Henley</b></sub></a><br /><a href="#ideas-ab18556" title="Ideas, Planning, & Feedback">🤔</a> <a href="#review-ab18556" title="Reviewed Pull Requests">👀</a></td><td align="center"><a href="https://twitter.com/matiosfm"><img src="https://avatars3.githubusercontent.com/u/7120471?v=4" width="100px;" alt="Matheus Marques"/><br /><sub><b>Matheus Marques</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=marquesm91" title="Code">💻</a></td><td align="center"><a href="https://ca.linkedin.com/in/dhruvmpatel"><img src="https://avatars1.githubusercontent.com/u/19353311?v=4" width="100px;" alt="Dhruv Patel"/><br /><sub><b>Dhruv Patel</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/issues?q=author%3Adhruv-m-patel" title="Bug reports">🐛</a> <a href="#review-dhruv-m-patel" title="Reviewed Pull Requests">👀</a></td><td align="center"><a href="https://ntucker.true.io"><img src="https://avatars0.githubusercontent.com/u/866147?v=4" width="100px;" alt="Nathaniel Tucker"/><br /><sub><b>Nathaniel Tucker</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/issues?q=author%3Antucker" title="Bug reports">🐛</a> <a href="#review-ntucker" title="Reviewed Pull Requests">👀</a></td><td align="center"><a href="https://github.com/sgrishchenko"><img src="https://avatars3.githubusercontent.com/u/15995890?v=4" width="100px;" alt="Sergei Grishchenko"/><br /><sub><b>Sergei Grishchenko</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=sgrishchenko" title="Code">💻</a> <a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=sgrishchenko" title="Documentation">📖</a> <a href="#ideas-sgrishchenko" title="Ideas, Planning, & Feedback">🤔</a></td></tr><tr><td align="center"><a href="https://github.com/josepot"><img src="https://avatars1.githubusercontent.com/u/8620144?v=4" width="100px;" alt="Josep M Sobrepere"/><br /><sub><b>Josep M Sobrepere</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=josepot" title="Documentation">📖</a></td></tr></table>
126113

127114
<!-- ALL-CONTRIBUTORS-LIST:END -->
128115

129-
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!
130117

131118
## Issues
132119

docs/introduction/getting-started.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
name: Getting Started
3+
menu: Introduction
4+
route: '/'
5+
---
6+
7+
<div align="center">
8+
<h1>react-hooks-testing-library</h1>
9+
10+
<a href="https://www.emojione.com/emoji/1f40f">
11+
<img
12+
height="80"
13+
width="80"
14+
alt="ram"
15+
src="https://raw.githubusercontent.com/mpeyper/react-hooks-testing-library/master/other/ram.png"
16+
/>
17+
</a>
18+
19+
<p>
20+
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

docs/introduction/setup.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Setup
3+
menu: Introduction
4+
route: '/setup'
5+
---
6+
7+
# Setup
8+
9+
## Installation
10+
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`:
13+
14+
```sh
15+
npm install --save-dev react-hooks-testing-library
16+
```
17+
18+
## Testing Framework
19+
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.

doczrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default {
22
files: '**/*.{md,mdx}',
3+
src: 'docs',
34
dest: 'site',
45
public: './other',
56
ignore: ['CODE_OF_CONDUCT.md', 'CONTRIBUTING.md', 'LICENSE.md'],
@@ -15,7 +16,7 @@ export default {
1516
}
1617
},
1718
menu: [
18-
{ name: 'Getting Started' },
19+
{ name: 'Introduction', menu: ['Getting Started', 'Setup'] },
1920
{ name: 'Usage', menu: ['Basic Hooks', 'Advanced Hooks', 'Async Hooks'] },
2021
{ name: 'Examples' },
2122
{ name: 'Reference', menu: ['FAQ', 'Troubleshooting', 'API'] }

0 commit comments

Comments
 (0)