Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { reactive } from 'vue-demi'
3import { sleep } from '@tanstack/query-test-utils'
4import { useMutation } from '../useMutation'
5import type {
6 MutationFunctionContext,
7 MutationKey,
8 QueryClient,
9} from '@tanstack/query-core'
10β
11describe('Discriminated union return type', () => {
12 it('data should be possibly undefined by default', () => {
13 const mutation = reactive(
14 useMutation({
15 mutationFn: (params: string) => sleep(0).then(() => params),
16 }),
17 )
18β
19 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
20 })
21β
22 it('data should be defined when mutation is success', () => {
23 const mutation = reactive(
24 useMutation({
25 mutationFn: (params: string) => sleep(0).then(() => params),
26 }),
27 )
28β
29 if (mutation.isSuccess) {
30 expectTypeOf(mutation.data).toEqualTypeOf<string>()
31 }
32 })
33β
34 it('error should be null when mutation is success', () => {
35 const mutation = reactive(
36 useMutation({
37 mutationFn: (params: string) => sleep(0).then(() => params),
38 }),
39 )
40β
41 if (mutation.isSuccess) {
42 expectTypeOf(mutation.error).toEqualTypeOf<null>()
43 }
44 })
45β
46 it('data should be undefined when mutation is pending', () => {
47 const mutation = reactive(
48 useMutation({
49 mutationFn: (params: string) => sleep(0).then(() => params),
50 }),
51 )
52β
53 if (mutation.isPending) {
54 expectTypeOf(mutation.data).toEqualTypeOf<undefined>()
55 }
56 })
57β
58 it('error should be defined when mutation is error', () => {
59 const mutation = reactive(
60 useMutation({
61 mutationFn: (params: string) => sleep(0).then(() => params),
62 }),
63 )
64β
65 if (mutation.isError) {
66 expectTypeOf(mutation.error).toEqualTypeOf<Error>()
67 }
68 })
69β
70 it('should narrow variables', () => {
71 const mutation = reactive(
72 useMutation({
73 mutationFn: (params: string) => sleep(0).then(() => params),
74 }),
75 )
76β
77 if (mutation.isIdle) {
78 expectTypeOf(mutation.variables).toEqualTypeOf<undefined>()
79 return
80 }
81 if (mutation.isPending) {
82 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
83 return
84 }
85 if (mutation.isSuccess) {
86 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
87 return
88 }
89 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
90 })
91})
92β
93describe('useMutation', () => {
94 it('should allow calling mutate with no arguments when TVariables is optional undefinable', () => {
95 const mutation = reactive(
96 useMutation({
97 mutationFn: (_variables: number | undefined) => sleep(0).then(() => 1),
98 }),
99 )
100β
101 expectTypeOf(mutation.mutate).toBeCallableWith()
102 expectTypeOf(mutation.mutateAsync).toBeCallableWith()
103 })
104β
105 it('should type context as the last argument for mutationFn and every hook-level callback', () => {
106 useMutation({
107 mutationFn: (_variables, context) => {
108 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
109 expectTypeOf(context.client).toEqualTypeOf<QueryClient>()
110 return Promise.resolve('data')
111 },
112 onMutate: (_variables, context) => {
113 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
114 },
115 onSuccess: (_data, _variables, _onMutateResult, context) => {
116 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
117 },
118 onError: (_error, _variables, _onMutateResult, context) => {
119 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
120 },
121 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
122 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
123 },
124 })
125 })
126β
127 it('should type context as the last argument for every per-call mutate option', () => {
128 const mutation = useMutation({
129 mutationFn: () => Promise.resolve('data'),
130 })
131β
132 mutation.mutate(undefined, {
133 onSuccess: (_data, _variables, _onMutateResult, context) => {
134 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
135 },
136 onError: (_error, _variables, _onMutateResult, context) => {
137 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
138 },
139 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
140 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
141 },
142 })
143 })
144β
145 it('should type context.mutationKey as MutationKey', () => {
146 useMutation({
147 mutationKey: ['todos', 'add'] as const,
148 mutationFn: () => Promise.resolve('data'),
149 onSuccess: (_data, _variables, _onMutateResult, context) => {
150 expectTypeOf(context.mutationKey).toEqualTypeOf<
151 MutationKey | undefined
152 >()
153 },
154 })
155 })
156})