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

infiniteQueryOptions.test-d.tsx

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