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