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

queryOptions.test-d.ts

269 lines7.7 KB

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