Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { reactive } from 'vue'
3import { createKey } from '@tanstack/query-test-utils'
4import { skipToken, useQueries } from '..'
5import { queryOptions } from '../queryOptions'
6import type { OmitKeyof, QueryObserverResult } from '..'
7import type { UseQueryOptions } from '../queryOptions'
8β
9describe('UseQueries config object overload', () => {
10 it('should always define TData when initialData is provided as an object', () => {
11 const key1 = createKey()
12 const key2 = createKey()
13 const key3 = createKey()
14 const query1 = {
15 queryKey: key1,
16 queryFn: () => {
17 return {
18 wow: true,
19 }
20 },
21 initialData: {
22 wow: false,
23 },
24 }
25β
26 const query2 = queryOptions({
27 queryKey: key2,
28 queryFn: () => 'Query Data',
29 initialData: 'initial data',
30 })
31β
32 const query3 = {
33 queryKey: key3,
34 queryFn: () => 'Query Data',
35 }
36β
37 const { value: queriesState } = useQueries({
38 queries: [query1, query2, query3],
39 })
40β
41 expectTypeOf(queriesState[0].data).toEqualTypeOf<{ wow: boolean }>()
42 expectTypeOf(queriesState[1].data).toEqualTypeOf<string>()
43 expectTypeOf(queriesState[2].data).toEqualTypeOf<string | undefined>()
44 })
45β
46 it('should define TData when passed through queryOptions', () => {
47 const key = createKey()
48 const options = queryOptions({
49 queryKey: key,
50 queryFn: () => {
51 return {
52 wow: true,
53 }
54 },
55 initialData: {
56 wow: true,
57 },
58 })
59β
60 const { value: queriesState } = useQueries({ queries: [options] })
61β
62 expectTypeOf(queriesState[0].data).toEqualTypeOf<{ wow: boolean }>()
63 })
64β
65 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQueries', () => {
66 const key1 = createKey()
67 const key2 = createKey()
68 const query1 = queryOptions({
69 queryKey: key1,
70 queryFn: () => Promise.resolve(1),
71 select: (data) => data > 1,
72 })
73β
74 const query2 = {
75 queryKey: key2,
76 queryFn: () => Promise.resolve(1),
77 select: (data: any) => data > 1,
78 }
79β
80 const queriesState = reactive(useQueries({ queries: [query1, query2] }))
81β
82 expectTypeOf(queriesState.value[0].data).toEqualTypeOf<
83 boolean | undefined
84 >()
85 expectTypeOf(queriesState.value[1].data).toEqualTypeOf<
86 boolean | undefined
87 >()
88 })
89β
90 it('should have undefined in the TData union when initialData is provided as a function which can return undefined', () => {
91 const key = createKey()
92 const { value: queriesState } = useQueries({
93 queries: [
94 {
95 queryKey: key,
96 queryFn: () => {
97 return {
98 wow: true,
99 }
100 },
101 initialData: () => undefined as { wow: boolean } | undefined,
102 },
103 ],
104 })
105β
106 expectTypeOf(queriesState[0].data).toEqualTypeOf<
107 { wow: boolean } | undefined
108 >()
109 })
110β
111 it('should have correct TData type when conditional skipToken is passed', () => {
112 const key = createKey()
113 const { value: queriesState } = useQueries({
114 queries: [
115 queryOptions({
116 queryKey: key,
117 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
118 }),
119 ],
120 })
121β
122 const firstResult = queriesState[0]
123β
124 expectTypeOf(firstResult).toEqualTypeOf<
125 QueryObserverResult<number, Error>
126 >()
127 expectTypeOf(firstResult.data).toEqualTypeOf<number | undefined>()
128 })
129β
130 describe('custom hook', () => {
131 it('should allow custom hooks using UseQueryOptions', () => {
132 const useCustomQueries = (
133 options?: OmitKeyof<
134 UseQueryOptions<string>,
135 'queryKey' | 'queryFn',
136 'safely'
137 >,
138 ) =>
139 useQueries({
140 queries: [
141 {
142 ...options,
143 queryKey: createKey(),
144 queryFn: () => Promise.resolve('data'),
145 },
146 ],
147 })
148β
149 const { value: queriesState } = useCustomQueries()
150β
151 expectTypeOf(queriesState[0].data).toEqualTypeOf<string | undefined>()
152 })
153 })
154β
155 // Fix #7270
156 it('should have proper type inference with different options provided', () => {
157 const numbers = [1, 2, 3]
158 const getQueryKey = (n: number) => [n]
159 const queryFn = (n: number) => () => Promise.resolve(n)
160 const select = (data: number) => data.toString()
161β
162 const queries = numbers.map((n) => ({
163 queryKey: [n],
164 queryFn: () => Promise.resolve(n),
165 select: (data: number) => data.toString(),
166 }))
167β
168 const queriesWithoutSelect = numbers.map((n) => ({
169 queryKey: getQueryKey(n),
170 queryFn: queryFn(n),
171 }))
172β
173 const queriesWithQueryOptions = numbers.map((n) =>
174 queryOptions({
175 queryKey: getQueryKey(n),
176 queryFn: queryFn(n),
177 select,
178 }),
179 )
180β
181 const queriesWithQueryOptionsWithoutSelect = numbers.map((n) =>
182 queryOptions({
183 queryKey: getQueryKey(n),
184 queryFn: queryFn(n),
185 }),
186 )
187β
188 const query1 = useQueries({ queries: queries })
189 expectTypeOf(query1.value).toEqualTypeOf<
190 Array<QueryObserverResult<string, Error>>
191 >()
192β
193 const query2 = useQueries({ queries: queriesWithoutSelect })
194 expectTypeOf(query2.value).toEqualTypeOf<
195 Array<QueryObserverResult<number, Error>>
196 >()
197β
198 const query3 = useQueries({ queries: queriesWithQueryOptions })
199 expectTypeOf(query3.value).toEqualTypeOf<
200 Array<QueryObserverResult<string, Error>>
201 >()
202β
203 const query4 = useQueries({ queries: queriesWithQueryOptionsWithoutSelect })
204 expectTypeOf(query4.value).toEqualTypeOf<
205 Array<QueryObserverResult<number, Error>>
206 >()
207β
208 const queryCombine = useQueries({
209 queries: queries,
210 combine: (data) => {
211 return data.reduce((acc, i) => {
212 acc.push(i.data ?? '')
213 return acc
214 }, [] as Array<string>)
215 },
216 })
217 expectTypeOf(queryCombine.value).toEqualTypeOf<Array<string>>()
218β
219 const queryCombineWithoutSelect = useQueries({
220 queries: queriesWithoutSelect,
221 combine: (data) => {
222 return data.reduce((acc, i) => {
223 acc.push(i.data ?? 0)
224 return acc
225 }, [] as Array<number>)
226 },
227 })
228β
229 expectTypeOf(queryCombineWithoutSelect.value).toEqualTypeOf<Array<number>>()
230 })
231β
232 it('should return correct data for dynamic queries with mixed result types', () => {
233 const key1 = createKey()
234 const key2 = createKey()
235 const Queries1 = {
236 get: () =>
237 queryOptions({
238 queryKey: key1,
239 queryFn: () => Promise.resolve(1),
240 }),
241 }
242 const Queries2 = {
243 get: () =>
244 queryOptions({
245 queryKey: key2,
246 queryFn: () => Promise.resolve(true),
247 }),
248 }
249β
250 const queries1List = [1, 2, 3].map(() => ({ ...Queries1.get() }))
251 const { value: queriesState } = useQueries({
252 queries: [...queries1List, { ...Queries2.get() }],
253 })
254β
255 expectTypeOf(queriesState).toEqualTypeOf<
256 [
257 ...Array<QueryObserverResult<number, Error>>,
258 QueryObserverResult<boolean, Error>,
259 ]
260 >()
261β
262 expectTypeOf(queriesState[0].data).toEqualTypeOf<
263 number | boolean | undefined
264 >()
265 })
266β
267 it('should infer correct data type from queryOptions without initialData in useQueries', () => {
268 const options = queryOptions({
269 queryKey: createKey(),
270 queryFn: () => Promise.resolve(5),
271 })
272β
273 const { value: queriesState } = useQueries({ queries: [options] })
274β
275 expectTypeOf(queriesState[0].data).toEqualTypeOf<number | undefined>()
276 })
277β
278 it('should infer correct data type from queryOptions with select in useQueries', () => {
279 const options = queryOptions({
280 queryKey: createKey(),
281 queryFn: () => Promise.resolve(5),
282 select: (data) => data.toString(),
283 })
284β
285 const { value: queriesState } = useQueries({ queries: [options] })
286β
287 expectTypeOf(queriesState[0].data).toEqualTypeOf<string | undefined>()
288 })
289})