Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { streamedQuery } from '../streamedQuery'
3import type { QueryClient, QueryFunction, QueryKey, QueryMeta } from '..'
4β
5function numberStream(): AsyncIterable<number> {
6 return {} as AsyncIterable<number>
7}
8β
9// `StreamedQueryParams` is a union, and `keyof` on a union is the intersection
10// of its members' keys, so each arm has to be pinned on its own.
11type Params = Parameters<typeof streamedQuery<number, string, ['stream']>>[0]
12type SimpleParams = Extract<Params, { reducer?: never }>
13type ReducibleParams = Exclude<Params, { reducer?: never }>
14β
15describe('streamedQuery', () => {
16 it('should infer TQueryFnData from the stream returned by streamFn', () => {
17 expectTypeOf(
18 streamedQuery({
19 streamFn: () => numberStream(),
20 }),
21 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
22 })
23β
24 it('should infer TQueryFnData from a promise of a stream', () => {
25 expectTypeOf(
26 streamedQuery({
27 streamFn: () => Promise.resolve(numberStream()),
28 }),
29 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
30 })
31β
32 it('should default TQueryFnData to unknown when the stream type cannot be inferred', () => {
33 expectTypeOf(
34 streamedQuery({
35 streamFn: () =>
36 ({}) as AsyncIterable<unknown> | Promise<AsyncIterable<unknown>>,
37 }),
38 ).toEqualTypeOf<QueryFunction<Array<unknown>, QueryKey>>()
39 })
40β
41 it('should default TQueryFnData to unknown when there is no inference site', () => {
42 // an `AsyncIterable` of an unconstrained type parameter gives the
43 // inference nothing to latch onto, so the default has to fill in
44 function streamOf<T>(): AsyncIterable<T> {
45 return {} as AsyncIterable<T>
46 }
47β
48 expectTypeOf(streamedQuery({ streamFn: () => streamOf() })).toEqualTypeOf<
49 QueryFunction<Array<unknown>, QueryKey>
50 >()
51 })
52β
53 it('should default TData to an array of the chunk type', () => {
54 expectTypeOf(
55 streamedQuery<{ value: string }>({
56 streamFn: () => ({}) as AsyncIterable<{ value: string }>,
57 }),
58 ).toEqualTypeOf<QueryFunction<Array<{ value: string }>, QueryKey>>()
59 })
60β
61 it('should set TData to the reducer result when a reducer is provided', () => {
62 expectTypeOf(
63 streamedQuery({
64 streamFn: () => numberStream(),
65 reducer: (acc: string, chunk: number) => acc + chunk,
66 initialValue: '',
67 }),
68 ).toEqualTypeOf<QueryFunction<string, QueryKey>>()
69 })
70β
71 it('should default TQueryKey to QueryKey', () => {
72 streamedQuery({
73 streamFn: (context) => {
74 expectTypeOf(context.queryKey).toEqualTypeOf<QueryKey>()
75 return numberStream()
76 },
77 })
78 })
79β
80 it('should carry TQueryKey into the returned QueryFunction when given explicitly', () => {
81 expectTypeOf(
82 streamedQuery<number, Array<number>, ['stream', number]>({
83 streamFn: () => numberStream(),
84 }),
85 ).toEqualTypeOf<QueryFunction<Array<number>, ['stream', number]>>()
86 })
87β
88 it('should constrain TQueryKey to a query key', () => {
89 // @ts-expect-error a query key must be an array
90 streamedQuery<number, Array<number>, string>({
91 streamFn: () => numberStream(),
92 })
93β
94 expectTypeOf(
95 streamedQuery<number, Array<number>, ReadonlyArray<unknown>>({
96 streamFn: () => numberStream(),
97 }),
98 ).toEqualTypeOf<QueryFunction<Array<number>, ReadonlyArray<unknown>>>()
99 })
100β
101 describe('StreamedQueryParams', () => {
102 it('should be the only parameter', () => {
103 streamedQuery<number, Array<number>, ['stream']>(
104 { streamFn: () => numberStream() },
105 // @ts-expect-error there is no second parameter
106 'extra',
107 )
108β
109 expectTypeOf<
110 Parameters<
111 typeof streamedQuery<number, Array<number>, ['stream']>
112 >['length']
113 >().toEqualTypeOf<1>()
114 })
115β
116 it('should only expose the options of its simple form', () => {
117 expectTypeOf<keyof SimpleParams>().toEqualTypeOf<
118 'streamFn' | 'refetchMode' | 'reducer' | 'initialValue'
119 >()
120 })
121β
122 it('should only expose the options of its reducible form', () => {
123 expectTypeOf<keyof ReducibleParams>().toEqualTypeOf<
124 'streamFn' | 'refetchMode' | 'reducer' | 'initialValue'
125 >()
126 })
127β
128 it('should keep every option writable', () => {
129 // `Pick` preserves the `readonly` modifier that an indexed access such as
130 // `Params['refetchMode']` silently strips, and distributes over the union
131 expectTypeOf<Pick<Params, 'streamFn'>>().toEqualTypeOf<{
132 streamFn: (
133 context: Parameters<QueryFunction<string, ['stream']>>[0],
134 ) => AsyncIterable<number> | Promise<AsyncIterable<number>>
135 }>()
136 expectTypeOf<Pick<Params, 'refetchMode'>>().toEqualTypeOf<{
137 refetchMode?: 'append' | 'reset' | 'replace'
138 }>()
139 expectTypeOf<
140 Pick<ReducibleParams, 'reducer' | 'initialValue'>
141 >().toEqualTypeOf<{
142 reducer: (acc: string, chunk: number) => string
143 initialValue: string
144 }>()
145 })
146β
147 it('should accept an object literal with all of its options', () => {
148 expectTypeOf(
149 streamedQuery<number, string, ['stream']>({
150 streamFn: () => numberStream(),
151 refetchMode: 'append',
152 reducer: (acc, chunk) => acc + chunk,
153 initialValue: '',
154 }),
155 ).toEqualTypeOf<QueryFunction<string, ['stream']>>()
156 })
157 })
158β
159 describe('streamFn', () => {
160 it('should be required', () => {
161 // @ts-expect-error streamFn is required
162 streamedQuery({})
163β
164 expectTypeOf(
165 streamedQuery({ streamFn: () => numberStream() }),
166 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
167 })
168β
169 it('should type the context it receives', () => {
170 streamedQuery<number, Array<number>, ['stream', number]>({
171 streamFn: (context) => {
172 expectTypeOf(context.queryKey).toEqualTypeOf<['stream', number]>()
173 expectTypeOf(context.client).toEqualTypeOf<QueryClient>()
174 expectTypeOf(context.signal).toEqualTypeOf<AbortSignal>()
175 expectTypeOf(context.meta).toEqualTypeOf<QueryMeta | undefined>()
176 expectTypeOf(context.pageParam).toEqualTypeOf<unknown>()
177 expectTypeOf(context.direction).toEqualTypeOf<unknown>()
178 return numberStream()
179 },
180 })
181 })
182β
183 it('should reject a stream whose chunk type does not match', () => {
184 streamedQuery<number>({
185 // @ts-expect-error the stream must yield numbers
186 streamFn: () => ({}) as AsyncIterable<string>,
187 })
188β
189 expectTypeOf(
190 streamedQuery<number>({
191 streamFn: () => ({}) as AsyncIterable<number>,
192 }),
193 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
194 })
195β
196 it('should reject a stream that is not an AsyncIterable', () => {
197 streamedQuery({
198 // @ts-expect-error a plain array is not an AsyncIterable
199 streamFn: () => [1, 2, 3],
200 })
201β
202 expectTypeOf(
203 streamedQuery({ streamFn: () => numberStream() }),
204 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
205 })
206 })
207β
208 describe('refetchMode', () => {
209 it('should be optional', () => {
210 expectTypeOf(
211 streamedQuery({ streamFn: () => numberStream() }),
212 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
213 })
214β
215 it('should only accept append, reset or replace', () => {
216 streamedQuery({
217 streamFn: () => numberStream(),
218 // @ts-expect-error 'merge' is not a valid refetch mode
219 refetchMode: 'merge',
220 })
221β
222 expectTypeOf(
223 streamedQuery({
224 streamFn: () => numberStream(),
225 refetchMode: 'append',
226 }),
227 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
228 expectTypeOf(
229 streamedQuery({
230 streamFn: () => numberStream(),
231 refetchMode: 'reset',
232 }),
233 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
234 expectTypeOf(
235 streamedQuery({
236 streamFn: () => numberStream(),
237 refetchMode: 'replace',
238 }),
239 ).toEqualTypeOf<QueryFunction<Array<number>, QueryKey>>()
240 })
241 })
242β
243 describe('reducer', () => {
244 it('should not be accepted without initialValue', () => {
245 streamedQuery({
246 streamFn: () => numberStream(),
247 // @ts-expect-error a reducer requires an initialValue
248 reducer: (acc: string, chunk: number) => acc + chunk,
249 })
250β
251 expectTypeOf(
252 streamedQuery({
253 streamFn: () => numberStream(),
254 reducer: (acc: string, chunk: number) => acc + chunk,
255 initialValue: '',
256 }),
257 ).toEqualTypeOf<QueryFunction<string, QueryKey>>()
258 })
259β
260 it('should type its accumulator and chunk parameters', () => {
261 streamedQuery<number, string>({
262 streamFn: () => numberStream(),
263 reducer: (acc, chunk) => {
264 expectTypeOf(acc).toEqualTypeOf<string>()
265 expectTypeOf(chunk).toEqualTypeOf<number>()
266 return acc + chunk
267 },
268 initialValue: '',
269 })
270 })
271β
272 it('should reject a return type that does not match the accumulator', () => {
273 streamedQuery<number, string>({
274 streamFn: () => numberStream(),
275 // @ts-expect-error the reducer must return the accumulator type
276 reducer: (_acc, chunk) => chunk,
277 initialValue: '',
278 })
279β
280 expectTypeOf(
281 streamedQuery<number, string>({
282 streamFn: () => numberStream(),
283 reducer: (_acc, chunk) => String(chunk),
284 initialValue: '',
285 }),
286 ).toEqualTypeOf<QueryFunction<string, QueryKey>>()
287 })
288 })
289β
290 describe('initialValue', () => {
291 it('should not be accepted without a reducer', () => {
292 streamedQuery({
293 streamFn: () => numberStream(),
294 // @ts-expect-error an initialValue requires a reducer
295 initialValue: '',
296 })
297β
298 expectTypeOf(
299 streamedQuery({
300 streamFn: () => numberStream(),
301 reducer: (acc: string, chunk: number) => acc + chunk,
302 initialValue: '',
303 }),
304 ).toEqualTypeOf<QueryFunction<string, QueryKey>>()
305 })
306β
307 it('should be required alongside a reducer', () => {
308 streamedQuery<number, string>({
309 streamFn: () => numberStream(),
310 reducer: (acc, chunk) => acc + chunk,
311 // @ts-expect-error initialValue must match TData
312 initialValue: 0,
313 })
314β
315 expectTypeOf(
316 streamedQuery<number, string>({
317 streamFn: () => numberStream(),
318 reducer: (acc, chunk) => acc + chunk,
319 initialValue: '',
320 }),
321 ).toEqualTypeOf<QueryFunction<string, QueryKey>>()
322 })
323 })
324β
325 describe('QueryFunction', () => {
326 it('should return the accumulated data, not a single chunk', () => {
327 const queryFn = streamedQuery({
328 streamFn: () => numberStream(),
329 })
330β
331 expectTypeOf(queryFn).returns.toEqualTypeOf<
332 Array<number> | Promise<Array<number>>
333 >()
334 })
335β
336 it('should take the query function context as its only parameter', () => {
337 const queryFn = streamedQuery<number, Array<number>, ['stream']>({
338 streamFn: () => numberStream(),
339 })
340β
341 expectTypeOf(queryFn).parameter(0).toHaveProperty('queryKey')
342 expectTypeOf(queryFn)
343 .parameter(0)
344 .toEqualTypeOf<
345 Parameters<QueryFunction<Array<number>, ['stream']>>[0]
346 >()
347 })
348 })
349})