Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { useMutation } from '../useMutation'
3import { QueryClient } from '../QueryClient'
4import type {
5 DefaultError,
6 MutationFunctionContext,
7 MutationKey,
8 QueryClient as QueryCoreClient,
9} from '@tanstack/query-core'
10import type { UseMutationResult } from '../types'
11β
12describe('useMutation', () => {
13 it('should infer TData from mutationFn return type', () => {
14 const mutation = useMutation(() => ({
15 mutationFn: () => Promise.resolve('data'),
16 }))
17β
18 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
19 expectTypeOf(mutation.error).toEqualTypeOf<DefaultError | null>()
20 })
21β
22 it('should infer TVariables from mutationFn parameter', () => {
23 const mutation = useMutation(() => ({
24 mutationFn: (vars: { id: string }) => Promise.resolve(vars.id),
25 }))
26β
27 expectTypeOf(mutation.mutate).toBeCallableWith({ id: '1' })
28 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
29 })
30β
31 it('should infer TOnMutateResult from onMutate return type', () => {
32 useMutation(() => ({
33 mutationFn: () => Promise.resolve('data'),
34 onMutate: () => {
35 return { token: 'abc' }
36 },
37 onSuccess: (_data, _variables, onMutateResult) => {
38 expectTypeOf(onMutateResult).toEqualTypeOf<{ token: string }>()
39 },
40 onError: (_error, _variables, onMutateResult) => {
41 expectTypeOf(onMutateResult).toEqualTypeOf<
42 { token: string } | undefined
43 >()
44 },
45 }))
46 })
47β
48 it('should allow explicit generic types', () => {
49 const mutation = useMutation<string, Error, { id: number }>(() => ({
50 mutationFn: (vars) => {
51 expectTypeOf(vars).toEqualTypeOf<{ id: number }>()
52 return Promise.resolve('result')
53 },
54 }))
55β
56 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
57 expectTypeOf(mutation.error).toEqualTypeOf<Error | null>()
58 })
59β
60 it('should return correct UseMutationResult type', () => {
61 const mutation = useMutation(() => ({
62 mutationFn: () => Promise.resolve(42),
63 }))
64β
65 expectTypeOf(mutation).toEqualTypeOf<
66 UseMutationResult<number, DefaultError, void, unknown>
67 >()
68 })
69β
70 it('should type mutateAsync with correct return type', () => {
71 const mutation = useMutation(() => ({
72 mutationFn: (id: string) => Promise.resolve(id.length),
73 }))
74β
75 expectTypeOf(mutation.mutateAsync).toBeCallableWith('test')
76 expectTypeOf(mutation.mutateAsync('test')).toEqualTypeOf<Promise<number>>()
77 })
78β
79 it('should default TVariables to void when mutationFn has no parameters', () => {
80 const mutation = useMutation(() => ({
81 mutationFn: () => Promise.resolve('data'),
82 }))
83β
84 expectTypeOf(mutation.mutate).toBeCallableWith()
85 })
86β
87 it('should allow calling mutate with no arguments when TVariables is optional undefinable', () => {
88 const mutation = useMutation(() => ({
89 mutationFn: (_variables: number | undefined) => Promise.resolve(1),
90 }))
91β
92 expectTypeOf(mutation.mutate).toBeCallableWith()
93 expectTypeOf(mutation.mutateAsync).toBeCallableWith()
94 })
95β
96 it('should infer custom TError type', () => {
97 class CustomError extends Error {
98 code: number
99 constructor(code: number) {
100 super()
101 this.code = code
102 }
103 }
104β
105 const mutation = useMutation<string, CustomError>(() => ({
106 mutationFn: () => Promise.resolve('data'),
107 }))
108β
109 expectTypeOf(mutation.error).toEqualTypeOf<CustomError | null>()
110 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
111 })
112β
113 it('should infer types for onSettled callback', () => {
114 useMutation(() => ({
115 mutationFn: () => Promise.resolve(42),
116 onSettled: (data, error, _variables, _onMutateResult) => {
117 expectTypeOf(data).toEqualTypeOf<number | undefined>()
118 expectTypeOf(error).toEqualTypeOf<DefaultError | null>()
119 },
120 }))
121 })
122β
123 it('should infer custom TError in onError callback', () => {
124 class CustomError extends Error {
125 code: number
126 constructor(code: number) {
127 super()
128 this.code = code
129 }
130 }
131β
132 useMutation<string, CustomError>(() => ({
133 mutationFn: () => Promise.resolve('data'),
134 onError: (error) => {
135 expectTypeOf(error).toEqualTypeOf<CustomError>()
136 },
137 }))
138 })
139β
140 it('should accept queryClient as second argument', () => {
141 const queryClient = new QueryClient()
142β
143 const mutation = useMutation(
144 () => ({
145 mutationFn: () => Promise.resolve('data'),
146 }),
147 () => queryClient,
148 )
149β
150 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
151 })
152β
153 it('should type context as the last argument for mutationFn and every hook-level callback', () => {
154 useMutation(() => ({
155 mutationFn: (_variables, context) => {
156 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
157 expectTypeOf(context.client).toEqualTypeOf<QueryCoreClient>()
158 return Promise.resolve('data')
159 },
160 onMutate: (_variables, context) => {
161 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
162 },
163 onSuccess: (_data, _variables, _onMutateResult, context) => {
164 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
165 },
166 onError: (_error, _variables, _onMutateResult, context) => {
167 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
168 },
169 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
170 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
171 },
172 }))
173 })
174β
175 it('should type context as the last argument for every per-call mutate option', () => {
176 const mutation = useMutation(() => ({
177 mutationFn: () => Promise.resolve('data'),
178 }))
179β
180 mutation.mutate(undefined, {
181 onSuccess: (_data, _variables, _onMutateResult, context) => {
182 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
183 },
184 onError: (_error, _variables, _onMutateResult, context) => {
185 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
186 },
187 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
188 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
189 },
190 })
191 })
192β
193 it('should type context.mutationKey as MutationKey', () => {
194 useMutation(() => ({
195 mutationKey: ['todos', 'add'] as const,
196 mutationFn: () => Promise.resolve('data'),
197 onSuccess: (_data, _variables, _onMutateResult, context) => {
198 expectTypeOf(context.mutationKey).toEqualTypeOf<
199 MutationKey | undefined
200 >()
201 },
202 }))
203 })
204})