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