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