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