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

infiniteQueryOptions.test-d.tsx

253 lines8.1 KB

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