Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

mutationObserver.test-d.tsx

1,015 lines30.5 KB

1import { afterEach, beforeEach, describe, expectTypeOf, it } from 'vitest'
2import { MutationObserver, QueryClient } from '..'
3import type {
4 DefaultError,
5 MutateFunction,
6 MutateOptions,
7 MutationFunctionContext,
8 MutationKey,
9 MutationMeta,
10 MutationObserverBaseResult,
11 MutationObserverErrorResult,
12 MutationObserverIdleResult,
13 MutationObserverLoadingResult,
14 MutationObserverOptions,
15 MutationObserverResult,
16 MutationObserverSuccessResult,
17 MutationState,
18} from '..'
19import type { Action } from '../mutation'
20​
21class CustomError extends Error {
22 override name = 'CustomError' as const
24​
25type Variables = { title: string }
26type Data = { id: number }
27type OnMutateResult = { previous: string }
28​
29describe('mutationObserver', () => {
30 let queryClient: QueryClient
31​
32 beforeEach(() => {
33 queryClient = new QueryClient()
34 queryClient.mount()
35 })
36​
37 afterEach(() => {
38 queryClient.clear()
39 })
40​
41 describe('MutationObserver', () => {
42 it('should only expose its documented members', () => {
43 expectTypeOf<keyof MutationObserver>().toEqualTypeOf<
44 | 'subscribe'
45 | 'hasListeners'
46 | 'options'
47 | 'setOptions'
48 | 'onMutationUpdate'
49 | 'getCurrentResult'
50 | 'reset'
51 | 'mutate'
52 >()
53 })
54 })
55​
56 describe('MutationObserverOptions', () => {
57 it('should keep every option writable', () => {
58 type Options = MutationObserverOptions<
59 Data,
60 CustomError,
61 Variables,
62 OnMutateResult
63 >
64​
65 expectTypeOf<Options>().toEqualTypeOf<{
66 -readonly [K in keyof Options]: Options[K]
67 }>()
68 })
69​
70 describe('mutationFn', () => {
71 it('should take the variables and return a promise of the data', () => {
72 expectTypeOf<
73 NonNullable<
74 MutationObserverOptions<Data, CustomError, Variables>['mutationFn']
75 >
76 >().toEqualTypeOf<
77 (
78 variables: Variables,
79 context: MutationFunctionContext,
80 ) => Promise<Data>
81 >()
82 })
83​
84 it('should reject a synchronously returned data', () => {
85 new MutationObserver<Data, CustomError, Variables>(queryClient, {
86 // @ts-expect-error the mutationFn must return a promise
87 mutationFn: (variables) => ({ id: variables.title.length }),
88 })
89​
90 const observer = new MutationObserver<Data, CustomError, Variables>(
91 queryClient,
92 {
93 mutationFn: (variables) =>
94 Promise.resolve({ id: variables.title.length }),
95 },
96 )
97​
98 expectTypeOf(observer).toEqualTypeOf<
99 MutationObserver<Data, CustomError, Variables, unknown>
100 >()
101 })
102 })
103​
104 describe('mutationKey', () => {
105 it('should be an optional mutation key', () => {
106 expectTypeOf<
107 MutationObserverOptions<Data, CustomError, Variables>['mutationKey']
108 >().toEqualTypeOf<ReadonlyArray<unknown> | undefined>()
109 })
110 })
111​
112 describe('onMutate', () => {
113 it('should take the variables and return the on-mutate result', () => {
114 expectTypeOf<
115 NonNullable<
116 MutationObserverOptions<
117 Data,
118 CustomError,
119 Variables,
120 OnMutateResult
121 >['onMutate']
122 >
123 >().toEqualTypeOf<
124 (
125 variables: Variables,
126 context: MutationFunctionContext,
127 ) => Promise<OnMutateResult> | OnMutateResult
128 >()
129 })
130 })
131​
132 describe('onSuccess', () => {
133 it('should take the on-mutate result without undefined', () => {
134 expectTypeOf<
135 NonNullable<
136 MutationObserverOptions<
137 Data,
138 CustomError,
139 Variables,
140 OnMutateResult
141 >['onSuccess']
142 >
143 >()
144 .parameter(2)
145 .toEqualTypeOf<OnMutateResult>()
146 })
147​
148 it('should take the data, the variables and the mutation function context', () => {
149 expectTypeOf<
150 NonNullable<
151 MutationObserverOptions<
152 Data,
153 CustomError,
154 Variables,
155 OnMutateResult
156 >['onSuccess']
157 >
158 >().toEqualTypeOf<
159 (
160 data: Data,
161 variables: Variables,
162 onMutateResult: OnMutateResult,
163 context: MutationFunctionContext,
164 ) => Promise<unknown> | unknown
165 >()
166 })
167 })
168​
169 describe('onError', () => {
170 it('should take the on-mutate result as possibly undefined', () => {
171 expectTypeOf<
172 NonNullable<
173 MutationObserverOptions<
174 Data,
175 CustomError,
176 Variables,
177 OnMutateResult
178 >['onError']
179 >
180 >()
181 .parameter(2)
182 .toEqualTypeOf<OnMutateResult | undefined>()
183 })
184​
185 it('should take the error without null and return unknown', () => {
186 type OnError = NonNullable<
187 MutationObserverOptions<
188 Data,
189 CustomError,
190 Variables,
191 OnMutateResult
192 >['onError']
193 >
194​
195 expectTypeOf<OnError>().parameter(0).toEqualTypeOf<CustomError>()
196 expectTypeOf<OnError>().returns.toEqualTypeOf<unknown>()
197 })
198 })
199​
200 describe('onSettled', () => {
201 it('should take the data and the error as nullable and return unknown', () => {
202 type OnSettled = NonNullable<
203 MutationObserverOptions<
204 Data,
205 CustomError,
206 Variables,
207 OnMutateResult
208 >['onSettled']
209 >
210​
211 expectTypeOf<OnSettled>().parameter(0).toEqualTypeOf<Data | undefined>()
212 expectTypeOf<OnSettled>()
213 .parameter(1)
214 .toEqualTypeOf<CustomError | null>()
215 expectTypeOf<OnSettled>().returns.toEqualTypeOf<unknown>()
216 })
217​
218 it('should take the on-mutate result as possibly undefined', () => {
219 expectTypeOf<
220 NonNullable<
221 MutationObserverOptions<
222 Data,
223 CustomError,
224 Variables,
225 OnMutateResult
226 >['onSettled']
227 >
228 >()
229 .parameter(3)
230 .toEqualTypeOf<OnMutateResult | undefined>()
231 })
232 })
233​
234 describe('throwOnError', () => {
235 it('should be a boolean or a predicate taking the error', () => {
236 expectTypeOf<
237 MutationObserverOptions<Data, CustomError, Variables>['throwOnError']
238 >().toEqualTypeOf<
239 boolean | ((error: CustomError) => boolean) | undefined
240 >()
241 })
242 })
243​
244 describe('meta', () => {
245 it('should be optional mutation meta', () => {
246 expectTypeOf<
247 MutationObserverOptions<Data, CustomError, Variables>['meta']
248 >().toEqualTypeOf<MutationMeta | undefined>()
249 })
250 })
251 })
252​
253 describe('constructor', () => {
254 it('should default its type parameters', () => {
255 const observer = new MutationObserver(queryClient, {})
256​
257 expectTypeOf(observer).toEqualTypeOf<
258 MutationObserver<unknown, DefaultError, void, unknown>
259 >()
260 })
261​
262 it('should take the query client as its first argument', () => {
263 expectTypeOf<
264 ConstructorParameters<
265 typeof MutationObserver<Data, CustomError, Variables, OnMutateResult>
266 >[0]
267 >().toEqualTypeOf<QueryClient>()
268 })
269​
270 it('should infer the on-mutate result from onMutate', () => {
271 const observer = new MutationObserver(queryClient, {
272 mutationFn: (variables: Variables) =>
273 Promise.resolve({ id: variables.title.length }),
274 onMutate: () => ({ previous: 'value' }),
275 })
276​
277 expectTypeOf(observer).toEqualTypeOf<
278 MutationObserver<Data, DefaultError, Variables, OnMutateResult>
279 >()
280 })
281​
282 it('should flow the on-mutate result into the settled callbacks', () => {
283 new MutationObserver(queryClient, {
284 mutationFn: (variables: Variables) =>
285 Promise.resolve({ id: variables.title.length }),
286 onMutate: () => ({ previous: 'value' }),
287 onSuccess: (data, variables, onMutateResult) => {
288 expectTypeOf(data).toEqualTypeOf<Data>()
289 expectTypeOf(variables).toEqualTypeOf<Variables>()
290 expectTypeOf(onMutateResult).toEqualTypeOf<OnMutateResult>()
291 },
292 onError: (error, variables, onMutateResult) => {
293 expectTypeOf(error).toEqualTypeOf<DefaultError>()
294 expectTypeOf(variables).toEqualTypeOf<Variables>()
295 expectTypeOf(onMutateResult).toEqualTypeOf<
296 OnMutateResult | undefined
297 >()
298 },
299 onSettled: (data, error, variables, onMutateResult) => {
300 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
301 expectTypeOf(error).toEqualTypeOf<DefaultError | null>()
302 expectTypeOf(variables).toEqualTypeOf<Variables>()
303 expectTypeOf(onMutateResult).toEqualTypeOf<
304 OnMutateResult | undefined
305 >()
306 },
307 })
308 })
309​
310 it('should infer the on-mutate result as undefined when onMutate returns it explicitly', () => {
311 new MutationObserver(queryClient, {
312 mutationFn: (variables: number) => Promise.resolve(String(variables)),
313 onMutate: () => {
314 return undefined
315 },
316 onSuccess: (data, variables, onMutateResult) => {
317 expectTypeOf(data).toEqualTypeOf<string>()
318 expectTypeOf(variables).toEqualTypeOf<number>()
319 expectTypeOf(onMutateResult).toEqualTypeOf<undefined>()
320 },
321 onError: (_error, _variables, onMutateResult) => {
322 expectTypeOf(onMutateResult).toEqualTypeOf<undefined>()
323 },
324 onSettled: (_data, _error, _variables, onMutateResult) => {
325 expectTypeOf(onMutateResult).toEqualTypeOf<undefined>()
326 },
327 })
328 })
329​
330 it('should infer the on-mutate result as unknown when onMutate is left out', () => {
331 new MutationObserver(queryClient, {
332 mutationFn: (variables: number) => Promise.resolve(String(variables)),
333 onSuccess: (_data, _variables, onMutateResult) => {
334 expectTypeOf(onMutateResult).toEqualTypeOf<unknown>()
335 },
336 onError: (_error, _variables, onMutateResult) => {
337 expectTypeOf(onMutateResult).toEqualTypeOf<unknown>()
338 },
339 onSettled: (_data, _error, _variables, onMutateResult) => {
340 expectTypeOf(onMutateResult).toEqualTypeOf<unknown>()
341 },
342 })
343 })
344​
345 it('should keep undefined in the on-mutate result union when onMutate may return it', () => {
346 new MutationObserver(queryClient, {
347 mutationFn: (variables: number) => Promise.resolve(String(variables)),
348 onMutate: () => {
349 return Math.random() > 0.5 ? { foo: 'bar' } : undefined
350 },
351 onSuccess: (_data, _variables, onMutateResult) => {
352 expectTypeOf(onMutateResult).toEqualTypeOf<
353 { foo: string } | undefined
354 >()
355 },
356 onError: (_error, _variables, onMutateResult) => {
357 expectTypeOf(onMutateResult).toEqualTypeOf<
358 { foo: string } | undefined
359 >()
360 },
361 onSettled: (_data, _error, _variables, onMutateResult) => {
362 expectTypeOf(onMutateResult).toEqualTypeOf<
363 { foo: string } | undefined
364 >()
365 },
366 })
367 })
368​
369 it('should require the options argument', () => {
370 expectTypeOf<
371 ConstructorParameters<
372 typeof MutationObserver<Data, CustomError, Variables, OnMutateResult>
373 >['length']
374 >().toEqualTypeOf<2>()
375 })
376 })
377​
378 describe('options', () => {
379 it('should expose the observed types through the options it holds', () => {
380 const observer = new MutationObserver<
381 Data,
382 CustomError,
383 Variables,
384 OnMutateResult
385 >(queryClient, {})
386​
387 expectTypeOf(observer.options).toEqualTypeOf<
388 MutationObserverOptions<Data, CustomError, Variables, OnMutateResult>
389 >()
390 })
391​
392 it('should stay writable', () => {
393 type Options = Pick<MutationObserver, 'options'>
394​
395 expectTypeOf<Options>().toEqualTypeOf<{
396 -readonly [K in keyof Options]: Options[K]
397 }>()
398 })
399 })
400​
401 describe('setOptions', () => {
402 it('should take the observer options', () => {
403 const observer = new MutationObserver<
404 Data,
405 CustomError,
406 Variables,
407 OnMutateResult
408 >(queryClient, {})
409​
410 expectTypeOf(observer.setOptions)
411 .parameter(0)
412 .toEqualTypeOf<
413 MutationObserverOptions<Data, CustomError, Variables, OnMutateResult>
414 >()
415 })
416​
417 it('should take exactly the options argument', () => {
418 expectTypeOf<
419 Parameters<MutationObserver['setOptions']>['length']
420 >().toEqualTypeOf<1>()
421 })
422​
423 it('should reject a mutationFn returning another data type', () => {
424 const observer = new MutationObserver<Data, CustomError, Variables>(
425 queryClient,
426 {},
427 )
428​
429 observer.setOptions({
430 // @ts-expect-error the mutationFn must resolve to the observed data type
431 mutationFn: () => Promise.resolve('nope'),
432 })
433​
434 expectTypeOf(
435 observer.setOptions({
436 mutationFn: (variables) =>
437 Promise.resolve({ id: variables.title.length }),
438 }),
439 ).toEqualTypeOf<void>()
440 })
441 })
442​
443 describe('getCurrentResult', () => {
444 it('should return the observer result', () => {
445 const observer = new MutationObserver<
446 Data,
447 CustomError,
448 Variables,
449 OnMutateResult
450 >(queryClient, {})
451​
452 expectTypeOf(observer.getCurrentResult()).toEqualTypeOf<
453 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>
454 >()
455 })
456​
457 it('should take no arguments', () => {
458 expectTypeOf<
459 Parameters<MutationObserver['getCurrentResult']>
460 >().toEqualTypeOf<[]>()
461 })
462 })
463​
464 describe('mutate', () => {
465 it('should take the variables and return a promise of the data', () => {
466 const observer = new MutationObserver<
467 Data,
468 CustomError,
469 Variables,
470 OnMutateResult
471 >(queryClient, {})
472​
473 expectTypeOf(observer.mutate).parameter(0).toEqualTypeOf<Variables>()
474 expectTypeOf(observer.mutate).returns.toEqualTypeOf<Promise<Data>>()
475 })
476​
477 it('should take the variables and the optional mutate options', () => {
478 const observer = new MutationObserver<
479 Data,
480 CustomError,
481 Variables,
482 OnMutateResult
483 >(queryClient, {})
484​
485 expectTypeOf<Parameters<typeof observer.mutate>>().toEqualTypeOf<
486 [
487 variables: Variables,
488 options?: MutateOptions<Data, CustomError, Variables, OnMutateResult>,
489 ]
490 >()
491 })
492​
493 it('should let void variables be omitted at the call site', () => {
494 const observer = new MutationObserver(queryClient, {})
495​
496 expectTypeOf(observer.mutate()).toEqualTypeOf<Promise<unknown>>()
497 expectTypeOf(observer.mutate(undefined)).toEqualTypeOf<Promise<unknown>>()
498​
499 const result = observer.getCurrentResult()
500​
501 expectTypeOf(result.mutate()).toEqualTypeOf<Promise<unknown>>()
502 })
503​
504 it('should take optional per-call mutate options', () => {
505 const observer = new MutationObserver<
506 Data,
507 CustomError,
508 Variables,
509 OnMutateResult
510 >(queryClient, {})
511​
512 expectTypeOf(observer.mutate)
513 .parameter(1)
514 .toEqualTypeOf<
515 | MutateOptions<Data, CustomError, Variables, OnMutateResult>
516 | undefined
517 >()
518 })
519​
520 it('should type the per-call callbacks it is given', () => {
521 const observer = new MutationObserver<
522 Data,
523 CustomError,
524 Variables,
525 OnMutateResult
526 >(queryClient, {})
527​
528 observer.mutate(
529 { title: 'title' },
530 {
531 onSuccess: (data, variables, onMutateResult, context) => {
532 expectTypeOf(data).toEqualTypeOf<Data>()
533 expectTypeOf(variables).toEqualTypeOf<Variables>()
534 expectTypeOf(onMutateResult).toEqualTypeOf<
535 OnMutateResult | undefined
536 >()
537 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
538 },
539 onError: (error, _variables, onMutateResult) => {
540 expectTypeOf(error).toEqualTypeOf<CustomError>()
541 expectTypeOf(onMutateResult).toEqualTypeOf<
542 OnMutateResult | undefined
543 >()
544 },
545 onSettled: (data, error, _variables, onMutateResult) => {
546 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
547 expectTypeOf(error).toEqualTypeOf<CustomError | null>()
548 expectTypeOf(onMutateResult).toEqualTypeOf<
549 OnMutateResult | undefined
550 >()
551 },
552 },
553 )
554 })
555​
556 it('should reject variables of another type', () => {
557 const observer = new MutationObserver<
558 Data,
559 CustomError,
560 Variables,
561 OnMutateResult
562 >(queryClient, {})
563​
564 // @ts-expect-error the variables must match the observed variables type
565 observer.mutate({ title: 42 })
566​
567 expectTypeOf(observer.mutate({ title: 'title' })).toEqualTypeOf<
568 Promise<Data>
569 >()
570 })
571 })
572​
573 describe('reset', () => {
574 it('should take no arguments and return nothing', () => {
575 expectTypeOf<Parameters<MutationObserver['reset']>>().toEqualTypeOf<[]>()
576 expectTypeOf<
577 ReturnType<MutationObserver['reset']>
578 >().toEqualTypeOf<void>()
579 })
580 })
581​
582 describe('onMutationUpdate', () => {
583 it('should take the action of the mutation it observes', () => {
584 const observer = new MutationObserver<
585 Data,
586 CustomError,
587 Variables,
588 OnMutateResult
589 >(queryClient, {})
590​
591 expectTypeOf(observer.onMutationUpdate)
592 .parameter(0)
593 .toEqualTypeOf<Action<Data, CustomError, Variables, OnMutateResult>>()
594 })
595​
596 it('should return nothing', () => {
597 expectTypeOf<
598 ReturnType<MutationObserver['onMutationUpdate']>
599 >().toEqualTypeOf<void>()
600 })
601 })
602​
603 describe('subscribe', () => {
604 it('should take a listener receiving the observer result', () => {
605 const observer = new MutationObserver<
606 Data,
607 CustomError,
608 Variables,
609 OnMutateResult
610 >(queryClient, {})
611​
612 expectTypeOf(observer.subscribe)
613 .parameter(0)
614 .parameter(0)
615 .toEqualTypeOf<
616 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>
617 >()
618 })
619​
620 it('should take a listener returning nothing', () => {
621 expectTypeOf<MutationObserver['subscribe']>()
622 .parameter(0)
623 .returns.toEqualTypeOf<void>()
624 })
625​
626 it('should return a function that unsubscribes', () => {
627 expectTypeOf<ReturnType<MutationObserver['subscribe']>>().toEqualTypeOf<
628 () => void
629 >()
630 })
631 })
632​
633 describe('hasListeners', () => {
634 it('should return whether the observer is subscribed to', () => {
635 const observer = new MutationObserver(queryClient, {})
636​
637 expectTypeOf(observer.hasListeners()).toEqualTypeOf<boolean>()
638 })
639 })
640​
641 describe('MutationObserverResult', () => {
642 describe('MutationObserverBaseResult', () => {
643 it('should keep every property writable', () => {
644 type Base = MutationObserverBaseResult<
645 Data,
646 CustomError,
647 Variables,
648 OnMutateResult
649 >
650​
651 expectTypeOf<Base>().toEqualTypeOf<{
652 -readonly [K in keyof Base]: Base[K]
653 }>()
654 })
655​
656 describe('data', () => {
657 it('should be the data or undefined', () => {
658 expectTypeOf<
659 MutationObserverBaseResult<
660 Data,
661 CustomError,
662 Variables,
663 OnMutateResult
664 >['data']
665 >().toEqualTypeOf<Data | undefined>()
666 })
667 })
668​
669 describe('error', () => {
670 it('should be the error or null', () => {
671 expectTypeOf<
672 MutationObserverBaseResult<
673 Data,
674 CustomError,
675 Variables,
676 OnMutateResult
677 >['error']
678 >().toEqualTypeOf<CustomError | null>()
679 })
680 })
681​
682 describe('variables', () => {
683 it('should be the variables or undefined', () => {
684 expectTypeOf<
685 MutationObserverBaseResult<
686 Data,
687 CustomError,
688 Variables,
689 OnMutateResult
690 >['variables']
691 >().toEqualTypeOf<Variables | undefined>()
692 })
693 })
694​
695 describe('MutationState', () => {
696 it('should inherit the members it does not redeclare', () => {
697 type Inherited =
698 | 'context'
699 | 'failureCount'
700 | 'failureReason'
701 | 'isPaused'
702 | 'submittedAt'
703​
704 expectTypeOf<
705 Pick<
706 MutationObserverBaseResult<
707 Data,
708 CustomError,
709 Variables,
710 OnMutateResult
711 >,
712 Inherited
713 >
714 >().toEqualTypeOf<
715 Pick<
716 MutationState<Data, CustomError, Variables, OnMutateResult>,
717 Inherited
718 >
719 >()
720 })
721 })
722​
723 describe('status', () => {
724 it('should be the mutation status', () => {
725 expectTypeOf<
726 MutationObserverBaseResult<Data, CustomError>['status']
727 >().toEqualTypeOf<'idle' | 'pending' | 'success' | 'error'>()
728 })
729 })
730​
731 describe('isIdle', () => {
732 it('should be a boolean on the base result', () => {
733 expectTypeOf<
734 MutationObserverBaseResult<Data, CustomError>['isIdle']
735 >().toEqualTypeOf<boolean>()
736 })
737 })
738​
739 describe('isPending', () => {
740 it('should be a boolean on the base result', () => {
741 expectTypeOf<
742 MutationObserverBaseResult<Data, CustomError>['isPending']
743 >().toEqualTypeOf<boolean>()
744 })
745 })
746​
747 describe('isError', () => {
748 it('should be a boolean on the base result', () => {
749 expectTypeOf<
750 MutationObserverBaseResult<Data, CustomError>['isError']
751 >().toEqualTypeOf<boolean>()
752 })
753 })
754​
755 describe('isSuccess', () => {
756 it('should be a boolean on the base result', () => {
757 expectTypeOf<
758 MutationObserverBaseResult<Data, CustomError>['isSuccess']
759 >().toEqualTypeOf<boolean>()
760 })
761 })
762​
763 describe('reset', () => {
764 it('should take no arguments and return nothing', () => {
765 expectTypeOf<
766 MutationObserverBaseResult<Data, CustomError>['reset']
767 >().toEqualTypeOf<() => void>()
768 })
769 })
770​
771 describe('mutate', () => {
772 it('should be the mutate function', () => {
773 expectTypeOf<
774 MutationObserverBaseResult<
775 Data,
776 CustomError,
777 Variables,
778 OnMutateResult
779 >['mutate']
780 >().toEqualTypeOf<
781 MutateFunction<Data, CustomError, Variables, OnMutateResult>
782 >()
783 })
784 })
785 })
786​
787 describe('MutationObserverIdleResult', () => {
788 it('should narrow every derived flag and the data', () => {
789 type Idle = Extract<
790 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>,
791 { status: 'idle' }
792 >
793​
794 expectTypeOf<Idle['data']>().toEqualTypeOf<undefined>()
795 expectTypeOf<Idle['error']>().toEqualTypeOf<null>()
796 expectTypeOf<Idle['variables']>().toEqualTypeOf<undefined>()
797 expectTypeOf<Idle['isIdle']>().toEqualTypeOf<true>()
798 expectTypeOf<Idle['isPending']>().toEqualTypeOf<false>()
799 expectTypeOf<Idle['isError']>().toEqualTypeOf<false>()
800 expectTypeOf<Idle['isSuccess']>().toEqualTypeOf<false>()
801 })
802​
803 it('should be reachable through the isIdle flag', () => {
804 type Idle = Extract<
805 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>,
806 { isIdle: true }
807 >
808​
809 expectTypeOf<Idle['status']>().toEqualTypeOf<'idle'>()
810 })
811​
812 it('should declare exactly the state, base and narrowed keys', () => {
813 expectTypeOf<keyof MutationObserverIdleResult>().toEqualTypeOf<
814 keyof MutationObserverBaseResult
815 >()
816 })
817 })
818​
819 describe('MutationObserverLoadingResult', () => {
820 it('should narrow every derived flag and the data', () => {
821 type Loading = Extract<
822 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>,
823 { status: 'pending' }
824 >
825​
826 expectTypeOf<Loading['data']>().toEqualTypeOf<undefined>()
827 expectTypeOf<Loading['error']>().toEqualTypeOf<null>()
828 expectTypeOf<Loading['variables']>().toEqualTypeOf<Variables>()
829 expectTypeOf<Loading['isIdle']>().toEqualTypeOf<false>()
830 expectTypeOf<Loading['isPending']>().toEqualTypeOf<true>()
831 expectTypeOf<Loading['isError']>().toEqualTypeOf<false>()
832 expectTypeOf<Loading['isSuccess']>().toEqualTypeOf<false>()
833 })
834​
835 it('should be reachable through the isPending flag', () => {
836 type Loading = Extract<
837 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>,
838 { isPending: true }
839 >
840​
841 expectTypeOf<Loading['status']>().toEqualTypeOf<'pending'>()
842 })
843​
844 it('should declare exactly the state, base and narrowed keys', () => {
845 expectTypeOf<keyof MutationObserverLoadingResult>().toEqualTypeOf<
846 keyof MutationObserverBaseResult
847 >()
848 })
849 })
850​
851 describe('MutationObserverErrorResult', () => {
852 it('should narrow every derived flag and the error', () => {
853 type ErrorResult = Extract<
854 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>,
855 { status: 'error' }
856 >
857​
858 expectTypeOf<ErrorResult['data']>().toEqualTypeOf<undefined>()
859 expectTypeOf<ErrorResult['error']>().toEqualTypeOf<CustomError>()
860 expectTypeOf<ErrorResult['variables']>().toEqualTypeOf<Variables>()
861 expectTypeOf<ErrorResult['isIdle']>().toEqualTypeOf<false>()
862 expectTypeOf<ErrorResult['isPending']>().toEqualTypeOf<false>()
863 expectTypeOf<ErrorResult['isError']>().toEqualTypeOf<true>()
864 expectTypeOf<ErrorResult['isSuccess']>().toEqualTypeOf<false>()
865 })
866​
867 it('should be reachable through the isError flag', () => {
868 type ErrorResult = Extract<
869 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>,
870 { isError: true }
871 >
872​
873 expectTypeOf<ErrorResult['status']>().toEqualTypeOf<'error'>()
874 })
875​
876 it('should declare exactly the state, base and narrowed keys', () => {
877 expectTypeOf<keyof MutationObserverErrorResult>().toEqualTypeOf<
878 keyof MutationObserverBaseResult
879 >()
880 })
881 })
882​
883 describe('MutationObserverSuccessResult', () => {
884 it('should narrow every derived flag and the data', () => {
885 type Success = Extract<
886 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>,
887 { status: 'success' }
888 >
889​
890 expectTypeOf<Success['data']>().toEqualTypeOf<Data>()
891 expectTypeOf<Success['error']>().toEqualTypeOf<null>()
892 expectTypeOf<Success['variables']>().toEqualTypeOf<Variables>()
893 expectTypeOf<Success['isIdle']>().toEqualTypeOf<false>()
894 expectTypeOf<Success['isPending']>().toEqualTypeOf<false>()
895 expectTypeOf<Success['isError']>().toEqualTypeOf<false>()
896 expectTypeOf<Success['isSuccess']>().toEqualTypeOf<true>()
897 })
898​
899 it('should be reachable through the isSuccess flag', () => {
900 type Success = Extract<
901 MutationObserverResult<Data, CustomError, Variables, OnMutateResult>,
902 { isSuccess: true }
903 >
904​
905 expectTypeOf<Success['status']>().toEqualTypeOf<'success'>()
906 })
907​
908 it('should declare exactly the state, base and narrowed keys', () => {
909 expectTypeOf<keyof MutationObserverSuccessResult>().toEqualTypeOf<
910 keyof MutationObserverBaseResult
911 >()
912 })
913 })
914​
915 it('should be made of exactly the four status branches', () => {
916 type Result = MutationObserverResult<
917 Data,
918 CustomError,
919 Variables,
920 OnMutateResult
921 >
922​
923 expectTypeOf<Result['status']>().toEqualTypeOf<
924 'idle' | 'pending' | 'error' | 'success'
925 >()
926 })
927 })
928​
929 describe('MutateOptions', () => {
930 it('should keep every per-call option writable', () => {
931 type Options = MutateOptions<Data, CustomError, Variables, OnMutateResult>
932​
933 expectTypeOf<Options>().toEqualTypeOf<{
934 -readonly [K in keyof Options]: Options[K]
935 }>()
936 })
937​
938 describe('onSuccess', () => {
939 it('should take the on-mutate result as possibly undefined and return nothing', () => {
940 expectTypeOf<
941 NonNullable<
942 MutateOptions<
943 Data,
944 CustomError,
945 Variables,
946 OnMutateResult
947 >['onSuccess']
948 >
949 >().toEqualTypeOf<
950 (
951 data: Data,
952 variables: Variables,
953 onMutateResult: OnMutateResult | undefined,
954 context: MutationFunctionContext,
955 ) => void
956 >()
957 })
958 })
959​
960 describe('onError', () => {
961 it('should take the error and return nothing', () => {
962 expectTypeOf<
963 NonNullable<
964 MutateOptions<
965 Data,
966 CustomError,
967 Variables,
968 OnMutateResult
969 >['onError']
970 >
971 >().toEqualTypeOf<
972 (
973 error: CustomError,
974 variables: Variables,
975 onMutateResult: OnMutateResult | undefined,
976 context: MutationFunctionContext,
977 ) => void
978 >()
979 })
980 })
981​
982 describe('onSettled', () => {
983 it('should take the nullable data and error and return nothing', () => {
984 expectTypeOf<
985 NonNullable<
986 MutateOptions<
987 Data,
988 CustomError,
989 Variables,
990 OnMutateResult
991 >['onSettled']
992 >
993 >().toEqualTypeOf<
994 (
995 data: Data | undefined,
996 error: CustomError | null,
997 variables: Variables,
998 onMutateResult: OnMutateResult | undefined,
999 context: MutationFunctionContext,
1000 ) => void
1001 >()
1002 })
1003 })
1004 })
1005​
1006 describe('MutationFunctionContext', () => {
1007 it('should carry the client, the meta and the mutation key', () => {
1008 expectTypeOf<MutationFunctionContext>().toEqualTypeOf<{
1009 client: QueryClient
1010 meta: MutationMeta | undefined
1011 mutationKey?: MutationKey
1012 }>()
1013 })
1014 })