@@ -4,10 +4,39 @@ import type { paths } from "./fixtures/api.js";
44import createClient from "../src/index.js" ;
55import createFetchClient from "openapi-fetch" ;
66import { 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" ;
815import { Suspense , type ReactNode } from "react" ;
916import { ErrorBoundary } from "react-error-boundary" ;
1017
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+
1140const queryClient = new QueryClient ( {
1241 defaultOptions : {
1342 queries : {
@@ -27,9 +56,7 @@ const fetchInfinite = async () => {
2756
2857beforeAll ( ( ) => {
2958 server . listen ( {
30- onUnhandledRequest : ( request ) => {
31- throw new Error ( `No request handler found for ${ request . method } ${ request . url } ` ) ;
32- } ,
59+ onUnhandledRequest : "error" ,
3360 } ) ;
3461} ) ;
3562
@@ -96,7 +123,7 @@ describe("client", () => {
96123 expect ( data ) . toEqual ( response ) ;
97124 } ) ;
98125
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 ( ) => {
100127 const fetchClient = createFetchClient < paths > ( { baseUrl, fetch : fetchInfinite } ) ;
101128 const client = createClient ( fetchClient ) ;
102129
@@ -150,6 +177,60 @@ describe("client", () => {
150177 // Generated different queryKey for each query.
151178 expect ( queryClient . isFetching ( ) ) . toBe ( 4 ) ;
152179 } ) ;
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+ } ) ;
153234 } ) ;
154235
155236 describe ( "useQuery" , ( ) => {
@@ -203,7 +284,7 @@ describe("client", () => {
203284 } ) ;
204285
205286 it ( "should infer correct data and error type" , async ( ) => {
206- const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
287+ const fetchClient = createFetchClient < paths > ( { baseUrl, fetch : fetchInfinite } ) ;
207288 const client = createClient ( fetchClient ) ;
208289
209290 const { result } = renderHook ( ( ) => client . useQuery ( "get" , "/string-array" ) , {
0 commit comments