Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { QueryClient } from '../queryClient'
3import { MutationCache } from '../mutationCache'
4import type { MutationObserver } from '../mutationObserver'
5import type { Action, Mutation, MutationState } from '../mutation'
6import type {
7 DefaultError,
8 MutateFunction,
9 MutateOptions,
10 MutationMeta,
11 MutationOptions,
12} from '../types'
13β
14class CustomError extends Error {
15 override name = 'CustomError' as const
16}
17β
18describe('mutation', () => {
19 describe('MutationState', () => {
20 it('should keep every property writable', () => {
21 type State = MutationState<{ value: string }, CustomError, number, string>
22β
23 expectTypeOf<State>().toEqualTypeOf<{
24 -readonly [K in keyof State]: State[K]
25 }>()
26 })
27β
28 it('should declare every property as required', () => {
29 type State = MutationState<{ value: string }, CustomError, number, string>
30 type OptionalKeys = {
31 [K in keyof State]-?: {} extends Pick<State, K> ? K : never
32 }[keyof State]
33β
34 expectTypeOf<OptionalKeys>().toEqualTypeOf<never>()
35 })
36β
37 it('should declare exactly its nine properties', () => {
38 expectTypeOf<keyof MutationState>().toEqualTypeOf<
39 | 'context'
40 | 'data'
41 | 'error'
42 | 'failureCount'
43 | 'failureReason'
44 | 'isPaused'
45 | 'status'
46 | 'variables'
47 | 'submittedAt'
48 >()
49 })
50β
51 it('should default its type parameters', () => {
52 expectTypeOf<MutationState>().toEqualTypeOf<
53 MutationState<unknown, DefaultError, unknown, unknown>
54 >()
55 })
56β
57 describe('context', () => {
58 it('should be the on-mutate result or undefined', () => {
59 expectTypeOf<
60 MutationState<
61 { value: string },
62 CustomError,
63 number,
64 string
65 >['context']
66 >().toEqualTypeOf<string | undefined>()
67 })
68 })
69β
70 describe('data', () => {
71 it('should be the data or undefined', () => {
72 expectTypeOf<
73 MutationState<{ value: string }, CustomError, number, string>['data']
74 >().toEqualTypeOf<{ value: string } | undefined>()
75 })
76 })
77β
78 describe('error', () => {
79 it('should be the error or null', () => {
80 expectTypeOf<
81 MutationState<{ value: string }, CustomError, number, string>['error']
82 >().toEqualTypeOf<CustomError | null>()
83 })
84 })
85β
86 describe('failureCount', () => {
87 it('should be a number', () => {
88 expectTypeOf<
89 MutationState<
90 { value: string },
91 CustomError,
92 number,
93 string
94 >['failureCount']
95 >().toEqualTypeOf<number>()
96 })
97 })
98β
99 describe('failureReason', () => {
100 it('should be the error or null', () => {
101 expectTypeOf<
102 MutationState<
103 { value: string },
104 CustomError,
105 number,
106 string
107 >['failureReason']
108 >().toEqualTypeOf<CustomError | null>()
109 })
110 })
111β
112 describe('isPaused', () => {
113 it('should be a boolean', () => {
114 expectTypeOf<
115 MutationState<
116 { value: string },
117 CustomError,
118 number,
119 string
120 >['isPaused']
121 >().toEqualTypeOf<boolean>()
122 })
123 })
124β
125 describe('status', () => {
126 it('should spell out every mutation status', () => {
127 expectTypeOf<
128 MutationState<
129 { value: string },
130 CustomError,
131 number,
132 string
133 >['status']
134 >().toEqualTypeOf<'idle' | 'pending' | 'success' | 'error'>()
135 })
136β
137 it('should not widen beyond those literals on the default parameters', () => {
138 expectTypeOf<MutationState['status']>().toEqualTypeOf<
139 'idle' | 'pending' | 'success' | 'error'
140 >()
141 })
142 })
143β
144 describe('variables', () => {
145 it('should be the variables or undefined', () => {
146 expectTypeOf<
147 MutationState<
148 { value: string },
149 CustomError,
150 number,
151 string
152 >['variables']
153 >().toEqualTypeOf<number | undefined>()
154 })
155 })
156β
157 describe('submittedAt', () => {
158 it('should be a number', () => {
159 expectTypeOf<
160 MutationState<
161 { value: string },
162 CustomError,
163 number,
164 string
165 >['submittedAt']
166 >().toEqualTypeOf<number>()
167 })
168 })
169 })
170β
171 describe('Mutation', () => {
172 it('should declare exactly its public members', () => {
173 expectTypeOf<keyof Mutation>().toEqualTypeOf<
174 | 'state'
175 | 'options'
176 | 'mutationId'
177 | 'setOptions'
178 | 'meta'
179 | 'addObserver'
180 | 'removeObserver'
181 | 'continue'
182 | 'execute'
183 | 'gcTime'
184 | 'destroy'
185 >()
186 })
187β
188 it('should default its type parameters', () => {
189 expectTypeOf<Mutation>().toEqualTypeOf<
190 Mutation<unknown, DefaultError, unknown, unknown>
191 >()
192 })
193β
194 describe('state', () => {
195 it('should be the mutation state for the same type parameters', () => {
196 const mutation = new MutationCache().build<
197 { value: string },
198 CustomError,
199 number,
200 string
201 >(new QueryClient(), {
202 mutationFn: (variables: number) =>
203 Promise.resolve({ value: String(variables) }),
204 })
205β
206 expectTypeOf(mutation.state).toEqualTypeOf<
207 MutationState<{ value: string }, CustomError, number, string>
208 >()
209 })
210β
211 it('should stay writable', () => {
212 // `Pick` preserves the `readonly` modifier that an indexed access such
213 // as `Mutation<...>['state']` silently strips
214 type State = Pick<
215 Mutation<{ value: string }, CustomError, number, string>,
216 'state'
217 >
218β
219 expectTypeOf<State>().toEqualTypeOf<{
220 -readonly [K in keyof State]: State[K]
221 }>()
222 })
223 })
224β
225 describe('options', () => {
226 it('should be the mutation options for the same type parameters', () => {
227 const mutation = new MutationCache().build<
228 { value: string },
229 CustomError,
230 number,
231 string
232 >(new QueryClient(), {
233 mutationFn: (variables: number) =>
234 Promise.resolve({ value: String(variables) }),
235 })
236β
237 expectTypeOf(mutation.options).toEqualTypeOf<
238 MutationOptions<{ value: string }, CustomError, number, string>
239 >()
240 })
241β
242 it('should always be declared, despite being assigned lazily', () => {
243 type Keys = Mutation<{ value: string }, CustomError, number, string>
244 type OptionalKeys = {
245 [K in keyof Keys]-?: {} extends Pick<Keys, K> ? K : never
246 }[keyof Keys]
247β
248 expectTypeOf<
249 'options' extends OptionalKeys ? true : false
250 >().toEqualTypeOf<false>()
251 })
252 })
253β
254 describe('mutationId', () => {
255 it('should be a readonly number', () => {
256 type Id = Pick<Mutation, 'mutationId'>
257β
258 expectTypeOf<Id>().toEqualTypeOf<{ readonly mutationId: number }>()
259 expectTypeOf<Id>().not.toEqualTypeOf<{ mutationId: number }>()
260 })
261 })
262β
263 describe('setOptions', () => {
264 it('should take the mutation options and return nothing', () => {
265 const mutation = new MutationCache().build<
266 { value: string },
267 CustomError,
268 number,
269 string
270 >(new QueryClient(), {
271 mutationFn: (variables: number) =>
272 Promise.resolve({ value: String(variables) }),
273 })
274β
275 expectTypeOf(mutation.setOptions)
276 .parameter(0)
277 .toEqualTypeOf<
278 MutationOptions<{ value: string }, CustomError, number, string>
279 >()
280 expectTypeOf(mutation.setOptions).returns.toEqualTypeOf<void>()
281 expectTypeOf(mutation.setOptions).parameters.toEqualTypeOf<
282 [MutationOptions<{ value: string }, CustomError, number, string>]
283 >()
284 })
285 })
286β
287 describe('meta', () => {
288 it('should be the mutation meta or undefined', () => {
289 const mutation = new MutationCache().build<
290 { value: string },
291 CustomError,
292 number,
293 string
294 >(new QueryClient(), {
295 mutationFn: (variables: number) =>
296 Promise.resolve({ value: String(variables) }),
297 })
298β
299 expectTypeOf(mutation.meta).toEqualTypeOf<MutationMeta | undefined>()
300 })
301β
302 it('should be a getter without a setter', () => {
303 const mutation = new MutationCache().build<
304 { value: string },
305 CustomError,
306 number,
307 string
308 >(new QueryClient(), {
309 mutationFn: (variables: number) =>
310 Promise.resolve({ value: String(variables) }),
311 })
312β
313 // @ts-expect-error meta is a getter only
314 mutation.meta = {}
315β
316 expectTypeOf<Mutation['meta']>().toEqualTypeOf<
317 MutationMeta | undefined
318 >()
319 })
320 })
321β
322 describe('addObserver', () => {
323 it('should take a single observer and return nothing', () => {
324 const mutation = new MutationCache().build<
325 { value: string },
326 CustomError,
327 number,
328 string
329 >(new QueryClient(), {
330 mutationFn: (variables: number) =>
331 Promise.resolve({ value: String(variables) }),
332 })
333β
334 expectTypeOf(mutation.addObserver).returns.toEqualTypeOf<void>()
335 expectTypeOf(mutation.addObserver).parameters.toEqualTypeOf<
336 [MutationObserver<any, any, any, any>]
337 >()
338 })
339 })
340β
341 describe('removeObserver', () => {
342 it('should take a single observer and return nothing', () => {
343 const mutation = new MutationCache().build<
344 { value: string },
345 CustomError,
346 number,
347 string
348 >(new QueryClient(), {
349 mutationFn: (variables: number) =>
350 Promise.resolve({ value: String(variables) }),
351 })
352β
353 expectTypeOf(mutation.removeObserver).returns.toEqualTypeOf<void>()
354 expectTypeOf(mutation.removeObserver).parameters.toEqualTypeOf<
355 [MutationObserver<any, any, any, any>]
356 >()
357 })
358 })
359β
360 describe('continue', () => {
361 it('should take no arguments and resolve with an unknown value', () => {
362 const mutation = new MutationCache().build<
363 { value: string },
364 CustomError,
365 number,
366 string
367 >(new QueryClient(), {
368 mutationFn: (variables: number) =>
369 Promise.resolve({ value: String(variables) }),
370 })
371β
372 expectTypeOf(mutation.continue).parameters.toEqualTypeOf<[]>()
373 expectTypeOf(mutation.continue()).toEqualTypeOf<Promise<unknown>>()
374 })
375 })
376β
377 describe('execute', () => {
378 it('should take the variables and resolve with the data', () => {
379 const mutation = new MutationCache().build<
380 { value: string },
381 CustomError,
382 number,
383 string
384 >(new QueryClient(), {
385 mutationFn: (variables: number) =>
386 Promise.resolve({ value: String(variables) }),
387 })
388β
389 expectTypeOf(mutation.execute).parameters.toEqualTypeOf<[number]>()
390 expectTypeOf(mutation.execute(1)).toEqualTypeOf<
391 Promise<{ value: string }>
392 >()
393 })
394β
395 it('should reject variables of another type', () => {
396 const mutation = new MutationCache().build<
397 { value: string },
398 CustomError,
399 number,
400 string
401 >(new QueryClient(), {
402 mutationFn: (variables: number) =>
403 Promise.resolve({ value: String(variables) }),
404 })
405β
406 // @ts-expect-error the variables must match the mutation function
407 mutation.execute('1')
408β
409 expectTypeOf(mutation.execute).parameter(0).toEqualTypeOf<number>()
410 })
411 })
412β
413 describe('gcTime', () => {
414 it('should be a number inherited from Removable', () => {
415 const mutation = new MutationCache().build<
416 { value: string },
417 CustomError,
418 number,
419 string
420 >(new QueryClient(), {
421 mutationFn: (variables: number) =>
422 Promise.resolve({ value: String(variables) }),
423 })
424β
425 expectTypeOf(mutation.gcTime).toEqualTypeOf<number>()
426 })
427 })
428β
429 describe('destroy', () => {
430 it('should take no arguments and return nothing', () => {
431 const mutation = new MutationCache().build<
432 { value: string },
433 CustomError,
434 number,
435 string
436 >(new QueryClient(), {
437 mutationFn: (variables: number) =>
438 Promise.resolve({ value: String(variables) }),
439 })
440β
441 expectTypeOf(mutation.destroy).parameters.toEqualTypeOf<[]>()
442 expectTypeOf(mutation.destroy).returns.toEqualTypeOf<void>()
443 })
444 })
445 })
446β
447 describe('MutationOptions', () => {
448 type Options = MutationOptions<
449 { value: string },
450 CustomError,
451 number,
452 string
453 >
454β
455 describe('retry', () => {
456 it('should be an optional retry value taking the error', () => {
457 expectTypeOf<Options['retry']>().toEqualTypeOf<
458 | boolean
459 | number
460 | ((failureCount: number, error: CustomError) => boolean)
461 | undefined
462 >()
463 })
464 })
465β
466 describe('retryDelay', () => {
467 it('should be an optional delay taking the error', () => {
468 expectTypeOf<Options['retryDelay']>().toEqualTypeOf<
469 | number
470 | ((failureCount: number, error: CustomError) => number)
471 | undefined
472 >()
473 })
474 })
475β
476 describe('networkMode', () => {
477 it('should spell out every network mode', () => {
478 expectTypeOf<Options['networkMode']>().toEqualTypeOf<
479 'online' | 'always' | 'offlineFirst' | undefined
480 >()
481 })
482 })
483β
484 describe('gcTime', () => {
485 it('should be an optional number', () => {
486 expectTypeOf<Options['gcTime']>().toEqualTypeOf<number | undefined>()
487 })
488 })
489β
490 describe('scope', () => {
491 it('should be an optional scope carrying a string id', () => {
492 expectTypeOf<Options['scope']>().toEqualTypeOf<
493 { id: string } | undefined
494 >()
495 })
496 })
497 })
498β
499 describe('Action', () => {
500 type MutationAction = Action<{ value: string }, CustomError, number, string>
501β
502 it('should union exactly its six action type literals', () => {
503 expectTypeOf<MutationAction['type']>().toEqualTypeOf<
504 'continue' | 'error' | 'failed' | 'pending' | 'pause' | 'success'
505 >()
506 })
507β
508 describe('failed', () => {
509 it('should carry the failure count and the nullable error', () => {
510 type Failed = Extract<MutationAction, { type: 'failed' }>
511β
512 expectTypeOf<Failed>().toEqualTypeOf<{
513 type: 'failed'
514 failureCount: number
515 error: CustomError | null
516 }>()
517 })
518 })
519β
520 describe('pause', () => {
521 it('should carry nothing but its type', () => {
522 expectTypeOf<
523 Extract<MutationAction, { type: 'pause' }>
524 >().toEqualTypeOf<{ type: 'pause' }>()
525 })
526 })
527β
528 describe('continue', () => {
529 it('should carry nothing but its type', () => {
530 expectTypeOf<
531 Extract<MutationAction, { type: 'continue' }>
532 >().toEqualTypeOf<{ type: 'continue' }>()
533 })
534 })
535β
536 describe('success', () => {
537 it('should carry the data', () => {
538 expectTypeOf<
539 Extract<MutationAction, { type: 'success' }>
540 >().toEqualTypeOf<{ type: 'success'; data: { value: string } }>()
541 })
542 })
543β
544 describe('error', () => {
545 it('should carry the error', () => {
546 expectTypeOf<
547 Extract<MutationAction, { type: 'error' }>
548 >().toEqualTypeOf<{ type: 'error'; error: CustomError }>()
549 })
550 })
551β
552 describe('pending', () => {
553 it('should carry the paused flag, the variables and the on-mutate result', () => {
554 expectTypeOf<
555 Extract<MutationAction, { type: 'pending' }>
556 >().toEqualTypeOf<{
557 type: 'pending'
558 isPaused: boolean
559 variables?: number
560 context?: string
561 }>()
562 })
563 })
564 })
565β
566 describe('MutateFunction', () => {
567 it('should resolve with the data', () => {
568 const mutate = {} as MutateFunction<string, CustomError, number, unknown>
569β
570 expectTypeOf(mutate(1)).toEqualTypeOf<Promise<string>>()
571 })
572β
573 it('should default its type parameters', () => {
574 expectTypeOf<MutateFunction>().toEqualTypeOf<
575 MutateFunction<unknown, DefaultError, void, unknown>
576 >()
577 })
578β
579 it('should handle void variables', () => {
580 const mutate = {} as MutateFunction
581β
582 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
583 undefined | void
584 >()
585β
586 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
587 undefined | MutateOptions<unknown, DefaultError, void, unknown>
588 >()
589β
590 mutate() // can be called with no arguments
591 mutate(undefined, {
592 onError: (e) => {
593 expectTypeOf(e).toEqualTypeOf<DefaultError>()
594 },
595 })
596 })
597β
598 it('should handle optional undefinable variables', () => {
599 const mutate = {} as MutateFunction<
600 unknown,
601 DefaultError,
602 number | undefined,
603 unknown
604 >
605β
606 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
607 number | undefined
608 >()
609β
610 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
611 | undefined
612 | MutateOptions<unknown, DefaultError, number | undefined, unknown>
613 >()
614β
615 mutate() // can be called with no arguments
616 mutate(undefined, {
617 onError: (e) => {
618 expectTypeOf(e).toEqualTypeOf<DefaultError>()
619 },
620 })
621 })
622β
623 it('should handle unknown variables', () => {
624 const mutate = {} as MutateFunction<
625 unknown,
626 DefaultError,
627 unknown,
628 unknown
629 >
630β
631 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<unknown>()
632β
633 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
634 undefined | MutateOptions<unknown, DefaultError, unknown, unknown>
635 >()
636β
637 mutate() // can be called with no arguments
638 })
639β
640 it('should handle any variables', () => {
641 const mutate = {} as MutateFunction<unknown, DefaultError, any, unknown>
642β
643 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<any>()
644β
645 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
646 undefined | MutateOptions<unknown, DefaultError, any, unknown>
647 >()
648β
649 mutate() // can be called with no arguments
650 })
651β
652 it('should handle void union variables', () => {
653 const mutate = {} as MutateFunction<
654 unknown,
655 DefaultError,
656 void | string,
657 unknown
658 >
659β
660 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
661 void | string | undefined
662 >()
663β
664 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
665 undefined | MutateOptions<unknown, DefaultError, void | string, unknown>
666 >()
667β
668 mutate() // can be called with no arguments
669 })
670β
671 it('should handle required non-undefinable variables', () => {
672 const mutate = {} as MutateFunction<
673 unknown,
674 DefaultError,
675 number,
676 unknown
677 >
678β
679 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<number>()
680β
681 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
682 undefined | MutateOptions<unknown, DefaultError, number, unknown>
683 >()
684β
685 // @ts-expect-error --- required variables
686 mutate()
687 mutate(123, {
688 onError: (e) => {
689 expectTypeOf(e).toEqualTypeOf<DefaultError>()
690 },
691 })
692 })
693β
694 describe('MutateFunctionRest', () => {
695 // this is common pattern used internal so we need make sure it still works
696β
697 it('should handle void variables', () => {
698 const mutate = {} as (...options: Parameters<MutateFunction>) => void
699β
700 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
701 undefined | void
702 >()
703β
704 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
705 undefined | MutateOptions<unknown, DefaultError, void, unknown>
706 >()
707β
708 mutate() // can be called with no arguments
709 mutate(undefined, {
710 onError: (e) => {
711 expectTypeOf(e).toEqualTypeOf<DefaultError>()
712 },
713 })
714 })
715β
716 it('should handle optional undefinable variables', () => {
717 const mutate = {} as (
718 ...options: Parameters<
719 MutateFunction<unknown, DefaultError, number | undefined, unknown>
720 >
721 ) => void
722β
723 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
724 number | undefined
725 >()
726β
727 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
728 | undefined
729 | MutateOptions<unknown, DefaultError, number | undefined, unknown>
730 >()
731β
732 mutate() // can be called with no arguments
733 mutate(undefined, {
734 onError: (e) => {
735 expectTypeOf(e).toEqualTypeOf<DefaultError>()
736 },
737 })
738 })
739β
740 it('should handle unknown variables', () => {
741 const mutate = {} as (
742 ...options: Parameters<
743 MutateFunction<unknown, DefaultError, unknown, unknown>
744 >
745 ) => void
746β
747 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<unknown>()
748β
749 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
750 undefined | MutateOptions<unknown, DefaultError, unknown, unknown>
751 >()
752β
753 mutate() // can be called with no arguments
754 })
755β
756 it('should handle any variables', () => {
757 const mutate = {} as (
758 ...options: Parameters<
759 MutateFunction<unknown, DefaultError, any, unknown>
760 >
761 ) => void
762β
763 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<any>()
764β
765 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
766 undefined | MutateOptions<unknown, DefaultError, any, unknown>
767 >()
768β
769 mutate() // can be called with no arguments
770 })
771β
772 it('should handle void union variables', () => {
773 const mutate = {} as (
774 ...options: Parameters<
775 MutateFunction<unknown, DefaultError, void | string, unknown>
776 >
777 ) => void
778β
779 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<
780 void | string | undefined
781 >()
782β
783 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
784 | undefined
785 | MutateOptions<unknown, DefaultError, void | string, unknown>
786 >()
787β
788 mutate() // can be called with no arguments
789 })
790β
791 it('should handle required non-undefinable variables', () => {
792 const mutate = {} as (
793 ...options: Parameters<
794 MutateFunction<unknown, DefaultError, number, unknown>
795 >
796 ) => void
797β
798 expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<number>()
799β
800 expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf<
801 undefined | MutateOptions<unknown, DefaultError, number, unknown>
802 >()
803β
804 // @ts-expect-error --- required variables
805 mutate()
806 mutate(123, {
807 onError: (e) => {
808 expectTypeOf(e).toEqualTypeOf<DefaultError>()
809 },
810 })
811 })
812 })
813 })
814})