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