@@ -4,10 +4,39 @@ import type { paths } from "./fixtures/api.js";
4
4
import createClient from "../src/index.js" ;
5
5
import createFetchClient from "openapi-fetch" ;
6
6
import { fireEvent , render , renderHook , screen , waitFor , act } from "@testing-library/react" ;
7
- import { QueryClient , QueryClientProvider , useQueries } from "@tanstack/react-query" ;
7
+ import {
8
+ QueryClient ,
9
+ QueryClientProvider ,
10
+ useQueries ,
11
+ useQuery ,
12
+ useSuspenseQuery ,
13
+ skipToken ,
14
+ } from "@tanstack/react-query" ;
8
15
import { Suspense , type ReactNode } from "react" ;
9
16
import { ErrorBoundary } from "react-error-boundary" ;
10
17
18
+ type minimalGetPaths = {
19
+ // Without parameters.
20
+ "/foo" : {
21
+ get : {
22
+ responses : {
23
+ 200 : { content : { "application/json" : true } } ;
24
+ 500 : { content : { "application/json" : false } } ;
25
+ } ;
26
+ } ;
27
+ } ;
28
+ // With some parameters (makes init required) and different responses.
29
+ "/bar" : {
30
+ get : {
31
+ parameters : { query : { } } ;
32
+ responses : {
33
+ 200 : { content : { "application/json" : "bar 200" } } ;
34
+ 500 : { content : { "application/json" : "bar 500" } } ;
35
+ } ;
36
+ } ;
37
+ } ;
38
+ } ;
39
+
11
40
const queryClient = new QueryClient ( {
12
41
defaultOptions : {
13
42
queries : {
@@ -27,9 +56,7 @@ const fetchInfinite = async () => {
27
56
28
57
beforeAll ( ( ) => {
29
58
server . listen ( {
30
- onUnhandledRequest : ( request ) => {
31
- throw new Error ( `No request handler found for ${ request . method } ${ request . url } ` ) ;
32
- } ,
59
+ onUnhandledRequest : "error" ,
33
60
} ) ;
34
61
} ) ;
35
62
@@ -96,7 +123,7 @@ describe("client", () => {
96
123
expect ( data ) . toEqual ( response ) ;
97
124
} ) ;
98
125
99
- it ( "returns query options that can be passed to useQueries and have correct types inferred " , async ( ) => {
126
+ it ( "returns query options that can be passed to useQueries" , async ( ) => {
100
127
const fetchClient = createFetchClient < paths > ( { baseUrl, fetch : fetchInfinite } ) ;
101
128
const client = createClient ( fetchClient ) ;
102
129
@@ -150,6 +177,60 @@ describe("client", () => {
150
177
// Generated different queryKey for each query.
151
178
expect ( queryClient . isFetching ( ) ) . toBe ( 4 ) ;
152
179
} ) ;
180
+
181
+ it ( "returns query options that can be passed to useQuery" , async ( ) => {
182
+ const SKIP = { queryKey : [ ] as any , queryFn : skipToken } as const ;
183
+
184
+ const fetchClient = createFetchClient < minimalGetPaths > ( { baseUrl } ) ;
185
+ const client = createClient ( fetchClient ) ;
186
+
187
+ const { result } = renderHook (
188
+ ( ) =>
189
+ useQuery (
190
+ // biome-ignore lint/correctness/noConstantCondition: it's just here to test types
191
+ false
192
+ ? {
193
+ ...client . queryOptions ( "get" , "/foo" ) ,
194
+ select : ( data ) => {
195
+ expectTypeOf ( data ) . toEqualTypeOf < true > ( ) ;
196
+
197
+ return "select(true)" as const ;
198
+ } ,
199
+ }
200
+ : SKIP ,
201
+ ) ,
202
+ { wrapper } ,
203
+ ) ;
204
+
205
+ expectTypeOf ( result . current . data ) . toEqualTypeOf < "select(true)" | undefined > ( ) ;
206
+ expectTypeOf ( result . current . error ) . toEqualTypeOf < false | null > ( ) ;
207
+ } ) ;
208
+
209
+ it ( "returns query options that can be passed to useSuspenseQuery" , async ( ) => {
210
+ const fetchClient = createFetchClient < minimalGetPaths > ( {
211
+ baseUrl,
212
+ fetch : ( ) => Promise . resolve ( Response . json ( true ) ) ,
213
+ } ) ;
214
+ const client = createClient ( fetchClient ) ;
215
+
216
+ const { result } = renderHook (
217
+ ( ) =>
218
+ useSuspenseQuery ( {
219
+ ...client . queryOptions ( "get" , "/foo" ) ,
220
+ select : ( data ) => {
221
+ expectTypeOf ( data ) . toEqualTypeOf < true > ( ) ;
222
+
223
+ return "select(true)" as const ;
224
+ } ,
225
+ } ) ,
226
+ { wrapper } ,
227
+ ) ;
228
+
229
+ await waitFor ( ( ) => expect ( result . current ) . not . toBeNull ( ) ) ;
230
+
231
+ expectTypeOf ( result . current . data ) . toEqualTypeOf < "select(true)" > ( ) ;
232
+ expectTypeOf ( result . current . error ) . toEqualTypeOf < false | null > ( ) ;
233
+ } ) ;
153
234
} ) ;
154
235
155
236
describe ( "useQuery" , ( ) => {
@@ -203,7 +284,7 @@ describe("client", () => {
203
284
} ) ;
204
285
205
286
it ( "should infer correct data and error type" , async ( ) => {
206
- const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
287
+ const fetchClient = createFetchClient < paths > ( { baseUrl, fetch : fetchInfinite } ) ;
207
288
const client = createClient ( fetchClient ) ;
208
289
209
290
const { result } = renderHook ( ( ) => client . useQuery ( "get" , "/string-array" ) , {
0 commit comments