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