Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { createKey } from '@tanstack/query-test-utils'
3import { skipToken, useSuspenseQueries } from '..'
4import { queryOptions } from '../queryOptions'
5import type { OmitKeyof } from '..'
6import type { UseQueryOptions, UseSuspenseQueryResult } from '../types'
7β
8describe('UseSuspenseQueries config object overload', () => {
9 it('should always define TData', () => {
10 const query1 = {
11 queryKey: createKey(),
12 queryFn: () => {
13 return {
14 wow: true,
15 }
16 },
17 initialData: {
18 wow: false,
19 },
20 }
21β
22 const query2 = {
23 queryKey: createKey(),
24 queryFn: () => 'Query Data',
25 }
26β
27 const queryResults = useSuspenseQueries({ queries: [query1, query2] })
28β
29 const query1Data = queryResults[0].data
30 const query2Data = queryResults[1].data
31β
32 expectTypeOf(query1Data).toEqualTypeOf<{ wow: boolean }>()
33 expectTypeOf(query2Data).toEqualTypeOf<string>()
34 })
35β
36 it('should define TData when passed through queryOptions', () => {
37 const options = queryOptions({
38 queryKey: createKey(),
39 queryFn: () => {
40 return {
41 wow: true,
42 }
43 },
44 })
45 const queryResults = useSuspenseQueries({ queries: [options] })
46β
47 const data = queryResults[0].data
48β
49 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
50 })
51β
52 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
53 const query1 = queryOptions({
54 queryKey: createKey(),
55 queryFn: () => Promise.resolve(1),
56 select: (data) => data > 1,
57 })
58β
59 const query2 = {
60 queryKey: createKey(),
61 queryFn: () => Promise.resolve(1),
62 select: (data: number) => data > 1,
63 }
64β
65 const queryResults = useSuspenseQueries({ queries: [query1, query2] })
66 const query1Data = queryResults[0].data
67 const query2Data = queryResults[1].data
68β
69 expectTypeOf(query1Data).toEqualTypeOf<boolean>()
70 expectTypeOf(query2Data).toEqualTypeOf<boolean>()
71 })
72β
73 it('should not have undefined in the TData union when initialData is provided as a function which can return undefined', () => {
74 const queryResults = useSuspenseQueries({
75 queries: [
76 {
77 queryKey: createKey(),
78 queryFn: () => {
79 return {
80 wow: true,
81 }
82 },
83 initialData: () => undefined as { wow: boolean } | undefined,
84 },
85 ],
86 })
87β
88 const data = queryResults[0].data
89β
90 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
91 })
92β
93 it('should not allow skipToken in queryFn', () => {
94 assertType(
95 useSuspenseQueries({
96 queries: [
97 {
98 queryKey: createKey(),
99 // @ts-expect-error
100 queryFn: skipToken,
101 },
102 ],
103 }),
104 )
105β
106 assertType(
107 useSuspenseQueries({
108 queries: [
109 {
110 queryKey: createKey(),
111 // @ts-expect-error
112 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
113 },
114 ],
115 }),
116 )
117 })
118β
119 it('should have correct TData type when conditional skipToken is passed', () => {
120 const queryResults = useSuspenseQueries({
121 queries: [
122 {
123 queryKey: createKey(),
124 // @ts-expect-error
125 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
126 },
127 ],
128 })
129β
130 const firstResult = queryResults[0]
131β
132 expectTypeOf(firstResult).toEqualTypeOf<
133 UseSuspenseQueryResult<number, Error>
134 >()
135 expectTypeOf(firstResult.data).toEqualTypeOf<number>()
136 })
137β
138 describe('custom hook', () => {
139 it('should allow custom hooks using UseQueryOptions', () => {
140 type Data = string
141β
142 const useCustomQueries = (
143 options?: OmitKeyof<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>,
144 ) => {
145 return useSuspenseQueries({
146 queries: [
147 {
148 ...options,
149 queryKey: createKey(),
150 queryFn: () => Promise.resolve('data'),
151 },
152 ],
153 })
154 }
155β
156 const queryResults = useCustomQueries()
157 const data = queryResults[0].data
158β
159 expectTypeOf(data).toEqualTypeOf<Data>()
160 })
161 })
162β
163 it('should return correct data for dynamic queries with mixed result types', () => {
164 const Queries1 = {
165 get: () =>
166 queryOptions({
167 queryKey: createKey(),
168 queryFn: () => Promise.resolve(1),
169 }),
170 }
171 const Queries2 = {
172 get: () =>
173 queryOptions({
174 queryKey: createKey(),
175 queryFn: () => Promise.resolve(true),
176 }),
177 }
178β
179 const queries1List = [1, 2, 3].map(() => ({ ...Queries1.get() }))
180 const result = useSuspenseQueries({
181 queries: [
182 ...queries1List,
183 {
184 ...Queries2.get(),
185 select(data: boolean) {
186 return data
187 },
188 },
189 ],
190 })
191β
192 expectTypeOf(result).toEqualTypeOf<
193 [
194 ...Array<UseSuspenseQueryResult<number, Error>>,
195 UseSuspenseQueryResult<boolean, Error>,
196 ]
197 >()
198 })
199β
200 it('should work with queryOptions with initialData on useSuspenseQueries', () => {
201 const query1 = queryOptions({
202 queryKey: createKey(),
203 queryFn: () => 'Query Data',
204 initialData: 'initial data',
205 })
206β
207 const queryResults = useSuspenseQueries({ queries: [query1] })
208 const query1Data = queryResults[0].data
209β
210 expectTypeOf(query1Data).toEqualTypeOf<string>()
211 })
212β
213 it('should not work with queryOptions with skipToken in queryFn on useSuspenseQueries', () => {
214 assertType(
215 useSuspenseQueries({
216 queries: [
217 // @ts-expect-error
218 queryOptions({
219 queryKey: createKey(),
220 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
221 }),
222 ],
223 }),
224 )
225β
226 assertType(
227 useSuspenseQueries({
228 queries: [
229 // @ts-expect-error
230 queryOptions({
231 queryKey: createKey(),
232 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
233 initialData: 5,
234 }),
235 ],
236 }),
237 )
238 })
239β
240 it('should not show type error when using rest queryOptions', () => {
241 assertType(
242 useSuspenseQueries({
243 queries: [
244 {
245 ...queryOptions({
246 queryKey: createKey(),
247 queryFn: () => 'Query Data',
248 }),
249 select(data: string) {
250 return data
251 },
252 },
253 ],
254 }),
255 )
256 })
257β
258 describe('select', () => {
259 // Inferring the `select` argument of an *inline* query object from its
260 // sibling `queryFn` is a known TypeScript limitation, because
261 // `useSuspenseQueries` infers its array generic from the argument itself.
262 // The two supported workarounds are to annotate the `select` parameter, or
263 // to define the query with the `queryOptions` helper.
264 // https://github.com/TanStack/query/issues/6556
265β
266 describe('without queryOptions (inline query object)', () => {
267 it('should leave the select argument as `unknown` without an annotation', () => {
268 useSuspenseQueries({
269 queries: [
270 {
271 queryKey: createKey(),
272 queryFn: () => Promise.resolve(1),
273 select: (data) => {
274 expectTypeOf(data).toBeUnknown()
275 // @ts-expect-error `data` is `unknown`, not the expected `number`
276 return data.toFixed()
277 },
278 },
279 ],
280 })
281 })
282β
283 it('should infer the result when the select parameter is annotated', () => {
284 const queryResults = useSuspenseQueries({
285 queries: [
286 {
287 queryKey: createKey(),
288 queryFn: () => Promise.resolve(1),
289 select: (data: number) => data.toFixed(),
290 },
291 ],
292 })
293 expectTypeOf(queryResults[0].data).toEqualTypeOf<string>()
294 })
295 })
296β
297 describe('with queryOptions passed directly', () => {
298 it('should infer the queryFn data as the result without select', () => {
299 const options = queryOptions({
300 queryKey: createKey(),
301 queryFn: () => Promise.resolve(1),
302 })
303 const queryResults = useSuspenseQueries({ queries: [options] })
304 expectTypeOf(queryResults[0].data).toEqualTypeOf<number>()
305 })
306β
307 it('should infer the select argument and the result with select', () => {
308 const options = queryOptions({
309 queryKey: createKey(),
310 queryFn: () => Promise.resolve(1),
311 select: (data) => {
312 expectTypeOf(data).toEqualTypeOf<number>()
313 return data.toFixed()
314 },
315 })
316 const queryResults = useSuspenseQueries({ queries: [options] })
317 expectTypeOf(queryResults[0].data).toEqualTypeOf<string>()
318 })
319β
320 it('should infer select when a base queryOptions is re-wrapped with queryOptions', () => {
321 const baseOptions = queryOptions({
322 queryKey: createKey(),
323 queryFn: () => Promise.resolve(1),
324 })
325 const queryResults = useSuspenseQueries({
326 queries: [
327 queryOptions({
328 ...baseOptions,
329 select: (data) => {
330 expectTypeOf(data).toEqualTypeOf<number>()
331 return data.toFixed()
332 },
333 }),
334 baseOptions,
335 ],
336 })
337 expectTypeOf(queryResults[0].data).toEqualTypeOf<string>()
338 expectTypeOf(queryResults[1].data).toEqualTypeOf<number>()
339 })
340β
341 it('should infer an overriding select when a queryOptions with a select is re-wrapped with queryOptions', () => {
342 const baseOptions = queryOptions({
343 queryKey: createKey(),
344 queryFn: () => Promise.resolve(1),
345 select: (data) => data + 1,
346 })
347 const queryResults = useSuspenseQueries({
348 queries: [
349 queryOptions({
350 ...baseOptions,
351 select: (data) => {
352 expectTypeOf(data).toEqualTypeOf<number>()
353 return data.toFixed()
354 },
355 }),
356 ],
357 })
358 expectTypeOf(queryResults[0].data).toEqualTypeOf<string>()
359 })
360 })
361β
362 describe('with queryOptions spread into an inline query object', () => {
363 it('should leave an unannotated select untyped without select in the factory', () => {
364 const options = queryOptions({
365 queryKey: createKey(),
366 queryFn: () => Promise.resolve(1),
367 })
368 useSuspenseQueries({
369 queries: [
370 {
371 ...options,
372 // @ts-expect-error Without an annotation the inline `select` parameter `data` implicitly has type `any`
373 select: (data) => {
374 expectTypeOf(data).toBeAny()
375 return data
376 },
377 },
378 ],
379 })
380 })
381β
382 it('should compile an annotated select without select in the factory', () => {
383 const options = queryOptions({
384 queryKey: createKey(),
385 queryFn: () => Promise.resolve(1),
386 })
387 const queryResults = useSuspenseQueries({
388 queries: [{ ...options, select: (data: number) => data.toFixed() }],
389 })
390 expectTypeOf(queryResults[0].data).toEqualTypeOf<string>()
391 })
392β
393 it('should leave an unannotated overriding select untyped with select in the factory', () => {
394 const options = queryOptions({
395 queryKey: createKey(),
396 queryFn: () => Promise.resolve(1),
397 select: (data) => data + 1,
398 })
399 useSuspenseQueries({
400 queries: [
401 {
402 ...options,
403 // @ts-expect-error Without an annotation the inline `select` parameter `data` implicitly has type `any`
404 select: (data) => {
405 expectTypeOf(data).toBeAny()
406 return data
407 },
408 },
409 ],
410 })
411 })
412β
413 it('should compile an annotated overriding select with select in the factory', () => {
414 const options = queryOptions({
415 queryKey: createKey(),
416 queryFn: () => Promise.resolve(1),
417 select: (data) => data + 1,
418 })
419 const queryResults = useSuspenseQueries({
420 queries: [{ ...options, select: (data: number) => data.toFixed() }],
421 })
422 expectTypeOf(queryResults[0].data).toEqualTypeOf<string>()
423 })
424 })
425 })
426})