Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { queryKey } from '@tanstack/query-test-utils'
3import { QueryClient } from '../queryClient'
4import type { DataTag, InfiniteData } from '@tanstack/query-core'
5β
6describe('getQueryData', () => {
7 it('should be typed if key is tagged', () => {
8 const key = ['key'] as DataTag<Array<string>, number>
9 const queryClient = new QueryClient()
10 const data = queryClient.getQueryData(key)
11β
12 expectTypeOf(data).toEqualTypeOf<number | undefined>()
13 })
14β
15 it('should infer unknown if key is not tagged', () => {
16 const key = ['key'] as const
17 const queryClient = new QueryClient()
18 const data = queryClient.getQueryData(key)
19β
20 expectTypeOf(data).toEqualTypeOf<unknown>()
21 })
22β
23 it('should infer passed generic if passed', () => {
24 const key = ['key'] as const
25 const queryClient = new QueryClient()
26 const data = queryClient.getQueryData<number>(key)
27β
28 expectTypeOf(data).toEqualTypeOf<number | undefined>()
29 })
30β
31 it('should only allow Arrays to be passed', () => {
32 assertType<Parameters<QueryClient['getQueryData']>>([
33 // @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey'
34 { queryKey: 'key' },
35 ])
36 })
37})
38β
39describe('setQueryData', () => {
40 it('updater should be typed if key is tagged', () => {
41 const key = ['key'] as DataTag<Array<string>, number>
42 const queryClient = new QueryClient()
43 const data = queryClient.setQueryData(key, (prev) => {
44 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
45 return prev
46 })
47 expectTypeOf(data).toEqualTypeOf<number | undefined>()
48 })
49β
50 it('value should be typed if key is tagged', () => {
51 const key = ['key'] as DataTag<Array<string>, number>
52 const queryClient = new QueryClient()
53β
54 // @ts-expect-error value should be a number
55 queryClient.setQueryData(key, '1')
56β
57 // @ts-expect-error value should be a number
58 queryClient.setQueryData(key, () => '1')
59β
60 const data = queryClient.setQueryData(key, 1)
61β
62 expectTypeOf(data).toEqualTypeOf<number | undefined>()
63 })
64β
65 it('should infer unknown for updater if key is not tagged', () => {
66 const key = ['key'] as const
67 const queryClient = new QueryClient()
68 const data = queryClient.setQueryData(key, (prev) => {
69 expectTypeOf(prev).toEqualTypeOf<unknown>()
70 return prev
71 })
72 expectTypeOf(data).toEqualTypeOf<unknown>()
73 })
74β
75 it('should infer unknown for value if key is not tagged', () => {
76 const key = ['key'] as const
77 const queryClient = new QueryClient()
78 const data = queryClient.setQueryData(key, 'foo')
79β
80 expectTypeOf(data).toEqualTypeOf<unknown>()
81 })
82β
83 it('should infer passed generic if passed', () => {
84 const key = ['key'] as const
85 const queryClient = new QueryClient()
86 const data = queryClient.setQueryData<string>(key, (prev) => {
87 expectTypeOf(prev).toEqualTypeOf<string | undefined>()
88 return prev
89 })
90 expectTypeOf(data).toEqualTypeOf<string | undefined>()
91 })
92β
93 it('should infer passed generic for value', () => {
94 const key = ['key'] as const
95 const queryClient = new QueryClient()
96 const data = queryClient.setQueryData<string>(key, 'foo')
97β
98 expectTypeOf(data).toEqualTypeOf<string | undefined>()
99 })
100β
101 it('should preserve updater parameter type inference when used in functions with explicit return types', () => {
102 const key = ['key'] as DataTag<Array<string>, number>
103 const queryClient = new QueryClient()
104β
105 // Simulate usage inside a function with explicit return type
106 // The outer function returns 'unknown' but this shouldn't affect the updater's type inference
107 ;(() =>
108 queryClient.setQueryData(key, (data) => {
109 expectTypeOf(data).toEqualTypeOf<number | undefined>()
110 return data
111 })) satisfies () => unknown
112 })
113})
114β
115describe('fetchInfiniteQuery', () => {
116 it('should allow passing pages', async () => {
117 const key = queryKey()
118 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
119 const data = await new QueryClient().fetchInfiniteQuery({
120 queryKey: key,
121 queryFn: () => Promise.resolve('string'),
122 getNextPageParam: () => 1,
123 initialPageParam: 1,
124 pages: 5,
125 })
126β
127 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
128 })
129β
130 it('should not allow passing getNextPageParam without pages', () => {
131 const key = queryKey()
132 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
133 {
134 queryKey: key,
135 queryFn: () => Promise.resolve('string'),
136 initialPageParam: 1,
137 getNextPageParam: () => 1,
138 },
139 ])
140 })
141β
142 it('should not allow passing pages without getNextPageParam', () => {
143 const key = queryKey()
144 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
145 // @ts-expect-error Property 'getNextPageParam' is missing
146 {
147 queryKey: key,
148 queryFn: () => Promise.resolve('string'),
149 initialPageParam: 1,
150 pages: 5,
151 },
152 ])
153 })
154})
155β
156describe('query', () => {
157 it('should return the type of the query fn', () => {
158 const result = new QueryClient().query({
159 queryKey: ['key'],
160 queryFn: () => Promise.resolve('string'),
161 })
162β
163 expectTypeOf(result).toEqualTypeOf<Promise<string>>()
164 })
165β
166 it('should return the selected type', () => {
167 const result = new QueryClient().query({
168 queryKey: ['key'],
169 queryFn: () => Promise.resolve('string'),
170 select: (data) => data.length,
171 })
172β
173 expectTypeOf(result).toEqualTypeOf<Promise<number>>()
174 })
175β
176 it('should not accept top-level options getters', () => {
177 assertType<Parameters<QueryClient['query']>>([
178 // @ts-expect-error One-shot imperative methods do not resolve top-level getters
179 () => ({
180 queryKey: ['key'],
181 queryFn: () => Promise.resolve('string'),
182 }),
183 ])
184 })
185})
186β
187describe('infiniteQuery', () => {
188 it('should return infinite data', async () => {
189 const data = await new QueryClient().infiniteQuery({
190 queryKey: ['key'],
191 queryFn: () => Promise.resolve('string'),
192 getNextPageParam: () => 1,
193 initialPageParam: 1,
194 })
195β
196 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
197 })
198β
199 it('should return the selected type', () => {
200 const result = new QueryClient().infiniteQuery({
201 queryKey: ['key'],
202 queryFn: () => Promise.resolve({ count: 1 }),
203 getNextPageParam: () => 2,
204 initialPageParam: 1,
205 select: (data) => data.pages.map((page) => page.count),
206 })
207β
208 expectTypeOf(result).toEqualTypeOf<Promise<Array<number>>>()
209 })
210β
211 it('should not accept top-level options getters', () => {
212 assertType<Parameters<QueryClient['infiniteQuery']>>([
213 // @ts-expect-error One-shot imperative methods do not resolve top-level getters
214 () => ({
215 queryKey: ['key'],
216 queryFn: () => Promise.resolve('string'),
217 getNextPageParam: () => 1,
218 initialPageParam: 1,
219 }),
220 ])
221 })
222β
223 it('should allow passing pages with getNextPageParam', () => {
224 assertType<Parameters<QueryClient['infiniteQuery']>>([
225 {
226 queryKey: ['key'],
227 queryFn: () => Promise.resolve('string'),
228 initialPageParam: 1,
229 getNextPageParam: () => 1,
230 pages: 5,
231 },
232 ])
233 })
234β
235 it('should not allow passing pages without getNextPageParam', () => {
236 assertType<Parameters<QueryClient['infiniteQuery']>>([
237 // @ts-expect-error Property 'getNextPageParam' is missing
238 {
239 queryKey: ['key'],
240 queryFn: () => Promise.resolve('string'),
241 initialPageParam: 1,
242 pages: 5,
243 },
244 ])
245 })
246β
247 it('should preserve page param inference', () => {
248 new QueryClient().infiniteQuery({
249 queryKey: ['key'],
250 queryFn: ({ pageParam }) => {
251 expectTypeOf(pageParam).toEqualTypeOf<number>()
252 return Promise.resolve(pageParam.toString())
253 },
254 initialPageParam: 1,
255 getNextPageParam: () => undefined,
256 })
257 })
258})