Open History to see the last commit
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 fetchInfiniteQuery', async () => {
67 const options = infiniteQueryOptions({
68 queryKey: queryKey(),
69 queryFn: () => Promise.resolve('string'),
70 getNextPageParam: () => 1,
71 initialPageParam: 1,
72 })
73β
74 const data = await new QueryClient().fetchInfiniteQuery(options)
75β
76 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
77 })
78 it('should tag the queryKey with the result type of the QueryFn', () => {
79 const { queryKey: tagged } = infiniteQueryOptions({
80 queryKey: queryKey(),
81 queryFn: () => Promise.resolve('string'),
82 getNextPageParam: () => 1,
83 initialPageParam: 1,
84 })
85β
86 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
87 })
88 it('should tag the queryKey even if no promise is returned', () => {
89 const { queryKey: tagged } = infiniteQueryOptions({
90 queryKey: queryKey(),
91 queryFn: () => 'string',
92 getNextPageParam: () => 1,
93 initialPageParam: 1,
94 })
95β
96 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
97 })
98 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
99 const { queryKey: tagged } = infiniteQueryOptions({
100 queryKey: queryKey(),
101 queryFn: () => Promise.resolve('string'),
102 select: (data) => data.pages,
103 getNextPageParam: () => 1,
104 initialPageParam: 1,
105 })
106β
107 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
108 })
109 it('should return the proper type when passed to getQueryData', () => {
110 const { queryKey: tagged } = infiniteQueryOptions({
111 queryKey: queryKey(),
112 queryFn: () => Promise.resolve('string'),
113 getNextPageParam: () => 1,
114 initialPageParam: 1,
115 })
116β
117 const queryClient = new QueryClient()
118 const data = queryClient.getQueryData(tagged)
119β
120 expectTypeOf(data).toEqualTypeOf<
121 InfiniteData<string, unknown> | undefined
122 >()
123 })
124 it('should properly type when passed to setQueryData', () => {
125 const { queryKey: tagged } = infiniteQueryOptions({
126 queryKey: queryKey(),
127 queryFn: () => Promise.resolve('string'),
128 getNextPageParam: () => 1,
129 initialPageParam: 1,
130 })
131β
132 const queryClient = new QueryClient()
133 const data = queryClient.setQueryData(tagged, (prev) => {
134 expectTypeOf(prev).toEqualTypeOf<
135 InfiniteData<string, unknown> | undefined
136 >()
137 return prev
138 })
139β
140 expectTypeOf(data).toEqualTypeOf<
141 InfiniteData<string, unknown> | undefined
142 >()
143 })
144 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
145 const options = infiniteQueryOptions({
146 queryKey: queryKey(),
147 queryFn:
148 Math.random() > 0.5 ? skipToken : () => Promise.resolve('string'),
149 getNextPageParam: () => 1,
150 initialPageParam: 1,
151 })
152 // @ts-expect-error TS2345
153 const { data } = useSuspenseInfiniteQuery(options)
154 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
155 })
156β
157 it('should not be allowed to be passed to non-infinite query functions', () => {
158 const queryClient = new QueryClient()
159 const options = infiniteQueryOptions({
160 queryKey: queryKey(),
161 queryFn: () => Promise.resolve('string'),
162 getNextPageParam: () => 1,
163 initialPageParam: 1,
164 })
165 assertType(
166 // @ts-expect-error cannot pass infinite options to non-infinite query functions
167 useQuery(options),
168 )
169 assertType(
170 // @ts-expect-error cannot pass infinite options to non-infinite query functions
171 queryClient.ensureQueryData(options),
172 )
173 assertType(
174 // @ts-expect-error cannot pass infinite options to non-infinite query functions
175 queryClient.fetchQuery(options),
176 )
177 assertType(
178 // @ts-expect-error cannot pass infinite options to non-infinite query functions
179 queryClient.prefetchQuery(options),
180 )
181 })
182β
183 it('allow optional initialData function', () => {
184 const initialData: { example: boolean } | undefined = { example: true }
185 const queryOptions = infiniteQueryOptions({
186 queryKey: queryKey(),
187 queryFn: () => initialData,
188 initialData: initialData
189 ? () => ({ pages: [initialData], pageParams: [] })
190 : undefined,
191 getNextPageParam: () => 1,
192 initialPageParam: 1,
193 })
194 expectTypeOf(queryOptions.initialData).toExtend<
195 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
196 | InfiniteData<{ example: boolean }, number>
197 | undefined
198 >()
199 })
200β
201 it('allow optional initialData object', () => {
202 const initialData: { example: boolean } | undefined = { example: true }
203 const queryOptions = infiniteQueryOptions({
204 queryKey: queryKey(),
205 queryFn: () => initialData,
206 initialData: initialData
207 ? { pages: [initialData], pageParams: [] }
208 : undefined,
209 getNextPageParam: () => 1,
210 initialPageParam: 1,
211 })
212 expectTypeOf(queryOptions.initialData).toExtend<
213 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
214 | InfiniteData<{ example: boolean }, number>
215 | undefined
216 >()
217 })
218β
219 it('should return a custom query key type', () => {
220 type MyQueryKey = [Array<string>, { type: 'foo' }]
221β
222 const options = infiniteQueryOptions({
223 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
224 queryFn: () => Promise.resolve(1),
225 getNextPageParam: () => 1,
226 initialPageParam: 1,
227 })
228β
229 expectTypeOf(options.queryKey).toEqualTypeOf<
230 DataTag<MyQueryKey, InfiniteData<number>, Error>
231 >()
232 })
233β
234 it('should return a custom query key type with datatag', () => {
235 type MyQueryKey = DataTag<
236 [Array<string>, { type: 'foo' }],
237 number,
238 Error & { myMessage: string }
239 >
240β
241 const options = infiniteQueryOptions({
242 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
243 queryFn: () => Promise.resolve(1),
244 getNextPageParam: () => 1,
245 initialPageParam: 1,
246 })
247β
248 expectTypeOf(options.queryKey).toEqualTypeOf<
249 DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
250 >()
251 })
252})