@@ -7,6 +7,18 @@ import { renderHook, waitFor } from "@testing-library/react";
7
7
import { QueryClient , QueryClientProvider } from "@tanstack/react-query" ;
8
8
import React , { type ReactNode } from "react" ;
9
9
10
+ const queryClient = new QueryClient ( {
11
+ defaultOptions : {
12
+ queries : {
13
+ retry : false ,
14
+ } ,
15
+ } ,
16
+ } ) ;
17
+
18
+ const wrapper = ( { children } : { children : ReactNode } ) => (
19
+ < QueryClientProvider client = { queryClient } > { children } </ QueryClientProvider >
20
+ ) ;
21
+
10
22
beforeAll ( ( ) => {
11
23
server . listen ( {
12
24
onUnhandledRequest : ( request ) => {
@@ -15,16 +27,13 @@ beforeAll(() => {
15
27
} ) ;
16
28
} ) ;
17
29
18
- afterEach ( ( ) => server . resetHandlers ( ) ) ;
30
+ afterEach ( ( ) => {
31
+ server . resetHandlers ( ) ;
32
+ queryClient . removeQueries ( ) ;
33
+ } ) ;
19
34
20
35
afterAll ( ( ) => server . close ( ) ) ;
21
36
22
- const queryClient = new QueryClient ( ) ;
23
-
24
- const wrapper = ( { children } : { children : ReactNode } ) => (
25
- < QueryClientProvider client = { queryClient } > { children } </ QueryClientProvider >
26
- ) ;
27
-
28
37
describe ( "client" , ( ) => {
29
38
it ( "generates all proper functions" , ( ) => {
30
39
const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
@@ -35,25 +44,90 @@ describe("client", () => {
35
44
} ) ;
36
45
37
46
describe ( "useQuery" , ( ) => {
38
- it ( "should work " , async ( ) => {
47
+ it ( "should resolve data properly and have error as null when successfull request " , async ( ) => {
39
48
const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
40
49
const client = createClient ( fetchClient ) ;
41
50
42
51
useMockRequestHandler ( {
43
52
baseUrl,
44
53
method : "get" ,
45
- path : "/self " ,
54
+ path : "/string-array " ,
46
55
status : 200 ,
47
- body : { message : "OK" } ,
56
+ body : [ "one" , "two" , "three" ] ,
48
57
} ) ;
49
58
50
- const { result } = renderHook ( ( ) => client . useQuery ( "get" , "/self " ) , {
59
+ const { result } = renderHook ( ( ) => client . useQuery ( "get" , "/string-array " ) , {
51
60
wrapper,
52
61
} ) ;
53
62
54
- await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) ) ;
63
+ await waitFor ( ( ) => expect ( result . current . isFetching ) . toBe ( false ) ) ;
55
64
56
- expect ( result . current . data ) . toEqual ( { message : "OK" } ) ;
65
+ const { data, error } = result . current ;
66
+
67
+ // … is initially possibly undefined
68
+ // @ts -expect-error
69
+ expect ( data [ 0 ] ) . toBe ( "one" ) ;
70
+ expect ( error ) . toBeNull ( ) ;
71
+ } ) ;
72
+
73
+ it ( "should resolve error properlly and have undefined data when failed request" , async ( ) => {
74
+ const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
75
+ const client = createClient ( fetchClient ) ;
76
+
77
+ useMockRequestHandler ( {
78
+ baseUrl,
79
+ method : "get" ,
80
+ path : "/string-array" ,
81
+ status : 500 ,
82
+ body : { code : 500 , message : "Something went wrong" } ,
83
+ } ) ;
84
+
85
+ const { result } = renderHook ( ( ) => client . useQuery ( "get" , "/string-array" ) , {
86
+ wrapper,
87
+ } ) ;
88
+
89
+ await waitFor ( ( ) => expect ( result . current . isFetching ) . toBe ( false ) ) ;
90
+
91
+ const { data, error } = result . current ;
92
+
93
+ expect ( error ?. message ) . toBe ( "Something went wrong" ) ;
94
+ expect ( data ) . toBeUndefined ( ) ;
95
+ } ) ;
96
+
97
+ it ( "should infer correct data and error type" , async ( ) => {
98
+ const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
99
+ const client = createClient ( fetchClient ) ;
100
+
101
+ const { result } = renderHook ( ( ) => client . useQuery ( "get" , "/string-array" ) , {
102
+ wrapper,
103
+ } ) ;
104
+
105
+ const { data, error } = result . current ;
106
+
107
+ expectTypeOf ( data ) . toEqualTypeOf < string [ ] | undefined > ( ) ;
108
+ expectTypeOf ( error ) . toEqualTypeOf < { code : number ; message : string } | null > ( ) ;
109
+ } ) ;
110
+
111
+ describe ( "params" , ( ) => {
112
+ it ( "typechecks" , async ( ) => {
113
+ const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
114
+ const client = createClient ( fetchClient ) ;
115
+
116
+ useMockRequestHandler ( {
117
+ baseUrl,
118
+ method : "get" ,
119
+ path : "/blogposts/:post_id" ,
120
+ status : 200 ,
121
+ body : { message : "OK" } ,
122
+ } ) ;
123
+
124
+ // expect error on missing 'params'
125
+ // @ts -expect-error
126
+ const { result } = renderHook ( ( ) => client . useQuery ( "get" , "/blogposts/{post_id}" ) , {
127
+ wrapper,
128
+ } ) ;
129
+ await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) ) ;
130
+ } ) ;
57
131
} ) ;
58
132
} ) ;
59
133
0 commit comments