Open History to see the last commit
1import { beforeEach, describe, expectTypeOf, it } from 'vitest'
2import {
3 QueryClient,
4 defaultShouldDehydrateMutation,
5 defaultShouldDehydrateQuery,
6 dehydrate,
7 dehydrateQuery,
8 hydrate,
9} from '..'
10import type {
11 DefaultError,
12 DehydrateOptions,
13 DehydratedState,
14 HydrateOptions,
15 Mutation,
16 MutationKey,
17 MutationMeta,
18 MutationOptions,
19 MutationScope,
20 MutationState,
21 Query,
22 QueryKey,
23 QueryMeta,
24 QueryOptions,
25 QueryState,
26} from '..'
27β
28type DehydratedQuery = DehydratedState['queries'][number]
29type DehydratedMutation = DehydratedState['mutations'][number]
30β
31describe('hydration', () => {
32 let queryClient: QueryClient
33β
34 beforeEach(() => {
35 queryClient = new QueryClient()
36 })
37β
38 describe('dehydrate', () => {
39 it('should take a query client and return a dehydrated state', () => {
40 expectTypeOf(dehydrate(queryClient)).toEqualTypeOf<DehydratedState>()
41 })
42β
43 it('should require a query client', () => {
44 // @ts-expect-error the query client is required
45 dehydrate()
46β
47 expectTypeOf(dehydrate(queryClient)).toEqualTypeOf<DehydratedState>()
48 })
49β
50 it('should reject a value that is not a query client', () => {
51 // @ts-expect-error a plain object is not a query client
52 dehydrate({})
53β
54 expectTypeOf(dehydrate).parameter(0).toEqualTypeOf<QueryClient>()
55 })
56β
57 it('should take optional dehydrate options as its second parameter', () => {
58 expectTypeOf(dehydrate)
59 .parameter(1)
60 .toEqualTypeOf<DehydrateOptions | undefined>()
61β
62 expectTypeOf(dehydrate(queryClient, {})).toEqualTypeOf<DehydratedState>()
63 })
64β
65 it('should take no more than two parameters', () => {
66 // @ts-expect-error there is no third parameter
67 dehydrate(queryClient, {}, 'extra')
68β
69 expectTypeOf(dehydrate).parameters.toEqualTypeOf<
70 [client: QueryClient, options?: DehydrateOptions]
71 >()
72 })
73β
74 it('should reject an unknown dehydrate option', () => {
75 dehydrate(queryClient, {
76 // @ts-expect-error 'shouldDehydrateAll' is not a dehydrate option
77 shouldDehydrateAll: true,
78 })
79β
80 expectTypeOf(
81 dehydrate(queryClient, { shouldDehydrateQuery: () => true }),
82 ).toEqualTypeOf<DehydratedState>()
83 })
84 })
85β
86 describe('hydrate', () => {
87 it('should take a query client and a dehydrated state and return void', () => {
88 expectTypeOf(
89 hydrate(queryClient, { queries: [], mutations: [] }),
90 ).toEqualTypeOf<void>()
91 })
92β
93 it('should accept a partial dehydrated state', () => {
94 expectTypeOf(hydrate(queryClient, {})).toEqualTypeOf<void>()
95 expectTypeOf(hydrate(queryClient, { queries: [] })).toEqualTypeOf<void>()
96 expectTypeOf(
97 hydrate(queryClient, { mutations: [] }),
98 ).toEqualTypeOf<void>()
99β
100 expectTypeOf(hydrate)
101 .parameter(1)
102 .toEqualTypeOf<Partial<DehydratedState>>()
103 })
104β
105 it('should require the dehydrated state', () => {
106 // @ts-expect-error the dehydrated state is required
107 hydrate(queryClient)
108β
109 expectTypeOf(hydrate(queryClient, {})).toEqualTypeOf<void>()
110 })
111β
112 it('should take optional hydrate options as its third parameter', () => {
113 expectTypeOf(hydrate)
114 .parameter(2)
115 .toEqualTypeOf<HydrateOptions | undefined>()
116β
117 expectTypeOf(
118 hydrate(queryClient, {}, { defaultOptions: {} }),
119 ).toEqualTypeOf<void>()
120 })
121β
122 it('should take no more than three parameters', () => {
123 // @ts-expect-error there is no fourth parameter
124 hydrate(queryClient, {}, {}, 'extra')
125β
126 expectTypeOf(hydrate).parameters.toEqualTypeOf<
127 [
128 client: QueryClient,
129 dehydratedState: Partial<DehydratedState>,
130 options?: HydrateOptions,
131 ]
132 >()
133 })
134β
135 it('should reject a dehydrated state with an unknown key', () => {
136 hydrate(queryClient, {
137 // @ts-expect-error 'infiniteQueries' is not part of a dehydrated state
138 infiniteQueries: [],
139 })
140β
141 expectTypeOf(hydrate(queryClient, { queries: [] })).toEqualTypeOf<void>()
142 })
143 })
144β
145 describe('dehydrateQuery', () => {
146 it('should take a query and return a dehydrated query', () => {
147 expectTypeOf(dehydrateQuery).parameter(0).toEqualTypeOf<Query>()
148 expectTypeOf(dehydrateQuery).returns.toEqualTypeOf<DehydratedQuery>()
149 })
150β
151 it('should take an optional serializeData transform', () => {
152 expectTypeOf(dehydrateQuery)
153 .parameter(1)
154 .toEqualTypeOf<((data: any) => any) | undefined>()
155 })
156β
157 it('should take an optional shouldRedactErrors predicate', () => {
158 expectTypeOf(dehydrateQuery)
159 .parameter(2)
160 .toEqualTypeOf<((error: unknown) => boolean) | undefined>()
161 })
162β
163 it('should require the query', () => {
164 // @ts-expect-error the query is required
165 dehydrateQuery()
166β
167 expectTypeOf(dehydrateQuery).parameters.toEqualTypeOf<
168 [
169 query: Query,
170 serializeData?: (data: any) => any,
171 shouldRedactErrors?: (error: unknown) => boolean,
172 ]
173 >()
174 })
175 })
176β
177 describe('DehydratedState', () => {
178 it('should have queries and mutations arrays', () => {
179 expectTypeOf<DehydratedState>().toEqualTypeOf<{
180 mutations: Array<DehydratedMutation>
181 queries: Array<DehydratedQuery>
182 }>()
183 })
184β
185 it('should require both of its keys', () => {
186 // @ts-expect-error mutations is required
187 const missing: DehydratedState = { queries: [] }
188β
189 expectTypeOf(missing).toEqualTypeOf<DehydratedState>()
190 expectTypeOf<DehydratedState>().toHaveProperty('queries')
191 expectTypeOf<DehydratedState>().toHaveProperty('mutations')
192 // neither key may be optional
193 expectTypeOf<
194 keyof {
195 [
196 K in keyof DehydratedState as {} extends Pick<DehydratedState, K>
197 ? K
198 : never
199 ]: true
200 }
201 >().toEqualTypeOf<never>()
202 })
203 })
204β
205 describe('DehydratedQuery', () => {
206 it('should keep every property writable', () => {
207 expectTypeOf<DehydratedQuery>().toEqualTypeOf<{
208 -readonly [K in keyof DehydratedQuery]: DehydratedQuery[K]
209 }>()
210 })
211β
212 describe('queryHash', () => {
213 it('should be a required string', () => {
214 expectTypeOf<DehydratedQuery['queryHash']>().toEqualTypeOf<string>()
215 })
216 })
217β
218 describe('queryKey', () => {
219 it('should be a required query key', () => {
220 expectTypeOf<DehydratedQuery['queryKey']>().toEqualTypeOf<QueryKey>()
221 })
222 })
223β
224 describe('state', () => {
225 it('should be a required query state', () => {
226 expectTypeOf<DehydratedQuery['state']>().toEqualTypeOf<QueryState>()
227 })
228 })
229β
230 describe('dehydratedAt', () => {
231 it('should be a required number', () => {
232 expectTypeOf<DehydratedQuery['dehydratedAt']>().toEqualTypeOf<number>()
233 })
234 })
235β
236 describe('promise', () => {
237 it('should be an optional promise of unknown', () => {
238 expectTypeOf<DehydratedQuery['promise']>().toEqualTypeOf<
239 Promise<unknown> | undefined
240 >()
241 })
242 })
243β
244 describe('meta', () => {
245 it('should be optional query meta', () => {
246 expectTypeOf<DehydratedQuery['meta']>().toEqualTypeOf<
247 QueryMeta | undefined
248 >()
249 })
250 })
251β
252 describe('queryType', () => {
253 it('should only ever be infinite', () => {
254 expectTypeOf<DehydratedQuery['queryType']>().toEqualTypeOf<
255 'infinite' | undefined
256 >()
257 })
258β
259 it('should reject any other query type', () => {
260 const query = {} as DehydratedQuery
261β
262 // @ts-expect-error 'paginated' is not a dehydrated query type
263 const invalid: typeof query.queryType = 'paginated'
264β
265 expectTypeOf(invalid).toEqualTypeOf<'infinite' | undefined>()
266 })
267 })
268β
269 it('should only expose its documented members', () => {
270 expectTypeOf<keyof DehydratedQuery>().toEqualTypeOf<
271 | 'queryHash'
272 | 'queryKey'
273 | 'state'
274 | 'dehydratedAt'
275 | 'promise'
276 | 'meta'
277 | 'queryType'
278 >()
279 })
280 })
281β
282 describe('DehydratedMutation', () => {
283 it('should keep every property writable', () => {
284 expectTypeOf<DehydratedMutation>().toEqualTypeOf<{
285 -readonly [K in keyof DehydratedMutation]: DehydratedMutation[K]
286 }>()
287 })
288β
289 describe('mutationKey', () => {
290 it('should be an optional mutation key', () => {
291 expectTypeOf<DehydratedMutation['mutationKey']>().toEqualTypeOf<
292 MutationKey | undefined
293 >()
294 })
295 })
296β
297 describe('state', () => {
298 it('should be a required mutation state', () => {
299 expectTypeOf<
300 DehydratedMutation['state']
301 >().toEqualTypeOf<MutationState>()
302 })
303 })
304β
305 describe('meta', () => {
306 it('should be optional mutation meta', () => {
307 expectTypeOf<DehydratedMutation['meta']>().toEqualTypeOf<
308 MutationMeta | undefined
309 >()
310 })
311 })
312β
313 describe('scope', () => {
314 it('should be an optional mutation scope', () => {
315 expectTypeOf<DehydratedMutation['scope']>().toEqualTypeOf<
316 MutationScope | undefined
317 >()
318 })
319 })
320β
321 it('should only expose its documented members', () => {
322 expectTypeOf<keyof DehydratedMutation>().toEqualTypeOf<
323 'mutationKey' | 'state' | 'meta' | 'scope'
324 >()
325 })
326 })
327β
328 describe('DehydrateOptions', () => {
329 it('should keep every option writable', () => {
330 expectTypeOf<DehydrateOptions>().toEqualTypeOf<{
331 -readonly [K in keyof DehydrateOptions]: DehydrateOptions[K]
332 }>()
333 })
334β
335 describe('serializeData', () => {
336 it('should be an optional transform', () => {
337 expectTypeOf<DehydrateOptions['serializeData']>().toEqualTypeOf<
338 ((data: any) => any) | undefined
339 >()
340 })
341 })
342β
343 describe('shouldDehydrateMutation', () => {
344 it('should take a mutation and return a boolean', () => {
345 expectTypeOf<
346 DehydrateOptions['shouldDehydrateMutation']
347 >().toEqualTypeOf<((mutation: Mutation) => boolean) | undefined>()
348 })
349β
350 it('should type the mutation it receives', () => {
351 dehydrate(queryClient, {
352 shouldDehydrateMutation: (mutation) => {
353 expectTypeOf(mutation).toEqualTypeOf<Mutation>()
354 expectTypeOf(mutation.state).toEqualTypeOf<MutationState>()
355 return true
356 },
357 })
358 })
359β
360 it('should only accept a boolean from the predicate', () => {
361 dehydrate(queryClient, {
362 // @ts-expect-error the predicate must return a boolean
363 shouldDehydrateMutation: () => 'yes',
364 })
365β
366 expectTypeOf(
367 dehydrate(queryClient, { shouldDehydrateMutation: () => false }),
368 ).toEqualTypeOf<DehydratedState>()
369 })
370 })
371β
372 describe('shouldDehydrateQuery', () => {
373 it('should take a query and return a boolean', () => {
374 expectTypeOf<DehydrateOptions['shouldDehydrateQuery']>().toEqualTypeOf<
375 ((query: Query) => boolean) | undefined
376 >()
377 })
378β
379 it('should type the query it receives', () => {
380 dehydrate(queryClient, {
381 shouldDehydrateQuery: (query) => {
382 expectTypeOf(query).toEqualTypeOf<Query>()
383 expectTypeOf(query.queryKey).toEqualTypeOf<QueryKey>()
384 expectTypeOf(query.queryHash).toEqualTypeOf<string>()
385 return true
386 },
387 })
388 })
389β
390 it('should only accept a boolean from the predicate', () => {
391 dehydrate(queryClient, {
392 // @ts-expect-error the predicate must return a boolean
393 shouldDehydrateQuery: () => 1,
394 })
395β
396 expectTypeOf(
397 dehydrate(queryClient, { shouldDehydrateQuery: () => true }),
398 ).toEqualTypeOf<DehydratedState>()
399 })
400 })
401β
402 describe('shouldRedactErrors', () => {
403 it('should take an unknown error and return a boolean', () => {
404 expectTypeOf<DehydrateOptions['shouldRedactErrors']>().toEqualTypeOf<
405 ((error: unknown) => boolean) | undefined
406 >()
407 })
408β
409 it('should type the error it receives as unknown', () => {
410 dehydrate(queryClient, {
411 shouldRedactErrors: (error) => {
412 expectTypeOf(error).toEqualTypeOf<unknown>()
413 return false
414 },
415 })
416 })
417 })
418β
419 it('should only expose its documented options', () => {
420 expectTypeOf<keyof DehydrateOptions>().toEqualTypeOf<
421 | 'serializeData'
422 | 'shouldDehydrateMutation'
423 | 'shouldDehydrateQuery'
424 | 'shouldRedactErrors'
425 >()
426 })
427 })
428β
429 describe('HydrateOptions', () => {
430 describe('defaultOptions', () => {
431 it('should be optional', () => {
432 expectTypeOf<HydrateOptions>().toEqualTypeOf<{
433 defaultOptions?: {
434 deserializeData?: (data: any) => any
435 queries?: QueryOptions
436 mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>
437 }
438 }>()
439 })
440 })
441β
442 describe('deserializeData', () => {
443 it('should be an optional transform', () => {
444 expectTypeOf<
445 NonNullable<HydrateOptions['defaultOptions']>['deserializeData']
446 >().toEqualTypeOf<((data: any) => any) | undefined>()
447 })
448 })
449β
450 describe('queries', () => {
451 it('should be optional query options', () => {
452 expectTypeOf<
453 NonNullable<HydrateOptions['defaultOptions']>['queries']
454 >().toEqualTypeOf<QueryOptions | undefined>()
455 })
456 })
457β
458 describe('mutations', () => {
459 it('should be optional mutation options with unknown data and variables', () => {
460 expectTypeOf<
461 NonNullable<HydrateOptions['defaultOptions']>['mutations']
462 >().toEqualTypeOf<
463 MutationOptions<unknown, DefaultError, unknown, unknown> | undefined
464 >()
465 })
466 })
467β
468 it('should reject an unknown hydrate option', () => {
469 hydrate(
470 queryClient,
471 {},
472 {
473 // @ts-expect-error 'defaults' is not a hydrate option
474 defaults: {},
475 },
476 )
477β
478 expectTypeOf(
479 hydrate(queryClient, {}, { defaultOptions: { queries: {} } }),
480 ).toEqualTypeOf<void>()
481 })
482 })
483β
484 describe('defaultShouldDehydrateQuery', () => {
485 it('should take a query and return a boolean', () => {
486 expectTypeOf(defaultShouldDehydrateQuery).toEqualTypeOf<
487 (query: Query) => boolean
488 >()
489 })
490β
491 it('should be assignable to the shouldDehydrateQuery option', () => {
492 expectTypeOf(
493 dehydrate(queryClient, {
494 shouldDehydrateQuery: defaultShouldDehydrateQuery,
495 }),
496 ).toEqualTypeOf<DehydratedState>()
497 })
498β
499 it('should require the query', () => {
500 // @ts-expect-error the query is required
501 defaultShouldDehydrateQuery()
502β
503 expectTypeOf(
504 defaultShouldDehydrateQuery({} as Query),
505 ).toEqualTypeOf<boolean>()
506 })
507 })
508β
509 describe('defaultShouldDehydrateMutation', () => {
510 it('should take a mutation and return a boolean', () => {
511 expectTypeOf(defaultShouldDehydrateMutation).toEqualTypeOf<
512 (mutation: Mutation) => boolean
513 >()
514 })
515β
516 it('should be assignable to the shouldDehydrateMutation option', () => {
517 expectTypeOf(
518 dehydrate(queryClient, {
519 shouldDehydrateMutation: defaultShouldDehydrateMutation,
520 }),
521 ).toEqualTypeOf<DehydratedState>()
522 })
523β
524 it('should require the mutation', () => {
525 // @ts-expect-error the mutation is required
526 defaultShouldDehydrateMutation()
527β
528 expectTypeOf(
529 defaultShouldDehydrateMutation({} as Mutation),
530 ).toEqualTypeOf<boolean>()
531 })
532 })
533})