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