@@ -122,7 +122,24 @@ describe('useMutation', () => {
122
122
} )
123
123
124
124
describe ( 'hooks' , ( ) => {
125
- it ( 'invokes the "onMutate" hook before mutating' , async ( ) => {
125
+ it ( 'invokes the "onBeforeMutate" hook before mutating' , async ( ) => {
126
+ const onBeforeMutate = vi . fn ( )
127
+ const { wrapper } = mountSimple ( {
128
+ mutation : async ( { a, b } : { a : number , b : number } ) => {
129
+ return a + b
130
+ } ,
131
+ onBeforeMutate,
132
+ } )
133
+ expect ( onBeforeMutate ) . not . toHaveBeenCalled ( )
134
+ wrapper . vm . mutate ( { a : 24 , b : 42 } )
135
+ expect ( onBeforeMutate ) . toHaveBeenCalledTimes ( 1 )
136
+ expect ( onBeforeMutate ) . toHaveBeenLastCalledWith ( { a : 24 , b : 42 } , expect . objectContaining ( { } ) )
137
+ wrapper . vm . mutateAsync ( { a : 0 , b : 1 } )
138
+ expect ( onBeforeMutate ) . toHaveBeenCalledTimes ( 2 )
139
+ expect ( onBeforeMutate ) . toHaveBeenLastCalledWith ( { a : 0 , b : 1 } , expect . objectContaining ( { } ) )
140
+ } )
141
+
142
+ it ( 'works with the deprecated local "onMutate"' , async ( ) => {
126
143
const onMutate = vi . fn ( )
127
144
const { wrapper } = mountSimple ( {
128
145
mutation : async ( { a, b } : { a : number , b : number } ) => {
@@ -137,6 +154,22 @@ describe('useMutation', () => {
137
154
wrapper . vm . mutateAsync ( { a : 0 , b : 1 } )
138
155
expect ( onMutate ) . toHaveBeenCalledTimes ( 2 )
139
156
expect ( onMutate ) . toHaveBeenLastCalledWith ( { a : 0 , b : 1 } , expect . objectContaining ( { } ) )
157
+ expect ( '"onMutate" option is deprecated' ) . toHaveBeenWarned ( )
158
+ } )
159
+
160
+ it ( 'warns about misusing deprecated "onMutate"' , async ( ) => {
161
+ const onMutate = vi . fn ( )
162
+ const onBeforeMutate = vi . fn ( )
163
+ const { wrapper } = mountSimple ( {
164
+ mutation : async ( { a, b } : { a : number , b : number } ) => {
165
+ return a + b
166
+ } ,
167
+ onMutate,
168
+ onBeforeMutate,
169
+ } )
170
+ wrapper . vm . mutate ( { a : 24 , b : 42 } )
171
+ expect ( '"onMutate" option is deprecated' ) . toHaveBeenWarned ( )
172
+ expect ( 'Use only "onBeforeMutate"' ) . toHaveBeenWarned ( )
140
173
} )
141
174
142
175
it ( 'invokes the "onError" hook if mutation throws' , async ( ) => {
@@ -154,36 +187,36 @@ describe('useMutation', () => {
154
187
expect ( onError ) . toHaveBeenCalledWith ( new Error ( '24' ) , 24 , expect . objectContaining ( { } ) )
155
188
} )
156
189
157
- it ( 'invokes the "onError" hook if onMutate throws' , async ( ) => {
190
+ it ( 'invokes the "onError" hook if onBeforeMutate throws' , async ( ) => {
158
191
const onError = vi . fn ( )
159
192
const { wrapper } = mountSimple ( {
160
- onMutate ( ) {
161
- throw new Error ( 'onMutate ' )
193
+ onBeforeMutate ( ) {
194
+ throw new Error ( 'onBeforeMutate ' )
162
195
} ,
163
196
onError,
164
197
} )
165
198
166
199
wrapper . vm . mutate ( )
167
200
await flushPromises ( )
168
201
expect ( onError ) . toHaveBeenCalledWith (
169
- new Error ( 'onMutate ' ) ,
202
+ new Error ( 'onBeforeMutate ' ) ,
170
203
undefined ,
171
204
expect . objectContaining ( { } ) ,
172
205
)
173
206
} )
174
207
175
- it ( 'passes the returned value from onMutate to onError' , async ( ) => {
208
+ it ( 'passes the returned value from onBeforeMutate to onError' , async ( ) => {
176
209
const onError = vi . fn ( )
177
210
const { wrapper, mutation } = mountSimple ( {
178
- onMutate : ( ) => ( { foo : 'bar' } ) ,
211
+ onBeforeMutate : ( ) => ( { foo : 'bar' } ) ,
179
212
onError,
180
213
} )
181
214
182
- mutation . mockRejectedValueOnce ( new Error ( 'onMutate ' ) )
215
+ mutation . mockRejectedValueOnce ( new Error ( 'onBeforeMutate ' ) )
183
216
wrapper . vm . mutate ( )
184
217
await flushPromises ( )
185
218
expect ( onError ) . toHaveBeenCalledWith (
186
- new Error ( 'onMutate ' ) ,
219
+ new Error ( 'onBeforeMutate ' ) ,
187
220
undefined ,
188
221
expect . objectContaining ( { foo : 'bar' } ) ,
189
222
)
@@ -205,12 +238,12 @@ describe('useMutation', () => {
205
238
expect ( wrapper . vm . error ) . toEqual ( null )
206
239
} )
207
240
208
- it ( 'awaits the "onMutate " hook before mutation' , async ( ) => {
209
- const onMutate = vi . fn ( async ( ) => delay ( 10 ) )
210
- const { wrapper, mutation } = mountSimple ( { onMutate } )
241
+ it ( 'awaits the "onBeforeMutate " hook before mutation' , async ( ) => {
242
+ const onBeforeMutate = vi . fn ( async ( ) => delay ( 10 ) )
243
+ const { wrapper, mutation } = mountSimple ( { onBeforeMutate } )
211
244
212
245
wrapper . vm . mutate ( )
213
- expect ( onMutate ) . toHaveBeenCalled ( )
246
+ expect ( onBeforeMutate ) . toHaveBeenCalled ( )
214
247
expect ( mutation ) . not . toHaveBeenCalled ( )
215
248
vi . advanceTimersByTime ( 10 )
216
249
expect ( mutation ) . not . toHaveBeenCalled ( )
@@ -249,14 +282,14 @@ describe('useMutation', () => {
249
282
expect ( wrapper . vm . error ) . toEqual ( new Error ( 'onSuccess' ) )
250
283
} )
251
284
252
- it ( 'sets the error if "onMutate " throws' , async ( ) => {
253
- const onMutate = vi . fn ( ) . mockRejectedValueOnce ( new Error ( 'onMutate ' ) )
254
- const { wrapper } = mountSimple ( { onMutate } )
285
+ it ( 'sets the error if "onBeforeMutate " throws' , async ( ) => {
286
+ const onBeforeMutate = vi . fn ( ) . mockRejectedValueOnce ( new Error ( 'onBeforeMutate ' ) )
287
+ const { wrapper } = mountSimple ( { onBeforeMutate } )
255
288
256
289
wrapper . vm . mutate ( )
257
290
await flushPromises ( )
258
- expect ( onMutate ) . toHaveBeenCalled ( )
259
- expect ( wrapper . vm . error ) . toEqual ( new Error ( 'onMutate ' ) )
291
+ expect ( onBeforeMutate ) . toHaveBeenCalled ( )
292
+ expect ( wrapper . vm . error ) . toEqual ( new Error ( 'onBeforeMutate ' ) )
260
293
} )
261
294
262
295
describe ( 'invokes the "onSettled" hook' , ( ) => {
@@ -296,37 +329,37 @@ describe('useMutation', () => {
296
329
} )
297
330
} )
298
331
299
- it ( 'triggers global onMutate ' , async ( ) => {
300
- const onMutate = vi . fn ( )
332
+ it ( 'triggers global onBeforeMutate ' , async ( ) => {
333
+ const onBeforeMutate = vi . fn ( )
301
334
const { wrapper } = mountSimple (
302
335
{ } ,
303
336
{
304
- plugins : [ createPinia ( ) , [ PiniaColada , { mutationOptions : { onMutate } } ] ] ,
337
+ plugins : [ createPinia ( ) , [ PiniaColada , { mutationOptions : { onBeforeMutate } } ] ] ,
305
338
} ,
306
339
)
307
340
308
- expect ( onMutate ) . toHaveBeenCalledTimes ( 0 )
341
+ expect ( onBeforeMutate ) . toHaveBeenCalledTimes ( 0 )
309
342
wrapper . vm . mutate ( )
310
343
// no need since it's synchronous
311
344
// await flushPromises()
312
- expect ( onMutate ) . toHaveBeenCalledTimes ( 1 )
313
- expect ( onMutate ) . toHaveBeenCalledWith ( undefined )
345
+ expect ( onBeforeMutate ) . toHaveBeenCalledTimes ( 1 )
346
+ expect ( onBeforeMutate ) . toHaveBeenCalledWith ( undefined )
314
347
} )
315
348
316
- it ( 'local onMutate receives global onMutate result' , async ( ) => {
317
- const onMutate = vi . fn ( ( ) => ( { foo : 'bar' } ) )
349
+ it ( 'local onBeforeMutate receives global onBeforeMutate result' , async ( ) => {
350
+ const onBeforeMutate = vi . fn ( ( ) => ( { foo : 'bar' } ) )
318
351
const { wrapper } = mountSimple (
319
- { onMutate } ,
352
+ { onBeforeMutate } ,
320
353
{
321
354
plugins : [
322
355
createPinia ( ) ,
323
- [ PiniaColada , { mutationOptions : { onMutate : ( ) => ( { global : true } ) } } ] ,
356
+ [ PiniaColada , { mutationOptions : { onBeforeMutate : ( ) => ( { global : true } ) } } ] ,
324
357
] ,
325
358
} ,
326
359
)
327
360
328
361
wrapper . vm . mutate ( )
329
- expect ( onMutate ) . toHaveBeenCalledWith ( undefined , { global : true } )
362
+ expect ( onBeforeMutate ) . toHaveBeenCalledWith ( undefined , { global : true } )
330
363
} )
331
364
332
365
it ( 'triggers global onSuccess' , async ( ) => {
0 commit comments