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

useQuery.test-d.ts

428 lines10.9 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { computed, reactive, ref } from 'vue-demi'
3import { queryKey, sleep } from '@tanstack/query-test-utils'
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('TData should always be defined when initialData is provided as an object', () => {
11 const key = queryKey()
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('TData should be defined when passed through queryOptions', () => {
31 const key = queryKey()
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 = queryKey()
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('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
68 const key = queryKey()
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('TData should have undefined in the union when initialData is NOT provided', () => {
88 const key = queryKey()
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('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
105 const key = queryKey()
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('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
123 const key = queryKey()
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('data should not have undefined when initialData is provided', () => {
143 const key = queryKey()
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 = queryKey()
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 = queryKey()
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 = queryKey()
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('Discriminated union return type', () => {
241 it('data should be possibly undefined by default', () => {
242 const key = queryKey()
243​
244 const query = reactive(
245 useQuery({
246 queryKey: key,
247 queryFn: () => sleep(0).then(() => 'Some data'),
248 }),
249 )
250​
251 expectTypeOf(query.data).toEqualTypeOf<string | undefined>()
252 })
253​
254 it('data should be defined when query is success', () => {
255 const key = queryKey()
256​
257 const query = reactive(
258 useQuery({
259 queryKey: key,
260 queryFn: () => sleep(0).then(() => 'Some data'),
261 }),
262 )
263​
264 if (query.isSuccess) {
265 expectTypeOf(query.data).toEqualTypeOf<string>()
266 }
267 })
268​
269 it('error should be null when query is success', () => {
270 const key = queryKey()
271​
272 const query = reactive(
273 useQuery({
274 queryKey: key,
275 queryFn: () => sleep(0).then(() => 'Some data'),
276 }),
277 )
278​
279 if (query.isSuccess) {
280 expectTypeOf(query.error).toEqualTypeOf<null>()
281 }
282 })
283​
284 it('data should be undefined when query is pending', () => {
285 const key = queryKey()
286​
287 const query = reactive(
288 useQuery({
289 queryKey: key,
290 queryFn: () => sleep(0).then(() => 'Some data'),
291 }),
292 )
293​
294 if (query.isPending) {
295 expectTypeOf(query.data).toEqualTypeOf<undefined>()
296 }
297 })
298​
299 it('error should be defined when query is error', () => {
300 const key = queryKey()
301​
302 const query = reactive(
303 useQuery({
304 queryKey: key,
305 queryFn: () => sleep(0).then(() => 'Some data'),
306 }),
307 )
308​
309 if (query.isError) {
310 expectTypeOf(query.error).toEqualTypeOf<Error>()
311 }
312 })
313​
314 it('data should be a union of refs without reactive()', () => {
315 const key = queryKey()
316​
317 const query = useQuery({
318 queryKey: key,
319 queryFn: () => sleep(0).then(() => 'Some data'),
320 })
321​
322 expectTypeOf(query.data).toEqualTypeOf<Ref<string> | Ref<undefined>>()
323 })
324​
325 it('data.value should narrow on an undefined check without reactive()', () => {
326 const key = queryKey()
327​
328 const { data } = useQuery({
329 queryKey: key,
330 queryFn: () => sleep(0).then(() => 'Some data'),
331 })
332​
333 if (data.value !== undefined) {
334 expectTypeOf(data.value).toEqualTypeOf<string>()
335 expectTypeOf(data).toEqualTypeOf<Ref<string>>()
336 }
337 })
338 })
339​
340 describe('accept ref options', () => {
341 it('should accept ref options', () => {
342 const options = ref({
343 queryKey: queryKey(),
344 queryFn: () => sleep(0).then(() => 'Some data'),
345 })
346​
347 const query = reactive(useQuery(options))
348​
349 if (query.isSuccess) {
350 expectTypeOf(query.data).toEqualTypeOf<string>()
351 }
352 })
353​
354 it('should accept computed options', () => {
355 const options = computed(() => ({
356 queryKey: queryKey(),
357 queryFn: () => sleep(0).then(() => 'Some data'),
358 }))
359​
360 const query = reactive(useQuery(options))
361​
362 if (query.isSuccess) {
363 expectTypeOf(query.data).toEqualTypeOf<string>()
364 }
365 })
366​
367 it('should accept computed query options', () => {
368 const options = computed(() =>
369 queryOptions({
370 queryKey: queryKey(),
371 queryFn: () => sleep(0).then(() => 'Some data'),
372 }),
373 )
374​
375 const query = reactive(useQuery(options))
376​
377 if (query.isSuccess) {
378 expectTypeOf(query.data).toEqualTypeOf<string>()
379 }
380 })
381 })
382​
383 describe('skipToken', () => {
384 it('should accept skipToken inside a whole-options getter', () => {
385 const id = ref<string | null>('1')
386​
387 const query = reactive(
388 useQuery(() => {
389 const current = id.value
390 return {
391 queryKey: ['post', current],
392 queryFn: current
393 ? () => sleep(0).then(() => 'Some data')
394 : skipToken,
395 }
396 }),
397 )
398​
399 if (query.isSuccess) {
400 expectTypeOf(query.data).toEqualTypeOf<string>()
401 }
402 })
403 })
404​
405 describe('queryKey reactivity rules', () => {
406 it('should accept a bare reactive getter for the whole queryKey array', () => {
407 const id = ref(1)
408 assertType(
409 useQuery({
410 queryKey: () => ['post', id.value],
411 queryFn: () => sleep(0).then(() => 'Some data'),
412 }),
413 )
414 })
415 })
416​
417 describe('select', () => {
418 it('should narrow data to the type select returns', () => {
419 const { data } = useQuery({
420 queryKey: queryKey(),
421 queryFn: () => sleep(0).then(() => ['a', 'b', 'c']),
422 select: (posts) => posts.length,
423 })
424​
425 expectTypeOf(data.value).toEqualTypeOf<number | undefined>()
426 })
427 })
428})