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

useMutation.test-d.tsx

189 lines6.1 KB

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 mutate with a Promise return type and no mutateAsync', () => {
71 const mutation = useMutation(() => ({
72 mutationFn: (id: string) => Promise.resolve(id.length),
73 }))
74​
75 expectTypeOf(mutation.mutate).toBeCallableWith('test')
76 expectTypeOf(mutation.mutate('test')).toEqualTypeOf<Promise<number>>()
77 // mutateAsync is removed; the single mutate returns the promise.
78 expectTypeOf(mutation).not.toHaveProperty('mutateAsync')
79 // context is removed from the result: no onMutate context threading.
80 expectTypeOf(mutation).not.toHaveProperty('context')
81 })
82​
83 it('should default TVariables to void when mutationFn has no parameters', () => {
84 const mutation = useMutation(() => ({
85 mutationFn: () => Promise.resolve('data'),
86 }))
87​
88 expectTypeOf(mutation.mutate).toBeCallableWith()
89 })
90​
91 it('should allow calling mutate with no arguments when TVariables is optional undefinable', () => {
92 const mutation = useMutation(() => ({
93 mutationFn: (_variables: number | undefined) => Promise.resolve(1),
94 }))
95​
96 expectTypeOf(mutation.mutate).toBeCallableWith()
97 })
98​
99 it('should infer custom TError type', () => {
100 class CustomError extends Error {
101 code: number
102 constructor(code: number) {
103 super()
104 this.code = code
105 }
106 }
107​
108 const mutation = useMutation<string, CustomError>(() => ({
109 mutationFn: () => Promise.resolve('data'),
110 }))
111​
112 expectTypeOf(mutation.error).toEqualTypeOf<CustomError | null>()
113 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
114 })
115​
116 it('should infer types for onSettled callback', () => {
117 useMutation(() => ({
118 mutationFn: () => Promise.resolve(42),
119 onSettled: (data, error, _variables, _onMutateResult) => {
120 expectTypeOf(data).toEqualTypeOf<number | undefined>()
121 expectTypeOf(error).toEqualTypeOf<DefaultError | null>()
122 },
123 }))
124 })
125​
126 it('should infer custom TError in onError callback', () => {
127 class CustomError extends Error {
128 code: number
129 constructor(code: number) {
130 super()
131 this.code = code
132 }
133 }
134​
135 useMutation<string, CustomError>(() => ({
136 mutationFn: () => Promise.resolve('data'),
137 onError: (error) => {
138 expectTypeOf(error).toEqualTypeOf<CustomError>()
139 },
140 }))
141 })
142​
143 it('should accept queryClient as second argument', () => {
144 const queryClient = new QueryClient()
145​
146 const mutation = useMutation(
147 () => ({
148 mutationFn: () => Promise.resolve('data'),
149 }),
150 () => queryClient,
151 )
152​
153 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
154 })
155​
156 it('should type context as the last argument for mutationFn and every hook-level callback', () => {
157 useMutation(() => ({
158 mutationFn: (_variables, context) => {
159 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
160 expectTypeOf(context.client).toEqualTypeOf<QueryCoreClient>()
161 return Promise.resolve('data')
162 },
163 onMutate: (_variables, context) => {
164 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
165 },
166 onSuccess: (_data, _variables, _onMutateResult, context) => {
167 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
168 },
169 onError: (_error, _variables, _onMutateResult, context) => {
170 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
171 },
172 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
173 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
174 },
175 }))
176 })
177​
178 it('should type context.mutationKey as MutationKey', () => {
179 useMutation(() => ({
180 mutationKey: ['todos', 'add'] as const,
181 mutationFn: () => Promise.resolve('data'),
182 onSuccess: (_data, _variables, _onMutateResult, context) => {
183 expectTypeOf(context.mutationKey).toEqualTypeOf<
184 MutationKey | undefined
185 >()
186 },
187 }))
188 })
189})