Open History to see the last commit
1import { 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 } 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('custom composable', () => {
156 it('should allow custom composable using UseQueryOptions', () => {
157 const key = queryKey()
158β
159 const useCustomQuery = (
160 options?: OmitKeyof<
161 UseQueryOptions<string>,
162 'queryKey' | 'queryFn',
163 'safely'
164 >,
165 ) => {
166 return useQuery({
167 ...options,
168 queryKey: key,
169 queryFn: () => Promise.resolve('data'),
170 })
171 }
172β
173 const { data } = reactive(useCustomQuery())
174β
175 expectTypeOf(data).toEqualTypeOf<string | undefined>()
176 })
177 })
178β
179 describe('structuralSharing', () => {
180 it('should be able to use structuralSharing with unknown types', () => {
181 const key = queryKey()
182β
183 // https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
184 useQuery({
185 queryKey: key,
186 queryFn: () => 5,
187 structuralSharing: (oldData, newData) => {
188 expectTypeOf(oldData).toBeUnknown()
189 expectTypeOf(newData).toBeUnknown()
190 return newData
191 },
192 })
193 })
194 })
195β
196 describe('Discriminated union return type', () => {
197 it('data should be possibly undefined by default', () => {
198 const key = queryKey()
199β
200 const query = reactive(
201 useQuery({
202 queryKey: key,
203 queryFn: () => sleep(0).then(() => 'Some data'),
204 }),
205 )
206β
207 expectTypeOf(query.data).toEqualTypeOf<string | undefined>()
208 })
209β
210 it('data should be defined when query is success', () => {
211 const key = queryKey()
212β
213 const query = reactive(
214 useQuery({
215 queryKey: key,
216 queryFn: () => sleep(0).then(() => 'Some data'),
217 }),
218 )
219β
220 if (query.isSuccess) {
221 expectTypeOf(query.data).toEqualTypeOf<string>()
222 }
223 })
224β
225 it('error should be null when query is success', () => {
226 const key = queryKey()
227β
228 const query = reactive(
229 useQuery({
230 queryKey: key,
231 queryFn: () => sleep(0).then(() => 'Some data'),
232 }),
233 )
234β
235 if (query.isSuccess) {
236 expectTypeOf(query.error).toEqualTypeOf<null>()
237 }
238 })
239β
240 it('data should be undefined when query is pending', () => {
241 const key = queryKey()
242β
243 const query = reactive(
244 useQuery({
245 queryKey: key,
246 queryFn: () => sleep(0).then(() => 'Some data'),
247 }),
248 )
249β
250 if (query.isPending) {
251 expectTypeOf(query.data).toEqualTypeOf<undefined>()
252 }
253 })
254β
255 it('error should be defined when query is error', () => {
256 const key = queryKey()
257β
258 const query = reactive(
259 useQuery({
260 queryKey: key,
261 queryFn: () => sleep(0).then(() => 'Some data'),
262 }),
263 )
264β
265 if (query.isError) {
266 expectTypeOf(query.error).toEqualTypeOf<Error>()
267 }
268 })
269 })
270β
271 describe('accept ref options', () => {
272 it('should accept ref options', () => {
273 const options = ref({
274 queryKey: queryKey(),
275 queryFn: () => sleep(0).then(() => 'Some data'),
276 })
277β
278 const query = reactive(useQuery(options))
279β
280 if (query.isSuccess) {
281 expectTypeOf(query.data).toEqualTypeOf<string>()
282 }
283 })
284β
285 it('should accept computed options', () => {
286 const options = computed(() => ({
287 queryKey: queryKey(),
288 queryFn: () => sleep(0).then(() => 'Some data'),
289 }))
290β
291 const query = reactive(useQuery(options))
292β
293 if (query.isSuccess) {
294 expectTypeOf(query.data).toEqualTypeOf<string>()
295 }
296 })
297β
298 it('should accept computed query options', () => {
299 const options = computed(() =>
300 queryOptions({
301 queryKey: queryKey(),
302 queryFn: () => sleep(0).then(() => 'Some data'),
303 }),
304 )
305β
306 const query = reactive(useQuery(options))
307β
308 if (query.isSuccess) {
309 expectTypeOf(query.data).toEqualTypeOf<string>()
310 }
311 })
312 })
313})