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<
67 { name: string } | undefined
68 >()
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 UseMutationOptions<number, DefaultError, string>,
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<UseMutationOptions<number, DefaultError, string>, 'mutationKey'>
137 >()
138 })
139β
140 it('should infer types when used with useMutation', () => {
141 const mutation = useMutation(
142 mutationOptions({
143 mutationKey: ['key'],
144 mutationFn: () => Promise.resolve('data'),
145 onSuccess: (data) => {
146 expectTypeOf(data).toEqualTypeOf<string>()
147 },
148 }),
149 )
150 expectTypeOf(mutation).toEqualTypeOf<
151 UseMutationResult<string, DefaultError, void, unknown>
152 >()
153β
154 useMutation(
155 // should allow when used with useMutation without mutationKey
156 mutationOptions({
157 mutationFn: () => Promise.resolve('data'),
158 onSuccess: (data) => {
159 expectTypeOf(data).toEqualTypeOf<string>()
160 },
161 }),
162 )
163 })
164β
165 it('should infer types when used with useIsMutating', () => {
166 const isMutating = useIsMutating(
167 mutationOptions({
168 mutationKey: ['key'],
169 mutationFn: () => Promise.resolve(5),
170 }),
171 )
172 expectTypeOf(isMutating).toEqualTypeOf<number>()
173β
174 useIsMutating(
175 // @ts-expect-error filters should have mutationKey
176 mutationOptions({
177 mutationFn: () => Promise.resolve(5),
178 }),
179 )
180 })
181β
182 it('should infer types when used with queryClient.isMutating', () => {
183 const queryClient = new QueryClient()
184β
185 const isMutating = queryClient.isMutating(
186 mutationOptions({
187 mutationKey: ['key'],
188 mutationFn: () => Promise.resolve(5),
189 }),
190 )
191 expectTypeOf(isMutating).toEqualTypeOf<number>()
192β
193 queryClient.isMutating(
194 // @ts-expect-error filters should have mutationKey
195 mutationOptions({
196 mutationFn: () => Promise.resolve(5),
197 }),
198 )
199 })
200β
201 it('should infer types when used with useMutationState', () => {
202 const mutationState = useMutationState({
203 filters: mutationOptions({
204 mutationKey: ['key'],
205 mutationFn: () => Promise.resolve(5),
206 }),
207 })
208 expectTypeOf(mutationState).toEqualTypeOf<
209 Array<MutationState<unknown, Error, unknown, unknown>>
210 >()
211β
212 useMutationState({
213 // @ts-expect-error filters should have mutationKey
214 filters: mutationOptions({
215 mutationFn: () => Promise.resolve(5),
216 }),
217 })
218 })
219})