Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import {
3 QueriesObserver,
4 QueryClient,
5 dataTagSymbol,
6 skipToken,
7} from '@tanstack/query-core'
8import { queryKey } from '@tanstack/query-test-utils'
9import { queryOptions } from '../queryOptions'
10import { useQuery } from '../useQuery'
11import { useQueries } from '../useQueries'
12import { useSuspenseQuery } from '../useSuspenseQuery'
13import type { AnyUseQueryOptions } from '../types'
14import type {
15 DataTag,
16 InitialDataFunction,
17 QueryObserverResult,
18} from '@tanstack/query-core'
19β
20describe('queryOptions', () => {
21 it('should not allow excess properties', () => {
22 assertType(
23 queryOptions({
24 queryKey: queryKey(),
25 queryFn: () => Promise.resolve(5),
26 // @ts-expect-error this is a good error, because stallTime does not exist!
27 stallTime: 1000,
28 }),
29 )
30 })
31 it('should infer types for callbacks', () => {
32 queryOptions({
33 queryKey: queryKey(),
34 queryFn: () => Promise.resolve(5),
35 staleTime: 1000,
36 select: (data) => {
37 expectTypeOf(data).toEqualTypeOf<number>()
38 },
39 })
40 })
41 it('should work when passed to useQuery', () => {
42 const options = queryOptions({
43 queryKey: queryKey(),
44 queryFn: () => Promise.resolve(5),
45 })
46β
47 const { data } = useQuery(options)
48 expectTypeOf(data).toEqualTypeOf<number | undefined>()
49 })
50 it('should work when passed to useSuspenseQuery', () => {
51 const options = queryOptions({
52 queryKey: queryKey(),
53 queryFn: () => Promise.resolve(5),
54 })
55β
56 const { data } = useSuspenseQuery(options)
57 expectTypeOf(data).toEqualTypeOf<number>()
58 })
59β
60 it('should work when passed to fetchQuery', async () => {
61 const options = queryOptions({
62 queryKey: queryKey(),
63 queryFn: () => Promise.resolve(5),
64 })
65β
66 const data = await new QueryClient().fetchQuery(options)
67 expectTypeOf(data).toEqualTypeOf<number>()
68 })
69 it('should work when passed to useQueries', () => {
70 const options = queryOptions({
71 queryKey: queryKey(),
72 queryFn: () => Promise.resolve(5),
73 })
74β
75 const [{ data }] = useQueries({
76 queries: [options],
77 })
78β
79 expectTypeOf(data).toEqualTypeOf<number | undefined>()
80 })
81 it('should tag the queryKey with the result type of the QueryFn', () => {
82 const { queryKey: tagged } = queryOptions({
83 queryKey: queryKey(),
84 queryFn: () => Promise.resolve(5),
85 })
86β
87 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
88 })
89 it('should tag the queryKey even if no promise is returned', () => {
90 const { queryKey: tagged } = queryOptions({
91 queryKey: queryKey(),
92 queryFn: () => 5,
93 })
94β
95 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
96 })
97 it('should tag the queryKey with unknown if there is no queryFn', () => {
98 const { queryKey: tagged } = queryOptions({
99 queryKey: queryKey(),
100 })
101β
102 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
103 })
104 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
105 const { queryKey: tagged } = queryOptions({
106 queryKey: queryKey(),
107 queryFn: () => Promise.resolve(5),
108 select: (data) => data.toString(),
109 })
110β
111 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
112 })
113 it('should return the proper type when passed to getQueryData', () => {
114 const { queryKey: tagged } = queryOptions({
115 queryKey: queryKey(),
116 queryFn: () => Promise.resolve(5),
117 })
118β
119 const queryClient = new QueryClient()
120 const data = queryClient.getQueryData(tagged)
121 expectTypeOf(data).toEqualTypeOf<number | undefined>()
122 })
123 it('should return the proper type when passed to getQueryState', () => {
124 const { queryKey: tagged } = queryOptions({
125 queryKey: queryKey(),
126 queryFn: () => Promise.resolve(5),
127 })
128β
129 const queryClient = new QueryClient()
130 const state = queryClient.getQueryState(tagged)
131 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
132 })
133 it('should properly type updaterFn when passed to setQueryData', () => {
134 const { queryKey: tagged } = queryOptions({
135 queryKey: queryKey(),
136 queryFn: () => Promise.resolve(5),
137 })
138β
139 const queryClient = new QueryClient()
140 const data = queryClient.setQueryData(tagged, (prev) => {
141 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
142 return prev
143 })
144 expectTypeOf(data).toEqualTypeOf<number | undefined>()
145 })
146 it('should properly type value when passed to setQueryData', () => {
147 const { queryKey: tagged } = queryOptions({
148 queryKey: queryKey(),
149 queryFn: () => Promise.resolve(5),
150 })
151β
152 const queryClient = new QueryClient()
153β
154 // @ts-expect-error value should be a number
155 queryClient.setQueryData(tagged, '5')
156 // @ts-expect-error value should be a number
157 queryClient.setQueryData(tagged, () => '5')
158β
159 const data = queryClient.setQueryData(tagged, 5)
160 expectTypeOf(data).toEqualTypeOf<number | undefined>()
161 })
162β
163 it('should infer even if there is a conditional skipToken', () => {
164 const options = queryOptions({
165 queryKey: queryKey(),
166 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
167 })
168β
169 const queryClient = new QueryClient()
170 const data = queryClient.getQueryData(options.queryKey)
171 expectTypeOf(data).toEqualTypeOf<number | undefined>()
172 })
173β
174 it('should infer to unknown if we disable a query with just a skipToken', () => {
175 const options = queryOptions({
176 queryKey: queryKey(),
177 queryFn: skipToken,
178 })
179β
180 const queryClient = new QueryClient()
181 const data = queryClient.getQueryData(options.queryKey)
182 expectTypeOf(data).toEqualTypeOf<unknown>()
183 })
184β
185 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
186 const options = queryOptions({
187 queryKey: queryKey(),
188 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
189 })
190 // @ts-expect-error TS2345
191 const { data } = useSuspenseQuery(options)
192 expectTypeOf(data).toEqualTypeOf<number>()
193 })
194β
195 it('should return the proper type when passed to QueriesObserver', () => {
196 const options = queryOptions({
197 queryKey: queryKey(),
198 queryFn: () => Promise.resolve(5),
199 })
200β
201 const queryClient = new QueryClient()
202 const queriesObserver = new QueriesObserver(queryClient, [options])
203 expectTypeOf(queriesObserver).toEqualTypeOf<
204 QueriesObserver<Array<QueryObserverResult>>
205 >()
206 })
207β
208 it('should allow undefined response in initialData', () => {
209 assertType((id: string | null) =>
210 queryOptions({
211 queryKey: ['todo', id],
212 queryFn: () =>
213 Promise.resolve({
214 id: '1',
215 title: 'Do Laundry',
216 }),
217 initialData: () =>
218 !id
219 ? undefined
220 : {
221 id,
222 title: 'Initial Data',
223 },
224 }),
225 )
226 })
227β
228 it('should allow optional initialData object', () => {
229 const testFn = (id?: string) => {
230 const options = queryOptions({
231 queryKey: queryKey(),
232 queryFn: () => Promise.resolve('something string'),
233 initialData: id ? 'initial string' : undefined,
234 })
235 expectTypeOf(options.initialData).toExtend<
236 InitialDataFunction<string> | string | undefined
237 >()
238 }
239 testFn('id')
240 testFn()
241 })
242β
243 it('should be passable to UseQueryOptions', () => {
244 function somethingWithQueryOptions<TQueryOpts extends AnyUseQueryOptions>(
245 options: TQueryOpts,
246 ) {
247 return options.queryKey
248 }
249β
250 const options = queryOptions({
251 queryKey: queryKey(),
252 queryFn: () => Promise.resolve(1),
253 })
254β
255 assertType(somethingWithQueryOptions(options))
256 })
257β
258 it('should return a custom query key type', () => {
259 type MyQueryKey = [Array<string>, { type: 'foo' }]
260β
261 const options = queryOptions({
262 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
263 queryFn: () => Promise.resolve(1),
264 })
265β
266 expectTypeOf(options.queryKey).toEqualTypeOf<
267 DataTag<MyQueryKey, number, Error>
268 >()
269 })
270β
271 it('should return a custom query key type with datatag', () => {
272 type MyQueryKey = DataTag<
273 [Array<string>, { type: 'foo' }],
274 number,
275 Error & { myMessage: string }
276 >
277β
278 const options = queryOptions({
279 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
280 queryFn: () => Promise.resolve(1),
281 })
282β
283 expectTypeOf(options.queryKey).toEqualTypeOf<
284 DataTag<MyQueryKey, number, Error & { myMessage: string }>
285 >()
286 })
287})