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

queryOptions.test-d.ts

263 lines7.7 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
3import { queryKey } from '@tanstack/query-test-utils'
4import { createQueries, queryOptions } from '../src/index.js'
5import type { InitialDataFunction } from '@tanstack/query-core'
6​
7// Regression test for exported queryOptions inference under declaration emit.
8// TypeScript should be able to name the return type without expanding the
9// internal data tag symbols into the consumer's .d.ts output.
10export const exportedQueryOptions = queryOptions({
11 queryKey: ['invalid'],
12})
13​
14describe('queryOptions', () => {
15 it('should not allow excess properties', () => {
16 const key = queryKey()
17 assertType(
18 queryOptions({
19 queryKey: key,
20 queryFn: () => Promise.resolve(5),
21 // @ts-expect-error this is a good error, because stallTime does not exist!
22 stallTime: 1000,
23 }),
24 )
25 })
26​
27 it('should infer types for callbacks', () => {
28 const key = queryKey()
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​
39 it('should work when passed to query', async () => {
40 const options = queryOptions({
41 queryKey: ['key'],
42 queryFn: () => Promise.resolve(5),
43 })
44​
45 const data = await new QueryClient().query(options)
46 expectTypeOf(data).toEqualTypeOf<number>()
47 })
48​
49 it('should work when passed to query with select', async () => {
50 const options = queryOptions({
51 queryKey: ['key'],
52 queryFn: () => Promise.resolve(5),
53 select: (data) => data.toString(),
54 })
55​
56 const data = await new QueryClient().query(options)
57 expectTypeOf(data).toEqualTypeOf<string>()
58 })
59​
60 it('should work when passed to query with enabled: false', async () => {
61 const options = queryOptions({
62 queryKey: ['key'],
63 queryFn: () => Promise.resolve(5),
64 enabled: false,
65 })
66​
67 const data = await new QueryClient().query(options)
68 expectTypeOf(data).toEqualTypeOf<number>()
69 })
70​
71 it('should work when passed to query with skipToken', async () => {
72 const options = queryOptions({
73 queryKey: ['key'],
74 queryFn: skipToken,
75 })
76​
77 const data = await new QueryClient().query(options)
78 expectTypeOf(data).toEqualTypeOf<unknown>()
79 })
80​
81 it('should work when passed to fetchQuery', async () => {
82 const key = queryKey()
83 const options = queryOptions({
84 queryKey: key,
85 queryFn: () => Promise.resolve(5),
86 })
87​
88 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
89 const data = await new QueryClient().fetchQuery(options)
90 expectTypeOf(data).toEqualTypeOf<number>()
91 })
92​
93 it('should work when passed to createQueries', () => {
94 const key = queryKey()
95 const options = queryOptions({
96 queryKey: key,
97 queryFn: () => Promise.resolve(5),
98 })
99​
100 const queries = createQueries(() => ({
101 queries: [options],
102 }))
103​
104 expectTypeOf(queries[0].data).toEqualTypeOf<number | undefined>()
105 })
106​
107 it('should tag the queryKey with the result type of the QueryFn', () => {
108 const key = queryKey()
109 const { queryKey: tagged } = queryOptions({
110 queryKey: key,
111 queryFn: () => Promise.resolve(5),
112 })
113​
114 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
115 })
116​
117 it('should tag the queryKey even if no promise is returned', () => {
118 const key = queryKey()
119 const { queryKey: tagged } = queryOptions({
120 queryKey: key,
121 queryFn: () => 5,
122 })
123​
124 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
125 })
126​
127 it('should tag the queryKey with unknown if there is no queryFn', () => {
128 const key = queryKey()
129 const { queryKey: tagged } = queryOptions({
130 queryKey: key,
131 })
132​
133 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
134 })
135​
136 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
137 const key = queryKey()
138 const { queryKey: tagged } = queryOptions({
139 queryKey: key,
140 queryFn: () => Promise.resolve(5),
141 select: (data) => data.toString(),
142 })
143​
144 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
145 })
146​
147 it('should return the proper type when passed to getQueryData', () => {
148 const key = queryKey()
149 const { queryKey: tagged } = queryOptions({
150 queryKey: key,
151 queryFn: () => Promise.resolve(5),
152 })
153​
154 const queryClient = new QueryClient()
155 const data = queryClient.getQueryData(tagged)
156 expectTypeOf(data).toEqualTypeOf<number | undefined>()
157 })
158​
159 it('should return the proper type when passed to getQueryState', () => {
160 const key = queryKey()
161 const { queryKey: tagged } = queryOptions({
162 queryKey: key,
163 queryFn: () => Promise.resolve(5),
164 })
165​
166 const queryClient = new QueryClient()
167 const state = queryClient.getQueryState(tagged)
168 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
169 })
170​
171 it('should properly type updaterFn when passed to setQueryData', () => {
172 const key = queryKey()
173 const { queryKey: tagged } = queryOptions({
174 queryKey: key,
175 queryFn: () => Promise.resolve(5),
176 })
177​
178 const queryClient = new QueryClient()
179 const data = queryClient.setQueryData(tagged, (prev) => {
180 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
181 return prev
182 })
183 expectTypeOf(data).toEqualTypeOf<number | undefined>()
184 })
185​
186 it('should properly type value when passed to setQueryData', () => {
187 const key = queryKey()
188 const { queryKey: tagged } = queryOptions({
189 queryKey: key,
190 queryFn: () => Promise.resolve(5),
191 })
192​
193 const queryClient = new QueryClient()
194​
195 // @ts-expect-error value should be a number
196 queryClient.setQueryData(tagged, '5')
197 // @ts-expect-error value should be a number
198 queryClient.setQueryData(tagged, () => '5')
199​
200 const data = queryClient.setQueryData(tagged, 5)
201 expectTypeOf(data).toEqualTypeOf<number | undefined>()
202 })
203​
204 it('should infer even if there is a conditional skipToken', () => {
205 const key = queryKey()
206 const options = queryOptions({
207 queryKey: key,
208 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
209 })
210​
211 const queryClient = new QueryClient()
212 const data = queryClient.getQueryData(options.queryKey)
213 expectTypeOf(data).toEqualTypeOf<number | undefined>()
214 })
215​
216 it('should infer to unknown if we disable a query with just a skipToken', () => {
217 const key = queryKey()
218 const options = queryOptions({
219 queryKey: key,
220 queryFn: skipToken,
221 })
222​
223 const queryClient = new QueryClient()
224 const data = queryClient.getQueryData(options.queryKey)
225 expectTypeOf(data).toEqualTypeOf<unknown>()
226 })
227​
228 it('should allow undefined response in initialData', () => {
229 const key = queryKey()
230 const options = (id: string | null) =>
231 queryOptions({
232 queryKey: [...key, id],
233 queryFn: () =>
234 Promise.resolve({
235 id: '1',
236 title: 'Do Laundry',
237 }),
238 initialData: () =>
239 !id
240 ? undefined
241 : {
242 id,
243 title: 'Initial Data',
244 },
245 })
246​
247 expectTypeOf(options(null).initialData).returns.toEqualTypeOf<
248 { id: string; title: string } | undefined
249 >()
250 })
251​
252 it('should allow optional initialData object', () => {
253 const options = queryOptions({
254 queryKey: queryKey(),
255 queryFn: () => Promise.resolve('something string'),
256 initialData: Math.random() > 0.5 ? 'initial string' : undefined,
257 })
258​
259 expectTypeOf(options.initialData).toExtend<
260 InitialDataFunction<string> | string | undefined
261 >()
262 })
263})