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 const data = await new QueryClient().fetchInfiniteQuery({
119 queryKey: key,
120 queryFn: () => Promise.resolve('string'),
121 getNextPageParam: () => 1,
122 initialPageParam: 1,
123 pages: 5,
124 })
125β
126 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
127 })
128β
129 it('should not allow passing getNextPageParam without pages', () => {
130 const key = queryKey()
131 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
132 {
133 queryKey: key,
134 queryFn: () => Promise.resolve('string'),
135 initialPageParam: 1,
136 getNextPageParam: () => 1,
137 },
138 ])
139 })
140β
141 it('should not allow passing pages without getNextPageParam', () => {
142 const key = queryKey()
143 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
144 // @ts-expect-error Property 'getNextPageParam' is missing
145 {
146 queryKey: key,
147 queryFn: () => Promise.resolve('string'),
148 initialPageParam: 1,
149 pages: 5,
150 },
151 ])
152 })
153})
154β
155describe('query', () => {
156 it('should return the type of the query fn', () => {
157 const result = new QueryClient().query({
158 queryKey: ['key'],
159 queryFn: () => Promise.resolve('string'),
160 })
161β
162 expectTypeOf(result).toEqualTypeOf<Promise<string>>()
163 })
164β
165 it('should return the selected type', () => {
166 const result = new QueryClient().query({
167 queryKey: ['key'],
168 queryFn: () => Promise.resolve('string'),
169 select: (data) => data.length,
170 })
171β
172 expectTypeOf(result).toEqualTypeOf<Promise<number>>()
173 })
174β
175 it('should not accept top-level options getters', () => {
176 assertType<Parameters<QueryClient['query']>>([
177 // @ts-expect-error One-shot imperative methods do not resolve top-level getters
178 () => ({
179 queryKey: ['key'],
180 queryFn: () => Promise.resolve('string'),
181 }),
182 ])
183 })
184})
185β
186describe('infiniteQuery', () => {
187 it('should return infinite data', async () => {
188 const data = await new QueryClient().infiniteQuery({
189 queryKey: ['key'],
190 queryFn: () => Promise.resolve('string'),
191 getNextPageParam: () => 1,
192 initialPageParam: 1,
193 })
194β
195 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
196 })
197β
198 it('should return the selected type', () => {
199 const result = new QueryClient().infiniteQuery({
200 queryKey: ['key'],
201 queryFn: () => Promise.resolve({ count: 1 }),
202 getNextPageParam: () => 2,
203 initialPageParam: 1,
204 select: (data) => data.pages.map((page) => page.count),
205 })
206β
207 expectTypeOf(result).toEqualTypeOf<Promise<Array<number>>>()
208 })
209β
210 it('should not accept top-level options getters', () => {
211 assertType<Parameters<QueryClient['infiniteQuery']>>([
212 // @ts-expect-error One-shot imperative methods do not resolve top-level getters
213 () => ({
214 queryKey: ['key'],
215 queryFn: () => Promise.resolve('string'),
216 getNextPageParam: () => 1,
217 initialPageParam: 1,
218 }),
219 ])
220 })
221β
222 it('should allow passing pages with getNextPageParam', () => {
223 assertType<Parameters<QueryClient['infiniteQuery']>>([
224 {
225 queryKey: ['key'],
226 queryFn: () => Promise.resolve('string'),
227 initialPageParam: 1,
228 getNextPageParam: () => 1,
229 pages: 5,
230 },
231 ])
232 })
233β
234 it('should not allow passing pages without getNextPageParam', () => {
235 assertType<Parameters<QueryClient['infiniteQuery']>>([
236 // @ts-expect-error Property 'getNextPageParam' is missing
237 {
238 queryKey: ['key'],
239 queryFn: () => Promise.resolve('string'),
240 initialPageParam: 1,
241 pages: 5,
242 },
243 ])
244 })
245β
246 it('should preserve page param inference', () => {
247 new QueryClient().infiniteQuery({
248 queryKey: ['key'],
249 queryFn: ({ pageParam }) => {
250 expectTypeOf(pageParam).toEqualTypeOf<number>()
251 return Promise.resolve(pageParam.toString())
252 },
253 initialPageParam: 1,
254 getNextPageParam: () => undefined,
255 })
256 })
257})