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

infiniteQueryOptions.test-d.tsx

241 lines7.9 KB

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