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

useMutation.test-d.tsx

74 lines2.0 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { reactive } from 'vue-demi'
3import { useMutation } from '../useMutation'
4import { successMutator } from './test-utils'
5​
6describe('Discriminated union return type', () => {
7 it('data should be possibly undefined by default', () => {
8 const mutation = reactive(
9 useMutation({ mutationFn: successMutator<string> }),
10 )
11​
12 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
13 })
14​
15 it('data should be defined when mutation is success', () => {
16 const mutation = reactive(
17 useMutation({ mutationFn: successMutator<string> }),
18 )
19​
20 if (mutation.isSuccess) {
21 expectTypeOf(mutation.data).toEqualTypeOf<string>()
22 }
23 })
24​
25 it('error should be null when mutation is success', () => {
26 const mutation = reactive(
27 useMutation({ mutationFn: successMutator<string> }),
28 )
29​
30 if (mutation.isSuccess) {
31 expectTypeOf(mutation.error).toEqualTypeOf<null>()
32 }
33 })
34​
35 it('data should be undefined when mutation is pending', () => {
36 const mutation = reactive(
37 useMutation({ mutationFn: successMutator<string> }),
38 )
39​
40 if (mutation.isPending) {
41 expectTypeOf(mutation.data).toEqualTypeOf<undefined>()
42 }
43 })
44​
45 it('error should be defined when mutation is error', () => {
46 const mutation = reactive(
47 useMutation({ mutationFn: successMutator<string> }),
48 )
49​
50 if (mutation.isError) {
51 expectTypeOf(mutation.error).toEqualTypeOf<Error>()
52 }
53 })
54​
55 it('should narrow variables', () => {
56 const mutation = reactive(
57 useMutation({ mutationFn: successMutator<string> }),
58 )
59​
60 if (mutation.isIdle) {
61 expectTypeOf(mutation.variables).toEqualTypeOf<undefined>()
62 return
63 }
64 if (mutation.isPending) {
65 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
66 return
67 }
68 if (mutation.isSuccess) {
69 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
70 return
71 }
72 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
73 })
74})