Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { computed, reactive, ref } from 'vue-demi'
3import { queryKey, sleep } from '@tanstack/query-test-utils'
4import { queryOptions, 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 queryKey inference (#8199)', () => {
157 it('should not error when wrapping useQuery in a composable that propagates a generic type to the queryKey', () => {
158 const basket = { fruit: 'apple', vegetable: 'broccoli' } as const
159β
160 function getBasket<T extends 'fruit' | 'vegetable'>(type: T) {
161 return basket[type]
162 }
163β
164 function useBasket<T extends 'fruit' | 'vegetable'>(type: T) {
165 return useQuery({
166 queryKey: ['basket', type] as const,
167 queryFn({ queryKey: [, t] }) {
168 return getBasket(t)
169 },
170 })
171 }
172β
173 assertType<UseQueryReturnType<'apple' | 'broccoli', Error>>(
174 useBasket('fruit'),
175 )
176 })
177 })
178β
179 describe('custom composable', () => {
180 it('should allow custom composable using UseQueryOptions', () => {
181 const key = queryKey()
182β
183 const useCustomQuery = (
184 options?: OmitKeyof<
185 UseQueryOptions<string>,
186 'queryKey' | 'queryFn',
187 'safely'
188 >,
189 ) => {
190 return useQuery({
191 ...options,
192 queryKey: key,
193 queryFn: () => Promise.resolve('data'),
194 })
195 }
196β
197 const { data } = reactive(useCustomQuery())
198β
199 expectTypeOf(data).toEqualTypeOf<string | undefined>()
200 })
201 })
202β
203 describe('structuralSharing', () => {
204 it('should be able to use structuralSharing with unknown types', () => {
205 const key = queryKey()
206β
207 // https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
208 useQuery({
209 queryKey: key,
210 queryFn: () => 5,
211 structuralSharing: (oldData, newData) => {
212 expectTypeOf(oldData).toBeUnknown()
213 expectTypeOf(newData).toBeUnknown()
214 return newData
215 },
216 })
217 })
218 })
219β
220 describe('Discriminated union return type', () => {
221 it('data should be possibly undefined by default', () => {
222 const key = queryKey()
223β
224 const query = reactive(
225 useQuery({
226 queryKey: key,
227 queryFn: () => sleep(0).then(() => 'Some data'),
228 }),
229 )
230β
231 expectTypeOf(query.data).toEqualTypeOf<string | undefined>()
232 })
233β
234 it('data should be defined when query is success', () => {
235 const key = queryKey()
236β
237 const query = reactive(
238 useQuery({
239 queryKey: key,
240 queryFn: () => sleep(0).then(() => 'Some data'),
241 }),
242 )
243β
244 if (query.isSuccess) {
245 expectTypeOf(query.data).toEqualTypeOf<string>()
246 }
247 })
248β
249 it('error should be null when query is success', () => {
250 const key = queryKey()
251β
252 const query = reactive(
253 useQuery({
254 queryKey: key,
255 queryFn: () => sleep(0).then(() => 'Some data'),
256 }),
257 )
258β
259 if (query.isSuccess) {
260 expectTypeOf(query.error).toEqualTypeOf<null>()
261 }
262 })
263β
264 it('data should be undefined when query is pending', () => {
265 const key = queryKey()
266β
267 const query = reactive(
268 useQuery({
269 queryKey: key,
270 queryFn: () => sleep(0).then(() => 'Some data'),
271 }),
272 )
273β
274 if (query.isPending) {
275 expectTypeOf(query.data).toEqualTypeOf<undefined>()
276 }
277 })
278β
279 it('error should be defined when query is error', () => {
280 const key = queryKey()
281β
282 const query = reactive(
283 useQuery({
284 queryKey: key,
285 queryFn: () => sleep(0).then(() => 'Some data'),
286 }),
287 )
288β
289 if (query.isError) {
290 expectTypeOf(query.error).toEqualTypeOf<Error>()
291 }
292 })
293β
294 it('data should be a union of refs without reactive()', () => {
295 const key = queryKey()
296β
297 const query = useQuery({
298 queryKey: key,
299 queryFn: () => sleep(0).then(() => 'Some data'),
300 })
301β
302 expectTypeOf(query.data).toEqualTypeOf<Ref<string> | Ref<undefined>>()
303 })
304β
305 it('data.value should narrow on an undefined check without reactive()', () => {
306 const key = queryKey()
307β
308 const { data } = useQuery({
309 queryKey: key,
310 queryFn: () => sleep(0).then(() => 'Some data'),
311 })
312β
313 if (data.value !== undefined) {
314 expectTypeOf(data.value).toEqualTypeOf<string>()
315 expectTypeOf(data).toEqualTypeOf<Ref<string>>()
316 }
317 })
318 })
319β
320 describe('accept ref options', () => {
321 it('should accept ref options', () => {
322 const options = ref({
323 queryKey: queryKey(),
324 queryFn: () => sleep(0).then(() => 'Some data'),
325 })
326β
327 const query = reactive(useQuery(options))
328β
329 if (query.isSuccess) {
330 expectTypeOf(query.data).toEqualTypeOf<string>()
331 }
332 })
333β
334 it('should accept computed options', () => {
335 const options = computed(() => ({
336 queryKey: queryKey(),
337 queryFn: () => sleep(0).then(() => 'Some data'),
338 }))
339β
340 const query = reactive(useQuery(options))
341β
342 if (query.isSuccess) {
343 expectTypeOf(query.data).toEqualTypeOf<string>()
344 }
345 })
346β
347 it('should accept computed query options', () => {
348 const options = computed(() =>
349 queryOptions({
350 queryKey: queryKey(),
351 queryFn: () => sleep(0).then(() => 'Some data'),
352 }),
353 )
354β
355 const query = reactive(useQuery(options))
356β
357 if (query.isSuccess) {
358 expectTypeOf(query.data).toEqualTypeOf<string>()
359 }
360 })
361 })
362β
363 describe('queryKey reactivity rules', () => {
364 it('should reject a bare reactive getter for the whole queryKey array', () => {
365 const id = ref(1)
366 assertType(
367 useQuery({
368 // @ts-expect-error when passed directly to useQuery, queryKey cannot be a bare
369 // reactive getter for the whole array (queryOptions() allows this)
370 queryKey: () => ['post', id.value],
371 queryFn: () => sleep(0).then(() => 'Some data'),
372 }),
373 )
374 })
375 })
376β
377 describe('select', () => {
378 it('should narrow data to the type select returns', () => {
379 const { data } = useQuery({
380 queryKey: queryKey(),
381 queryFn: () => sleep(0).then(() => ['a', 'b', 'c']),
382 select: (posts) => posts.length,
383 })
384β
385 expectTypeOf(data.value).toEqualTypeOf<number | undefined>()
386 })
387 })
388})