Open History to see the last commit
1import { QueryClient } from '@tanstack/query-core'
2import { describe, expectTypeOf, it } from 'vitest'
3import { createMutation } from '../../src/index.js'
4import type {
5 DefaultError,
6 MutationFunctionContext,
7 MutationKey,
8} from '@tanstack/query-core'
9import type { CreateMutationResult } from '../../src/types.js'
10β
11describe('createMutation', () => {
12 it('should infer TData from mutationFn return type', () => {
13 const mutation = createMutation(() => ({
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 = createMutation(() => ({
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 createMutation(() => ({
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 = createMutation<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 CreateMutationResult type', () => {
60 const mutation = createMutation(() => ({
61 mutationFn: () => Promise.resolve(42),
62 }))
63β
64 expectTypeOf(mutation).toEqualTypeOf<
65 CreateMutationResult<number, DefaultError, void, unknown>
66 >()
67 })
68β
69 it('should type mutateAsync with correct return type', () => {
70 const mutation = createMutation(() => ({
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 = createMutation(() => ({
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 = createMutation(() => ({
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 = createMutation<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 createMutation(() => ({
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 createMutation<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 = createMutation(
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 createMutation(() => ({
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 = createMutation(() => ({
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 createMutation(() => ({
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})