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

useQuery.test-d.ts

336 lines8.4 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, useQuery } from '..'
5import type { OmitKeyof, UseQueryOptions, UseQueryReturnType } from '..'
6​
7describe('useQuery', () => {
8 describe('Config object overload', () => {
9 it('TData should always be defined when initialData is provided as an object', () => {
10 const key = queryKey()
11​
12 const { data } = reactive(
13 useQuery({
14 queryKey: key,
15 queryFn: () => {
16 return {
17 wow: true,
18 }
19 },
20 initialData: {
21 wow: true,
22 },
23 }),
24 )
25​
26 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
27 })
28​
29 it('TData should be defined when passed through queryOptions', () => {
30 const key = queryKey()
31​
32 const options = queryOptions({
33 queryKey: key,
34 queryFn: () => {
35 return {
36 wow: true,
37 }
38 },
39 initialData: {
40 wow: true,
41 },
42 })
43 const { data } = reactive(useQuery(options))
44​
45 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
46 })
47​
48 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
49 const key = queryKey()
50​
51 const options = queryOptions({
52 queryKey: key,
53 queryFn: () => Promise.resolve(1),
54 })
55​
56 const query = reactive(
57 useQuery({
58 ...options,
59 select: (data) => data > 1,
60 }),
61 )
62​
63 expectTypeOf(query.data).toEqualTypeOf<boolean | undefined>()
64 })
65​
66 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
67 const key = queryKey()
68​
69 const { data } = reactive(
70 useQuery({
71 queryKey: key,
72 queryFn: () => {
73 return {
74 wow: true,
75 }
76 },
77 initialData: () => ({
78 wow: true,
79 }),
80 }),
81 )
82​
83 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
84 })
85​
86 it('TData should have undefined in the union when initialData is NOT provided', () => {
87 const key = queryKey()
88​
89 const { data } = reactive(
90 useQuery({
91 queryKey: key,
92 queryFn: () => {
93 return {
94 wow: true,
95 }
96 },
97 }),
98 )
99​
100 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
101 })
102​
103 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
104 const key = queryKey()
105​
106 const { data } = reactive(
107 useQuery({
108 queryKey: key,
109 queryFn: () => {
110 return {
111 wow: true,
112 }
113 },
114 initialData: () => undefined as { wow: boolean } | undefined,
115 }),
116 )
117​
118 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
119 })
120​
121 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
122 const key = queryKey()
123​
124 const { data, isSuccess } = reactive(
125 useQuery({
126 queryKey: key,
127 queryFn: () => {
128 return {
129 wow: true,
130 }
131 },
132 initialData: () => undefined as { wow: boolean } | undefined,
133 }),
134 )
135​
136 if (isSuccess) {
137 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
138 }
139 })
140​
141 it('data should not have undefined when initialData is provided', () => {
142 const key = queryKey()
143​
144 const { data } = reactive(
145 useQuery({
146 queryKey: key,
147 initialData: 42,
148 }),
149 )
150​
151 expectTypeOf(data).toEqualTypeOf<number>()
152 })
153 })
154​
155 describe('generic queryKey inference (#8199)', () => {
156 it('should not error when wrapping useQuery in a composable that propagates a generic type to the queryKey', () => {
157 const basket = { fruit: 'apple', vegetable: 'broccoli' } as const
158​
159 function getBasket<T extends 'fruit' | 'vegetable'>(type: T) {
160 return basket[type]
161 }
162​
163 function useBasket<T extends 'fruit' | 'vegetable'>(type: T) {
164 return useQuery({
165 queryKey: ['basket', type] as const,
166 queryFn({ queryKey: [, t] }) {
167 return getBasket(t)
168 },
169 })
170 }
171​
172 assertType<UseQueryReturnType<'apple' | 'broccoli', Error>>(
173 useBasket('fruit'),
174 )
175 })
176 })
177​
178 describe('custom composable', () => {
179 it('should allow custom composable using UseQueryOptions', () => {
180 const key = queryKey()
181​
182 const useCustomQuery = (
183 options?: OmitKeyof<
184 UseQueryOptions<string>,
185 'queryKey' | 'queryFn',
186 'safely'
187 >,
188 ) => {
189 return useQuery({
190 ...options,
191 queryKey: key,
192 queryFn: () => Promise.resolve('data'),
193 })
194 }
195​
196 const { data } = reactive(useCustomQuery())
197​
198 expectTypeOf(data).toEqualTypeOf<string | undefined>()
199 })
200 })
201​
202 describe('structuralSharing', () => {
203 it('should be able to use structuralSharing with unknown types', () => {
204 const key = queryKey()
205​
206 // https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
207 useQuery({
208 queryKey: key,
209 queryFn: () => 5,
210 structuralSharing: (oldData, newData) => {
211 expectTypeOf(oldData).toBeUnknown()
212 expectTypeOf(newData).toBeUnknown()
213 return newData
214 },
215 })
216 })
217 })
218​
219 describe('Discriminated union return type', () => {
220 it('data should be possibly undefined by default', () => {
221 const key = queryKey()
222​
223 const query = reactive(
224 useQuery({
225 queryKey: key,
226 queryFn: () => sleep(0).then(() => 'Some data'),
227 }),
228 )
229​
230 expectTypeOf(query.data).toEqualTypeOf<string | undefined>()
231 })
232​
233 it('data should be defined when query is success', () => {
234 const key = queryKey()
235​
236 const query = reactive(
237 useQuery({
238 queryKey: key,
239 queryFn: () => sleep(0).then(() => 'Some data'),
240 }),
241 )
242​
243 if (query.isSuccess) {
244 expectTypeOf(query.data).toEqualTypeOf<string>()
245 }
246 })
247​
248 it('error should be null when query is success', () => {
249 const key = queryKey()
250​
251 const query = reactive(
252 useQuery({
253 queryKey: key,
254 queryFn: () => sleep(0).then(() => 'Some data'),
255 }),
256 )
257​
258 if (query.isSuccess) {
259 expectTypeOf(query.error).toEqualTypeOf<null>()
260 }
261 })
262​
263 it('data should be undefined when query is pending', () => {
264 const key = queryKey()
265​
266 const query = reactive(
267 useQuery({
268 queryKey: key,
269 queryFn: () => sleep(0).then(() => 'Some data'),
270 }),
271 )
272​
273 if (query.isPending) {
274 expectTypeOf(query.data).toEqualTypeOf<undefined>()
275 }
276 })
277​
278 it('error should be defined when query is error', () => {
279 const key = queryKey()
280​
281 const query = reactive(
282 useQuery({
283 queryKey: key,
284 queryFn: () => sleep(0).then(() => 'Some data'),
285 }),
286 )
287​
288 if (query.isError) {
289 expectTypeOf(query.error).toEqualTypeOf<Error>()
290 }
291 })
292 })
293​
294 describe('accept ref options', () => {
295 it('should accept ref options', () => {
296 const options = ref({
297 queryKey: queryKey(),
298 queryFn: () => sleep(0).then(() => 'Some data'),
299 })
300​
301 const query = reactive(useQuery(options))
302​
303 if (query.isSuccess) {
304 expectTypeOf(query.data).toEqualTypeOf<string>()
305 }
306 })
307​
308 it('should accept computed options', () => {
309 const options = computed(() => ({
310 queryKey: queryKey(),
311 queryFn: () => sleep(0).then(() => 'Some data'),
312 }))
313​
314 const query = reactive(useQuery(options))
315​
316 if (query.isSuccess) {
317 expectTypeOf(query.data).toEqualTypeOf<string>()
318 }
319 })
320​
321 it('should accept computed query options', () => {
322 const options = computed(() =>
323 queryOptions({
324 queryKey: queryKey(),
325 queryFn: () => sleep(0).then(() => 'Some data'),
326 }),
327 )
328​
329 const query = reactive(useQuery(options))
330​
331 if (query.isSuccess) {
332 expectTypeOf(query.data).toEqualTypeOf<string>()
333 }
334 })
335 })
336})