Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queryOptions.test-d.tsx

287 lines8.3 KB

1import {
2 QueriesObserver,
3 QueryClient,
4 dataTagSymbol,
5 skipToken,
6} from '@tanstack/query-core'
7import type {
8 DataTag,
9 InitialDataFunction,
10 QueryObserverResult,
11} from '@tanstack/query-core'
12import { assertType, describe, expectTypeOf, it } from 'vitest'
13​
14import { queryOptions } from '../queryOptions'
15import type { AnyUseQueryOptions } from '../types'
16import { useQueries } from '../useQueries'
17import { useQuery } from '../useQuery'
18import { useSuspenseQuery } from '../useSuspenseQuery'
19​
20describe('queryOptions', () => {
21 it('should not allow excess properties', () => {
22 assertType(
23 queryOptions({
24 queryKey: ['key'],
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: ['key'],
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: ['key'],
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: ['key'],
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: ['key'],
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: ['key'],
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 } = queryOptions({
83 queryKey: ['key'],
84 queryFn: () => Promise.resolve(5),
85 })
86​
87 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
88 })
89 it('should tag the queryKey even if no promise is returned', () => {
90 const { queryKey } = queryOptions({
91 queryKey: ['key'],
92 queryFn: () => 5,
93 })
94​
95 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
96 })
97 it('should tag the queryKey with unknown if there is no queryFn', () => {
98 const { queryKey } = queryOptions({
99 queryKey: ['key'],
100 })
101​
102 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
103 })
104 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
105 const { queryKey } = queryOptions({
106 queryKey: ['key'],
107 queryFn: () => Promise.resolve(5),
108 select: (data) => data.toString(),
109 })
110​
111 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
112 })
113 it('should return the proper type when passed to getQueryData', () => {
114 const { queryKey } = queryOptions({
115 queryKey: ['key'],
116 queryFn: () => Promise.resolve(5),
117 })
118​
119 const queryClient = new QueryClient()
120 const data = queryClient.getQueryData(queryKey)
121 expectTypeOf(data).toEqualTypeOf<number | undefined>()
122 })
123 it('should return the proper type when passed to getQueryState', () => {
124 const { queryKey } = queryOptions({
125 queryKey: ['key'],
126 queryFn: () => Promise.resolve(5),
127 })
128​
129 const queryClient = new QueryClient()
130 const state = queryClient.getQueryState(queryKey)
131 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
132 })
133 it('should properly type updaterFn when passed to setQueryData', () => {
134 const { queryKey } = queryOptions({
135 queryKey: ['key'],
136 queryFn: () => Promise.resolve(5),
137 })
138​
139 const queryClient = new QueryClient()
140 const data = queryClient.setQueryData(queryKey, (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 } = queryOptions({
148 queryKey: ['key'],
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(queryKey, '5')
156 // @ts-expect-error value should be a number
157 queryClient.setQueryData(queryKey, () => '5')
158​
159 const data = queryClient.setQueryData(queryKey, 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: ['key'],
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: ['key'],
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: ['key'],
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: ['key'],
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: ['test'],
232 queryFn: () => Promise.resolve('something string'),
233 initialData: id ? 'initial string' : undefined,
234 })
235 expectTypeOf(options.initialData).toMatchTypeOf<
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: ['key'],
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})