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

infiniteQueryOptions.test-d.ts

295 lines9.0 KB

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