Open History to see the last commit
1import { createKey, sleep } from '@tanstack/query-test-utils'
2import { assertType, describe, expectTypeOf, it } from 'vitest'
3import { computed, reactive, ref } from 'vue-demi'
4import { queryOptions, skipToken, useQuery } from '..'
5import type { Ref } from 'vue-demi'
6import type { OmitKeyof, UseQueryOptions, UseQueryReturnType } from '..'
7β
8describe('useQuery', () => {
9 describe('Config object overload', () => {
10 it('should always define TData when initialData is provided as an object', () => {
11 const key = createKey()
12β
13 const { data } = reactive(
14 useQuery({
15 queryKey: key,
16 queryFn: () => {
17 return {
18 wow: true,
19 }
20 },
21 initialData: {
22 wow: true,
23 },
24 }),
25 )
26β
27 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
28 })
29β
30 it('should define TData when passed through queryOptions', () => {
31 const key = createKey()
32β
33 const options = queryOptions({
34 queryKey: key,
35 queryFn: () => {
36 return {
37 wow: true,
38 }
39 },
40 initialData: {
41 wow: true,
42 },
43 })
44 const { data } = reactive(useQuery(options))
45β
46 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
47 })
48β
49 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
50 const key = createKey()
51β
52 const options = queryOptions({
53 queryKey: key,
54 queryFn: () => Promise.resolve(1),
55 })
56β
57 const query = reactive(
58 useQuery({
59 ...options,
60 select: (data) => data > 1,
61 }),
62 )
63β
64 expectTypeOf(query.data).toEqualTypeOf<boolean | undefined>()
65 })
66β
67 it('should always define TData when initialData is provided as a function which ALWAYS returns the data', () => {
68 const key = createKey()
69β
70 const { data } = reactive(
71 useQuery({
72 queryKey: key,
73 queryFn: () => {
74 return {
75 wow: true,
76 }
77 },
78 initialData: () => ({
79 wow: true,
80 }),
81 }),
82 )
83β
84 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
85 })
86β
87 it('should have undefined in the TData union when initialData is NOT provided', () => {
88 const key = createKey()
89β
90 const { data } = reactive(
91 useQuery({
92 queryKey: key,
93 queryFn: () => {
94 return {
95 wow: true,
96 }
97 },
98 }),
99 )
100β
101 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
102 })
103β
104 it('should have undefined in the TData union when initialData is provided as a function which can return undefined', () => {
105 const key = createKey()
106β
107 const { data } = reactive(
108 useQuery({
109 queryKey: key,
110 queryFn: () => {
111 return {
112 wow: true,
113 }
114 },
115 initialData: () => undefined as { wow: boolean } | undefined,
116 }),
117 )
118β
119 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
120 })
121β
122 it('should narrow TData after an isSuccess check when initialData is provided as a function which can return undefined', () => {
123 const key = createKey()
124β
125 const { data, isSuccess } = reactive(
126 useQuery({
127 queryKey: key,
128 queryFn: () => {
129 return {
130 wow: true,
131 }
132 },
133 initialData: () => undefined as { wow: boolean } | undefined,
134 }),
135 )
136β
137 if (isSuccess) {
138 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
139 }
140 })
141β
142 it('should not have undefined in data when initialData is provided', () => {
143 const key = createKey()
144β
145 const { data } = reactive(
146 useQuery({
147 queryKey: key,
148 initialData: 42,
149 }),
150 )
151β
152 expectTypeOf(data).toEqualTypeOf<number>()
153 })
154 })
155β
156 describe('generic queryFn', () => {
157 it('should infer the result type from a generic query function', () => {
158 const key = createKey()
159β
160 function queryFn<T = string>(): Promise<T> {
161 return Promise.resolve({} as T)
162 }
163β
164 const query = reactive(
165 useQuery({
166 queryKey: key,
167 queryFn: () => queryFn(),
168 }),
169 )
170β
171 expectTypeOf(query.data).toEqualTypeOf<string | undefined>()
172 expectTypeOf(query.error).toEqualTypeOf<Error | null>()
173 })
174 })
175β
176 describe('generic queryKey inference (#8199)', () => {
177 it('should not error when wrapping useQuery in a composable that propagates a generic type to the queryKey', () => {
178 const basket = { fruit: 'apple', vegetable: 'broccoli' } as const
179β
180 function getBasket<T extends 'fruit' | 'vegetable'>(type: T) {
181 return basket[type]
182 }
183β
184 function useBasket<T extends 'fruit' | 'vegetable'>(type: T) {
185 return useQuery({
186 queryKey: ['basket', type] as const,
187 queryFn({ queryKey: [, t] }) {
188 return getBasket(t)
189 },
190 })
191 }
192β
193 assertType<UseQueryReturnType<'apple' | 'broccoli', Error>>(
194 useBasket('fruit'),
195 )
196 })
197 })
198β
199 describe('custom composable', () => {
200 it('should allow custom composable using UseQueryOptions', () => {
201 const key = createKey()
202β
203 const useCustomQuery = (
204 options?: OmitKeyof<
205 UseQueryOptions<string>,
206 'queryKey' | 'queryFn',
207 'safely'
208 >,
209 ) => {
210 return useQuery({
211 ...options,
212 queryKey: key,
213 queryFn: () => Promise.resolve('data'),
214 })
215 }
216β
217 const { data } = reactive(useCustomQuery())
218β
219 expectTypeOf(data).toEqualTypeOf<string | undefined>()
220 })
221 })
222β
223 describe('structuralSharing', () => {
224 it('should be able to use structuralSharing with unknown types', () => {
225 const key = createKey()
226β
227 // https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
228 useQuery({
229 queryKey: key,
230 queryFn: () => 5,
231 structuralSharing: (oldData, newData) => {
232 expectTypeOf(oldData).toBeUnknown()
233 expectTypeOf(newData).toBeUnknown()
234 return newData
235 },
236 })
237 })
238 })
239β
240 describe('state', () => {
241 it('should read data and isPending without unwrapping refs', () => {
242 const key = createKey()
243β
244 const { state } = useQuery({
245 queryKey: key,
246 queryFn: () => sleep(0).then(() => 'Some data'),
247 })
248β
249 expectTypeOf(state.status).toEqualTypeOf<
250 'pending' | 'error' | 'success'
251 >()
252 expectTypeOf(state.isPending).toEqualTypeOf<boolean>()
253 expectTypeOf(state.data).toEqualTypeOf<string | undefined>()
254 expectTypeOf(state.refetch).toBeFunction()
255 })
256β
257 it('should narrow data to TData when status is success', () => {
258 const key = createKey()
259β
260 const { state } = useQuery({
261 queryKey: key,
262 queryFn: () => sleep(0).then(() => 'Some data'),
263 })
264β
265 if (state.status === 'success') {
266 expectTypeOf(state.data).toEqualTypeOf<string>()
267 expectTypeOf(state.error).toEqualTypeOf<null>()
268 expectTypeOf(state.isPending).toEqualTypeOf<false>()
269 expectTypeOf(state.isSuccess).toEqualTypeOf<true>()
270 }
271 })
272β
273 it('should narrow data to TData when isSuccess is true', () => {
274 const key = createKey()
275β
276 const { state } = useQuery({
277 queryKey: key,
278 queryFn: () => sleep(0).then(() => 'Some data'),
279 })
280β
281 if (state.isSuccess) {
282 expectTypeOf(state.data).toEqualTypeOf<string>()
283 expectTypeOf(state.error).toEqualTypeOf<null>()
284 }
285 })
286β
287 it('should narrow data to undefined when status is pending', () => {
288 const key = createKey()
289β
290 const { state } = useQuery({
291 queryKey: key,
292 queryFn: () => sleep(0).then(() => 'Some data'),
293 })
294β
295 if (state.status === 'pending') {
296 expectTypeOf(state.data).toEqualTypeOf<undefined>()
297 expectTypeOf(state.error).toEqualTypeOf<null>()
298 expectTypeOf(state.isPending).toEqualTypeOf<true>()
299 }
300β
301 if (state.isPending) {
302 expectTypeOf(state.data).toEqualTypeOf<undefined>()
303 }
304 })
305β
306 it('should narrow error when status is error and keep data possibly undefined', () => {
307 const key = createKey()
308β
309 const { state } = useQuery({
310 queryKey: key,
311 queryFn: () => sleep(0).then(() => 'Some data'),
312 })
313β
314 if (state.status === 'error') {
315 expectTypeOf(state.error).toEqualTypeOf<Error>()
316 expectTypeOf(state.data).toEqualTypeOf<string | undefined>()
317 expectTypeOf(state.isPending).toEqualTypeOf<false>()
318 }
319 })
320β
321 it('should keep data defined on state when initialData is provided', () => {
322 const key = createKey()
323β
324 const { state } = useQuery({
325 queryKey: key,
326 queryFn: () => ({ wow: true }),
327 initialData: { wow: true },
328 })
329β
330 expectTypeOf(state.data).toEqualTypeOf<{ wow: boolean }>()
331β
332 if (state.status === 'success') {
333 expectTypeOf(state.data).toEqualTypeOf<{ wow: boolean }>()
334 }
335 })
336β
337 it('should keep a function returned by select as data', () => {
338 const key = createKey()
339β
340 const { state } = useQuery({
341 queryKey: key,
342 queryFn: () => 'fetched',
343 initialData: 'initial',
344 select: (value: string) => () => value,
345 })
346β
347 if (state.status === 'success') {
348 expectTypeOf(state.data).toEqualTypeOf<() => string>()
349 }
350 })
351 })
352β
353 describe('Discriminated union return type', () => {
354 it('should have possibly undefined data by default', () => {
355 const key = createKey()
356β
357 const query = reactive(
358 useQuery({
359 queryKey: key,
360 queryFn: () => sleep(0).then(() => 'Some data'),
361 }),
362 )
363β
364 expectTypeOf(query.data).toEqualTypeOf<string | undefined>()
365 })
366β
367 it('should have defined data when query is success', () => {
368 const key = createKey()
369β
370 const query = reactive(
371 useQuery({
372 queryKey: key,
373 queryFn: () => sleep(0).then(() => 'Some data'),
374 }),
375 )
376β
377 if (query.isSuccess) {
378 expectTypeOf(query.data).toEqualTypeOf<string>()
379 }
380 })
381β
382 it('should have null error when query is success', () => {
383 const key = createKey()
384β
385 const query = reactive(
386 useQuery({
387 queryKey: key,
388 queryFn: () => sleep(0).then(() => 'Some data'),
389 }),
390 )
391β
392 if (query.isSuccess) {
393 expectTypeOf(query.error).toEqualTypeOf<null>()
394 }
395 })
396β
397 it('should have undefined data when query is pending', () => {
398 const key = createKey()
399β
400 const query = reactive(
401 useQuery({
402 queryKey: key,
403 queryFn: () => sleep(0).then(() => 'Some data'),
404 }),
405 )
406β
407 if (query.isPending) {
408 expectTypeOf(query.data).toEqualTypeOf<undefined>()
409 }
410 })
411β
412 it('should have defined error when query is error', () => {
413 const key = createKey()
414β
415 const query = reactive(
416 useQuery({
417 queryKey: key,
418 queryFn: () => sleep(0).then(() => 'Some data'),
419 }),
420 )
421β
422 if (query.isError) {
423 expectTypeOf(query.error).toEqualTypeOf<Error>()
424 }
425 })
426β
427 it('should have data as a union of refs without reactive()', () => {
428 const key = createKey()
429β
430 const query = useQuery({
431 queryKey: key,
432 queryFn: () => sleep(0).then(() => 'Some data'),
433 })
434β
435 expectTypeOf(query.data).toEqualTypeOf<Ref<string> | Ref<undefined>>()
436 })
437β
438 it('should narrow data.value on an undefined check without reactive()', () => {
439 const key = createKey()
440β
441 const { data } = useQuery({
442 queryKey: key,
443 queryFn: () => sleep(0).then(() => 'Some data'),
444 })
445β
446 if (data.value !== undefined) {
447 expectTypeOf(data.value).toEqualTypeOf<string>()
448 expectTypeOf(data).toEqualTypeOf<Ref<string>>()
449 }
450 })
451 })
452β
453 describe('accept ref options', () => {
454 it('should accept ref options', () => {
455 const options = ref({
456 queryKey: createKey(),
457 queryFn: () => sleep(0).then(() => 'Some data'),
458 })
459β
460 const query = reactive(useQuery(options))
461β
462 if (query.isSuccess) {
463 expectTypeOf(query.data).toEqualTypeOf<string>()
464 }
465 })
466β
467 it('should accept computed options', () => {
468 const options = computed(() => ({
469 queryKey: createKey(),
470 queryFn: () => sleep(0).then(() => 'Some data'),
471 }))
472β
473 const query = reactive(useQuery(options))
474β
475 if (query.isSuccess) {
476 expectTypeOf(query.data).toEqualTypeOf<string>()
477 }
478 })
479β
480 it('should accept computed query options', () => {
481 const options = computed(() =>
482 queryOptions({
483 queryKey: createKey(),
484 queryFn: () => sleep(0).then(() => 'Some data'),
485 }),
486 )
487β
488 const query = reactive(useQuery(options))
489β
490 if (query.isSuccess) {
491 expectTypeOf(query.data).toEqualTypeOf<string>()
492 }
493 })
494 })
495β
496 describe('skipToken', () => {
497 it('should accept skipToken inside a whole-options getter', () => {
498 const id = ref<string | null>('1')
499β
500 const query = reactive(
501 useQuery(() => {
502 const current = id.value
503 return {
504 queryKey: ['post', current],
505 queryFn: current
506 ? () => sleep(0).then(() => 'Some data')
507 : skipToken,
508 }
509 }),
510 )
511β
512 if (query.isSuccess) {
513 expectTypeOf(query.data).toEqualTypeOf<string>()
514 }
515 })
516 })
517β
518 describe('queryKey reactivity rules', () => {
519 it('should accept a bare reactive getter for the whole queryKey array', () => {
520 const id = ref(1)
521 assertType(
522 useQuery({
523 queryKey: () => ['post', id.value],
524 queryFn: () => sleep(0).then(() => 'Some data'),
525 }),
526 )
527 })
528 })
529β
530 describe('select', () => {
531 it('should narrow data to the type select returns', () => {
532 const { data } = useQuery({
533 queryKey: createKey(),
534 queryFn: () => sleep(0).then(() => ['a', 'b', 'c']),
535 select: (posts) => posts.length,
536 })
537β
538 expectTypeOf(data.value).toEqualTypeOf<number | undefined>()
539 })
540 })
541})