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