Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { queryKey } from '@tanstack/query-test-utils'
3import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
4import { infiniteQueryOptions } from '../infinite-query-options'
5import { injectInfiniteQuery } from '../inject-infinite-query'
6import { injectQuery } from '../inject-query'
7import type {
8 DataTag,
9 InfiniteData,
10 InitialDataFunction,
11} from '@tanstack/query-core'
12β
13// Regression test for exported infiniteQueryOptions inference under declaration emit.
14// TypeScript should be able to name the return type without expanding the
15// internal data tag symbols into the consumer's .d.ts output.
16export const exportedInfiniteQueryOptions = infiniteQueryOptions({
17 queryKey: ['invalid'],
18 getNextPageParam: () => 1,
19 initialPageParam: 1,
20})
21β
22describe('infiniteQueryOptions', () => {
23 it('should not allow excess properties', () => {
24 const key = queryKey()
25 assertType(
26 infiniteQueryOptions({
27 queryKey: key,
28 queryFn: () => Promise.resolve('data'),
29 getNextPageParam: () => 1,
30 initialPageParam: 1,
31 // @ts-expect-error this is a good error, because stallTime does not exist!
32 stallTime: 1000,
33 }),
34 )
35 })
36 it('should infer types for callbacks', () => {
37 const key = queryKey()
38 infiniteQueryOptions({
39 queryKey: key,
40 queryFn: () => Promise.resolve('data'),
41 staleTime: 1000,
42 getNextPageParam: () => 1,
43 initialPageParam: 1,
44 select: (data) => {
45 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
46 },
47 })
48 })
49 it('should work when passed to useInfiniteQuery', () => {
50 const key = queryKey()
51 const options = infiniteQueryOptions({
52 queryKey: key,
53 queryFn: () => Promise.resolve('string'),
54 getNextPageParam: () => 1,
55 initialPageParam: 1,
56 })
57β
58 const { data } = injectInfiniteQuery(() => options)
59β
60 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
61 expectTypeOf(data()).toEqualTypeOf<
62 InfiniteData<string, unknown> | undefined
63 >()
64 })
65β
66 it('should work when passed to fetchInfiniteQuery', async () => {
67 const key = queryKey()
68 const options = infiniteQueryOptions({
69 queryKey: key,
70 queryFn: () => Promise.resolve('string'),
71 getNextPageParam: () => 1,
72 initialPageParam: 1,
73 })
74β
75 const data = await new QueryClient().fetchInfiniteQuery(options)
76β
77 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
78 })
79β
80 it('should work when passed to infiniteQuery', async () => {
81 const options = infiniteQueryOptions({
82 queryKey: ['key'],
83 queryFn: () => Promise.resolve('string'),
84 getNextPageParam: () => 1,
85 initialPageParam: 1,
86 })
87β
88 const data = await new QueryClient().infiniteQuery(options)
89β
90 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
91 })
92β
93 it('should work when passed to infiniteQuery with select', async () => {
94 const options = infiniteQueryOptions({
95 queryKey: ['key'],
96 queryFn: () => Promise.resolve('string'),
97 getNextPageParam: () => 1,
98 initialPageParam: 1,
99 select: (data) => data.pages,
100 })
101β
102 const data = await new QueryClient().infiniteQuery(options)
103β
104 expectTypeOf(data).toEqualTypeOf<Array<string>>()
105 })
106β
107 it('should work when passed to infiniteQuery with enabled: false', async () => {
108 const options = infiniteQueryOptions({
109 queryKey: ['key'],
110 queryFn: () => Promise.resolve('string'),
111 getNextPageParam: () => 1,
112 initialPageParam: 1,
113 enabled: false,
114 })
115β
116 const data = await new QueryClient().infiniteQuery(options)
117β
118 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
119 })
120β
121 it('should work when passed to infiniteQuery with skipToken', async () => {
122 const options = infiniteQueryOptions({
123 queryKey: ['key'],
124 queryFn: skipToken,
125 getNextPageParam: () => 1,
126 initialPageParam: 1,
127 })
128β
129 const data = await new QueryClient().infiniteQuery(options)
130β
131 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
132 })
133β
134 it('should tag the queryKey with the result type of the QueryFn', () => {
135 const key = queryKey()
136 const { queryKey: tagged } = infiniteQueryOptions({
137 queryKey: key,
138 queryFn: () => Promise.resolve('string'),
139 getNextPageParam: () => 1,
140 initialPageParam: 1,
141 })
142β
143 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
144 })
145 it('should tag the queryKey even if no promise is returned', () => {
146 const key = queryKey()
147 const { queryKey: tagged } = infiniteQueryOptions({
148 queryKey: key,
149 queryFn: () => 'string',
150 getNextPageParam: () => 1,
151 initialPageParam: 1,
152 })
153β
154 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
155 })
156 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
157 const key = queryKey()
158 const { queryKey: tagged } = infiniteQueryOptions({
159 queryKey: key,
160 queryFn: () => Promise.resolve('string'),
161 select: (data) => data.pages,
162 getNextPageParam: () => 1,
163 initialPageParam: 1,
164 })
165β
166 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
167 })
168 it('should return the proper type when passed to getQueryData', () => {
169 const key = queryKey()
170 const { queryKey: tagged } = infiniteQueryOptions({
171 queryKey: key,
172 queryFn: () => Promise.resolve('string'),
173 getNextPageParam: () => 1,
174 initialPageParam: 1,
175 })
176β
177 const queryClient = new QueryClient()
178 const data = queryClient.getQueryData(tagged)
179β
180 expectTypeOf(data).toEqualTypeOf<
181 InfiniteData<string, unknown> | undefined
182 >()
183 })
184 it('should properly type when passed to setQueryData', () => {
185 const key = queryKey()
186 const { queryKey: tagged } = infiniteQueryOptions({
187 queryKey: key,
188 queryFn: () => Promise.resolve('string'),
189 getNextPageParam: () => 1,
190 initialPageParam: 1,
191 })
192β
193 const queryClient = new QueryClient()
194 const data = queryClient.setQueryData(tagged, (prev) => {
195 expectTypeOf(prev).toEqualTypeOf<
196 InfiniteData<string, unknown> | undefined
197 >()
198 return prev
199 })
200β
201 expectTypeOf(data).toEqualTypeOf<
202 InfiniteData<string, unknown> | undefined
203 >()
204 })
205β
206 it('should not be allowed to be passed to non-infinite query functions', () => {
207 const key = queryKey()
208 const queryClient = new QueryClient()
209 const options = infiniteQueryOptions({
210 queryKey: key,
211 queryFn: () => Promise.resolve('string'),
212 getNextPageParam: () => 1,
213 initialPageParam: 1,
214 })
215 assertType(
216 // @ts-expect-error cannot pass infinite options to non-infinite query functions
217 injectQuery(() => options),
218 )
219 assertType(
220 // @ts-expect-error cannot pass infinite options to non-infinite query functions
221 queryClient.ensureQueryData(options),
222 )
223 assertType(
224 // @ts-expect-error cannot pass infinite options to non-infinite query functions
225 queryClient.fetchQuery(options),
226 )
227 assertType(
228 // @ts-expect-error cannot pass infinite options to non-infinite query functions
229 queryClient.prefetchQuery(options),
230 )
231 })
232β
233 it('should allow optional initialData function', () => {
234 const key = queryKey()
235 const initialData: { example: boolean } | undefined = { example: true }
236 const queryOptions = infiniteQueryOptions({
237 queryKey: key,
238 queryFn: () => initialData,
239 initialData: initialData
240 ? () => ({ pages: [initialData], pageParams: [] })
241 : undefined,
242 getNextPageParam: () => 1,
243 initialPageParam: 1,
244 })
245 expectTypeOf(queryOptions.initialData).toExtend<
246 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
247 | InfiniteData<{ example: boolean }, number>
248 | undefined
249 >()
250 })
251β
252 it('should allow optional initialData object', () => {
253 const key = queryKey()
254 const initialData: { example: boolean } | undefined = { example: true }
255 const queryOptions = infiniteQueryOptions({
256 queryKey: key,
257 queryFn: () => initialData,
258 initialData: initialData
259 ? { pages: [initialData], pageParams: [] }
260 : undefined,
261 getNextPageParam: () => 1,
262 initialPageParam: 1,
263 })
264 expectTypeOf(queryOptions.initialData).toExtend<
265 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
266 | InfiniteData<{ example: boolean }, number>
267 | undefined
268 >()
269 })
270β
271 it('should return a custom query key type', () => {
272 type MyQueryKey = [Array<string>, { type: 'foo' }]
273β
274 const options = infiniteQueryOptions({
275 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
276 queryFn: () => Promise.resolve(1),
277 getNextPageParam: () => 1,
278 initialPageParam: 1,
279 })
280β
281 expectTypeOf(options.queryKey).toEqualTypeOf<
282 DataTag<MyQueryKey, InfiniteData<number>, Error>
283 >()
284 })
285β
286 it('should return a custom query key type with datatag', () => {
287 type MyQueryKey = DataTag<
288 [Array<string>, { type: 'foo' }],
289 number,
290 Error & { myMessage: string }
291 >
292β
293 const options = infiniteQueryOptions({
294 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
295 queryFn: () => Promise.resolve(1),
296 getNextPageParam: () => 1,
297 initialPageParam: 1,
298 })
299β
300 expectTypeOf(options.queryKey).toEqualTypeOf<
301 DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
302 >()
303 })
304})