Open History to see the last commit
1import { QueryClient } from '@tanstack/query-core'
2import { createKey } from '@tanstack/query-test-utils'
3import { assertType, describe, expectTypeOf, it } from 'vitest'
4import {
5 createMutation,
6 mutationOptions,
7 useIsMutating,
8 useMutationState,
9} from '../../src/index.js'
10import type {
11 DefaultError,
12 MutationFunctionContext,
13 MutationState,
14 WithRequired,
15} from '@tanstack/query-core'
16import type {
17 CreateMutationOptions,
18 CreateMutationResult,
19} from '../../src/types.js'
20β
21describe('mutationOptions', () => {
22 it('should not allow excess properties', () => {
23 const key = createKey()
24β
25 // @ts-expect-error this is a good error, because onMutates does not exist!
26 mutationOptions({
27 mutationFn: () => Promise.resolve(5),
28 mutationKey: key,
29 onMutates: 1000,
30 onSuccess: (data) => {
31 expectTypeOf(data).toEqualTypeOf<number>()
32 },
33 })
34 })
35β
36 it('should infer types for callbacks', () => {
37 const key = createKey()
38β
39 mutationOptions({
40 mutationFn: () => Promise.resolve(5),
41 mutationKey: key,
42 onSuccess: (data) => {
43 expectTypeOf(data).toEqualTypeOf<number>()
44 },
45 })
46 })
47β
48 it('should infer types for onError callback', () => {
49 const key = createKey()
50β
51 mutationOptions({
52 mutationFn: () => {
53 throw new Error('fail')
54 },
55 mutationKey: key,
56 onError: (error) => {
57 expectTypeOf(error).toEqualTypeOf<DefaultError>()
58 },
59 })
60 })
61β
62 it('should infer types for variables', () => {
63 const key = createKey()
64β
65 mutationOptions<number, DefaultError, { id: string }>({
66 mutationFn: (vars) => {
67 expectTypeOf(vars).toEqualTypeOf<{ id: string }>()
68 return Promise.resolve(5)
69 },
70 mutationKey: key,
71 })
72 })
73β
74 it('should infer result type correctly', () => {
75 const key = createKey()
76β
77 mutationOptions<number, DefaultError, void, { name: string }>({
78 mutationFn: () => Promise.resolve(5),
79 mutationKey: key,
80 onMutate: () => {
81 return { name: 'onMutateResult' }
82 },
83 onSuccess: (_data, _variables, onMutateResult) => {
84 expectTypeOf(onMutateResult).toEqualTypeOf<{ name: string }>()
85 },
86 })
87 })
88β
89 it('should infer context type correctly', () => {
90 const key = createKey()
91β
92 mutationOptions<number>({
93 mutationFn: (_variables, context) => {
94 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
95 return Promise.resolve(5)
96 },
97 mutationKey: key,
98 onMutate: (_variables, context) => {
99 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
100 },
101 onSuccess: (_data, _variables, _onMutateResult, context) => {
102 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
103 },
104 onError: (_error, _variables, _onMutateResult, context) => {
105 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
106 },
107 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
108 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
109 },
110 })
111 })
112β
113 it('should error if mutationFn return type mismatches TData', () => {
114 assertType(
115 mutationOptions<number>({
116 // @ts-expect-error this is a good error, because return type is string, not number
117 mutationFn: async () => Promise.resolve('wrong return'),
118 }),
119 )
120 })
121β
122 it('should allow mutationKey to be omitted', () => {
123 return mutationOptions({
124 mutationFn: () => Promise.resolve(123),
125 onSuccess: (data) => {
126 expectTypeOf(data).toEqualTypeOf<number>()
127 },
128 })
129 })
130β
131 it('should infer all types when not explicitly provided', () => {
132 const key = createKey()
133β
134 expectTypeOf(
135 mutationOptions({
136 mutationFn: (id: string) => Promise.resolve(id.length),
137 mutationKey: key,
138 onSuccess: (data) => {
139 expectTypeOf(data).toEqualTypeOf<number>()
140 },
141 }),
142 ).toEqualTypeOf<
143 WithRequired<
144 CreateMutationOptions<number, DefaultError, string>,
145 'mutationKey'
146 >
147 >()
148 expectTypeOf(
149 mutationOptions({
150 mutationFn: (id: string) => Promise.resolve(id.length),
151 onSuccess: (data) => {
152 expectTypeOf(data).toEqualTypeOf<number>()
153 },
154 }),
155 ).toEqualTypeOf<
156 Omit<CreateMutationOptions<number, DefaultError, string>, 'mutationKey'>
157 >()
158 })
159β
160 it('should work when used with createMutation', () => {
161 const key = createKey()
162β
163 const mutation = createMutation(() =>
164 mutationOptions({
165 mutationKey: key,
166 mutationFn: () => Promise.resolve('data'),
167 onSuccess: (data) => {
168 expectTypeOf(data).toEqualTypeOf<string>()
169 },
170 }),
171 )
172 expectTypeOf(mutation).toEqualTypeOf<
173 CreateMutationResult<string, DefaultError, void, unknown>
174 >()
175β
176 createMutation(() =>
177 // should allow when used with createMutation without mutationKey
178 mutationOptions({
179 mutationFn: () => Promise.resolve('data'),
180 onSuccess: (data) => {
181 expectTypeOf(data).toEqualTypeOf<string>()
182 },
183 }),
184 )
185 })
186β
187 it('should work when used with useIsMutating', () => {
188 const key = createKey()
189β
190 const isMutating = useIsMutating(
191 mutationOptions({
192 mutationKey: key,
193 mutationFn: () => Promise.resolve(5),
194 }),
195 )
196 expectTypeOf(isMutating.current).toEqualTypeOf<number>()
197β
198 useIsMutating(
199 // @ts-expect-error filters should have mutationKey
200 mutationOptions({
201 mutationFn: () => Promise.resolve(5),
202 }),
203 )
204 })
205β
206 it('should work when used with queryClient.isMutating', () => {
207 const key = createKey()
208 const queryClient = new QueryClient()
209β
210 const isMutating = queryClient.isMutating(
211 mutationOptions({
212 mutationKey: key,
213 mutationFn: () => Promise.resolve(5),
214 }),
215 )
216 expectTypeOf(isMutating).toEqualTypeOf<number>()
217β
218 queryClient.isMutating(
219 // @ts-expect-error filters should have mutationKey
220 mutationOptions({
221 mutationFn: () => Promise.resolve(5),
222 }),
223 )
224 })
225β
226 it('should work when used with useMutationState', () => {
227 const key = createKey()
228β
229 const mutationState = useMutationState({
230 filters: mutationOptions({
231 mutationKey: key,
232 mutationFn: () => Promise.resolve(5),
233 }),
234 })
235 expectTypeOf(mutationState).toEqualTypeOf<
236 Array<MutationState<unknown, Error, unknown, unknown>>
237 >()
238β
239 useMutationState({
240 // @ts-expect-error filters should have mutationKey
241 filters: mutationOptions({
242 mutationFn: () => Promise.resolve(5),
243 }),
244 })
245 })
246})