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