Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { QueryClient } from '@tanstack/query-core'
3import { useMutation } from '../useMutation'
4import type { DefaultError } from '@tanstack/query-core'
5import type { UseMutationResult } from '../types'
6β
7describe('useMutation', () => {
8 it('should infer TData from mutationFn return type', () => {
9 const mutation = useMutation({
10 mutationFn: () => Promise.resolve('data'),
11 })
12β
13 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
14 expectTypeOf(mutation.error).toEqualTypeOf<DefaultError | null>()
15 })
16β
17 it('should infer TVariables from mutationFn parameter', () => {
18 const mutation = useMutation({
19 mutationFn: (vars: { id: string }) => Promise.resolve(vars.id),
20 })
21β
22 expectTypeOf(mutation.mutate).toBeCallableWith({ id: '1' })
23 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
24 })
25β
26 it('should infer TOnMutateResult from onMutate return type', () => {
27 useMutation({
28 mutationFn: () => Promise.resolve('data'),
29 onMutate: () => {
30 return { token: 'abc' }
31 },
32 onSuccess: (_data, _variables, onMutateResult) => {
33 expectTypeOf(onMutateResult).toEqualTypeOf<{ token: string }>()
34 },
35 onError: (_error, _variables, onMutateResult) => {
36 expectTypeOf(onMutateResult).toEqualTypeOf<
37 { token: string } | undefined
38 >()
39 },
40 })
41 })
42β
43 it('should allow explicit generic types', () => {
44 const mutation = useMutation<string, Error, { id: number }>({
45 mutationFn: (vars) => {
46 expectTypeOf(vars).toEqualTypeOf<{ id: number }>()
47 return Promise.resolve('result')
48 },
49 })
50β
51 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
52 expectTypeOf(mutation.error).toEqualTypeOf<Error | null>()
53 })
54β
55 it('should return correct UseMutationResult type', () => {
56 const mutation = useMutation({
57 mutationFn: () => Promise.resolve(42),
58 })
59β
60 expectTypeOf(mutation).toEqualTypeOf<
61 UseMutationResult<number, DefaultError, void, unknown>
62 >()
63 })
64β
65 it('should type mutateAsync with correct return type', () => {
66 const mutation = useMutation({
67 mutationFn: (id: string) => Promise.resolve(id.length),
68 })
69β
70 expectTypeOf(mutation.mutateAsync).toBeCallableWith('test')
71 expectTypeOf(mutation.mutateAsync('test')).toEqualTypeOf<Promise<number>>()
72 })
73β
74 it('should default TVariables to void when mutationFn has no parameters', () => {
75 const mutation = useMutation({
76 mutationFn: () => Promise.resolve('data'),
77 })
78β
79 expectTypeOf(mutation.mutate).toBeCallableWith()
80 })
81β
82 it('should allow calling mutate with no arguments when TVariables is optional undefinable', () => {
83 const mutation = useMutation({
84 mutationFn: (_variables: number | undefined) => Promise.resolve(1),
85 })
86β
87 expectTypeOf(mutation.mutate).toBeCallableWith()
88 expectTypeOf(mutation.mutateAsync).toBeCallableWith()
89 })
90β
91 it('should infer custom TError type', () => {
92 class CustomError extends Error {
93 code: number
94 constructor(code: number) {
95 super()
96 this.code = code
97 }
98 }
99β
100 const mutation = useMutation<string, CustomError>({
101 mutationFn: () => Promise.resolve('data'),
102 })
103β
104 expectTypeOf(mutation.error).toEqualTypeOf<CustomError | null>()
105 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
106 })
107β
108 it('should infer types for onSettled callback', () => {
109 useMutation({
110 mutationFn: () => Promise.resolve(42),
111 onSettled: (data, error, _variables, _onMutateResult) => {
112 expectTypeOf(data).toEqualTypeOf<number | undefined>()
113 expectTypeOf(error).toEqualTypeOf<DefaultError | null>()
114 },
115 })
116 })
117β
118 it('should infer custom TError in onError callback', () => {
119 class CustomError extends Error {
120 code: number
121 constructor(code: number) {
122 super()
123 this.code = code
124 }
125 }
126β
127 useMutation<string, CustomError>({
128 mutationFn: () => Promise.resolve('data'),
129 onError: (error) => {
130 expectTypeOf(error).toEqualTypeOf<CustomError>()
131 },
132 })
133 })
134β
135 it('should accept queryClient as second argument', () => {
136 const queryClient = new QueryClient()
137β
138 const mutation = useMutation(
139 {
140 mutationFn: () => Promise.resolve('data'),
141 },
142 queryClient,
143 )
144β
145 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
146 })
147})