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

useMutation.test-d.tsx

203 lines6.4 KB

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