Skip to content

Commit 35efec7

Browse files
docs: Guide page for testing - (#1244)
* Guide page for testing - first draft * Update docs/src/pages/docs/guides/testing.md Co-authored-by: Tanner Linsley <[email protected]>
1 parent 61e7b9b commit 35efec7

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

docs/src/manifests/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
159159
"path": "/docs/guides/suspense",
160160
"editUrl": "/docs/guides/suspense.md"
161161
},
162+
{
163+
"title": "Testing",
164+
"path": "/docs/guides/testing",
165+
"editUrl": "/docs/guides/testing.md"
166+
},
162167
{
163168
"title": "Does this replace [Redux, MobX, etc]?",
164169
"path": "/docs/guides/does-this-replace-client-state",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
id: testing
3+
title: Testing
4+
---
5+
6+
React Query works by means of hooks - either the ones we offer or custom ones that wrap around them.
7+
8+
Writing unit tests for these custom hooks can be done by means of the [React Hooks Testing Library](https://react-hooks-testing-library.com/) library.
9+
10+
Install this by running:
11+
12+
```sh
13+
npm install @testing-library/react-hooks react-test-renderer --save-dev
14+
```
15+
16+
(The `react-test-renderer` library is needed as a peer dependency of `@testing-library/react-hooks`, and needs to correspond to the version of React that you are using.)
17+
18+
## Our First Test
19+
20+
Once installed, a simple test can be written. Given the following custom hook:
21+
22+
```
23+
export function useCustomHook() {
24+
const { data } = useQuery('customHook', () => 'Hello');
25+
return data;
26+
}
27+
```
28+
29+
We can write a test for this as follows:
30+
31+
```
32+
const queryCache = new QueryCache();
33+
const wrapper = ({ children }) => (
34+
<ReactQueryCacheProvider queryCache={queryCache}>
35+
{children}
36+
</ReactQueryCacheProvider>
37+
);
38+
39+
const { result } = renderHook(() => useCustomHook(), { wrapper });
40+
41+
expect(result.current).toEqual('Hello');
42+
```
43+
44+
Note that we provide a custom wrapper that builds the `QueryCache` and `ReactQueryCacheProvider`. This helps to ensure that our test is completely isolated from any other tests.
45+
46+
It is possible to write this wrapper only once, but if so we need to ensure that the `QueryCache` gets cleared before every test, and that tests don't run in parallel otherwise one test will influence the results of others.
47+
48+
## Testing Network Calls
49+
50+
The primary use for React Query is to cache network requests, so it's important that we can test our code is making the correct network requests in the first place.
51+
52+
There are plenty of ways that these can be tested, but for this example we are going to use [nock](https://www.npmjs.com/package/nock).
53+
54+
Given the following custom hook:
55+
56+
```
57+
function useFetchData() {
58+
const { data } = useQuery('fetchData', () => request('/api/data'));
59+
return data;
60+
}
61+
```
62+
63+
We can write a test for this as follows:
64+
65+
```
66+
const queryCache = new QueryCache();
67+
const wrapper = ({ children }) => (
68+
<ReactQueryCacheProvider queryCache={queryCache}>
69+
{children}
70+
</ReactQueryCacheProvider>
71+
);
72+
73+
const expectation = nock('http://example.com')
74+
.get('/api/data')
75+
.reply(200, {
76+
answer: 42
77+
});
78+
79+
const { result, waitFor } = renderHook(() => useFetchData(), { wrapper });
80+
81+
await waitFor(() => {
82+
return result.current.isSuccess();
83+
});
84+
85+
expect(result.current).toEqual({answer: 42});
86+
```
87+
88+
Here we are making use of `waitFor` and waiting until our Nock expectation indicates that it has been called. This way we know that our hook has finished and should have the correct data.

0 commit comments

Comments
 (0)