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