Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useQuery.test-d.ts

541 lines14.2 KB

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})