Skip to content

Commit e63aaba

Browse files
committed
tests: added tests react query hook
1 parent a2c3cd7 commit e63aaba

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hmarques98/react-native-template-typescript",
3-
"version": "1.0.36",
3+
"version": "1.0.37",
44
"description": "A minimal template with good architecture and common packages to let you focus on writing features right away.",
55
"scripts": {
66
"publish": ""
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React from 'react';
2+
import { renderHook, act } from '@testing-library/react-hooks';
3+
import { log } from '@utils/console';
4+
import useReactQuery from 'hooks/useReactQuery';
5+
import { QueryClient, QueryClientProvider } from 'react-query';
6+
7+
const queryClient = new QueryClient();
8+
9+
const QueryProvider = ({ children }: { children: React.ReactNode }) => {
10+
return (
11+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
12+
);
13+
};
14+
15+
const mockData = {
16+
data: {
17+
name: 'Luke Skywalker',
18+
height: '172',
19+
mass: '77',
20+
hair_color: 'blond',
21+
skin_color: 'fair',
22+
eye_color: 'blue',
23+
birth_year: '19BBY',
24+
gender: 'male',
25+
homeworld: 'https://swapi.dev/api/planets/1/',
26+
films: [
27+
'https://swapi.dev/api/films/1/',
28+
'https://swapi.dev/api/films/2/',
29+
'https://swapi.dev/api/films/3/',
30+
'https://swapi.dev/api/films/6/',
31+
],
32+
species: [],
33+
vehicles: [
34+
'https://swapi.dev/api/vehicles/14/',
35+
'https://swapi.dev/api/vehicles/30/',
36+
],
37+
starships: [
38+
'https://swapi.dev/api/starships/12/',
39+
'https://swapi.dev/api/starships/22/',
40+
],
41+
created: '2014-12-09T13:50:51.644000Z',
42+
edited: '2014-12-20T21:17:56.891000Z',
43+
url: 'https://swapi.dev/api/people/1/',
44+
},
45+
};
46+
47+
describe('useReactQuery Hook', () => {
48+
test('should render hook data', async () => {
49+
const { result, waitForValueToChange } = renderHook(
50+
() =>
51+
useReactQuery<{ data: any }>({
52+
path: 'people/1/',
53+
queryName: 'people',
54+
}),
55+
{
56+
wrapper: QueryProvider,
57+
},
58+
);
59+
60+
await waitForValueToChange(() => result.current.data);
61+
62+
expect(result.current.data).toMatchObject(mockData.data);
63+
});
64+
65+
test('should to call refetch from hook ', async () => {
66+
const { result, waitForNextUpdate } = renderHook(
67+
() =>
68+
useReactQuery<{ data: any }>({
69+
path: 'people/1/',
70+
queryName: 'people',
71+
}),
72+
{
73+
wrapper: QueryProvider,
74+
},
75+
);
76+
const mockRefetch = jest.fn().mockImplementationOnce(()=>result.current.refetch())
77+
78+
act(() => {
79+
mockRefetch()
80+
});
81+
82+
await waitForNextUpdate({timeout:5000});
83+
84+
expect(mockRefetch).toBeCalledTimes(1);
85+
});
86+
});

0 commit comments

Comments
 (0)