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

queryOptions.test-d.ts

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