1- import { beforeEach , describe , expect , test , vi } from 'vitest'
1+ import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
22import { reactive , ref } from 'vue-demi'
33import { sleep } from '@tanstack/query-test-utils'
44import { useMutation } from '../useMutation'
@@ -7,6 +7,13 @@ import { useQueryClient } from '../useQueryClient'
77vi . mock ( '../useQueryClient' )
88
99describe ( 'useMutation' , ( ) => {
10+ beforeEach ( ( ) => {
11+ vi . useFakeTimers ( )
12+ } )
13+ afterEach ( ( ) => {
14+ vi . useRealTimers ( )
15+ } )
16+
1017 test ( 'should be in idle state initially' , ( ) => {
1118 const mutation = useMutation ( {
1219 mutationFn : ( params ) => sleep ( 0 ) . then ( ( ) => params ) ,
@@ -41,10 +48,10 @@ describe('useMutation', () => {
4148 test ( 'should return error when request fails' , async ( ) => {
4249 const mutation = useMutation ( {
4350 mutationFn : ( ) =>
44- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
51+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
4552 } )
4653 mutation . mutate ( )
47- await sleep ( 10 )
54+ await vi . advanceTimersByTimeAsync ( 10 )
4855 expect ( mutation ) . toMatchObject ( {
4956 isIdle : { value : false } ,
5057 isPending : { value : false } ,
@@ -58,12 +65,12 @@ describe('useMutation', () => {
5865 test ( 'should return data when request succeeds' , async ( ) => {
5966 const result = 'Mock data'
6067 const mutation = useMutation ( {
61- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
68+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
6269 } )
6370
6471 mutation . mutate ( result )
6572
66- await sleep ( 10 )
73+ await vi . advanceTimersByTimeAsync ( 10 )
6774
6875 expect ( mutation ) . toMatchObject ( {
6976 isIdle : { value : false } ,
@@ -80,15 +87,15 @@ describe('useMutation', () => {
8087 const mutationCache = queryClient . getMutationCache ( )
8188 const options = reactive ( {
8289 mutationKey : [ 'foo' ] ,
83- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
90+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
8491 } )
8592 const mutation = useMutation ( options )
8693
8794 options . mutationKey = [ 'bar' ]
88- await sleep ( 0 )
95+ await vi . advanceTimersByTimeAsync ( 10 )
8996 mutation . mutate ( 'xyz' )
9097
91- await sleep ( 0 )
98+ await vi . advanceTimersByTimeAsync ( 10 )
9299
93100 const mutations = mutationCache . find ( { mutationKey : [ 'bar' ] } )
94101
@@ -112,15 +119,15 @@ describe('useMutation', () => {
112119 const mutationCache = queryClient . getMutationCache ( )
113120 const options = reactive ( {
114121 mutationKey,
115- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
122+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
116123 } )
117124 const mutation = useMutation ( options )
118125
119126 mutationKey . value [ 0 ] ! . otherObject . name = 'someOtherObjectName'
120- await sleep ( 0 )
127+ await vi . advanceTimersByTimeAsync ( 10 )
121128 mutation . mutate ( 'xyz' )
122129
123- await sleep ( 0 )
130+ await vi . advanceTimersByTimeAsync ( 10 )
124131
125132 const mutations = mutationCache . getAll ( )
126133 const relevantMutation = mutations . find ( ( m ) => {
@@ -147,12 +154,12 @@ describe('useMutation', () => {
147154 let proof = false
148155 mutationFn . value = ( params : string ) => {
149156 proof = true
150- return sleep ( 0 ) . then ( ( ) => params )
157+ return sleep ( 10 ) . then ( ( ) => params )
151158 }
152- await sleep ( 0 )
159+ await vi . advanceTimersByTimeAsync ( 10 )
153160
154161 mutation . mutate ( 'xyz' )
155- await sleep ( 0 )
162+ await vi . advanceTimersByTimeAsync ( 10 )
156163
157164 const mutations = mutationCache . find ( { mutationKey : [ 'bar2' ] } )
158165 expect ( mutations ?. options . mutationKey ) . toEqual ( [ 'bar2' ] )
@@ -162,12 +169,12 @@ describe('useMutation', () => {
162169 test ( 'should reset state after invoking mutation.reset' , async ( ) => {
163170 const mutation = useMutation ( {
164171 mutationFn : ( ) =>
165- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
172+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
166173 } )
167174
168175 mutation . mutate ( )
169176
170- await sleep ( 10 )
177+ await vi . advanceTimersByTimeAsync ( 10 )
171178
172179 mutation . reset ( )
173180
@@ -189,13 +196,13 @@ describe('useMutation', () => {
189196 test ( 'should call onMutate when passed as an option' , async ( ) => {
190197 const onMutate = vi . fn ( )
191198 const mutation = useMutation ( {
192- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
199+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
193200 onMutate,
194201 } )
195202
196203 mutation . mutate ( '' )
197204
198- await sleep ( 10 )
205+ await vi . advanceTimersByTimeAsync ( 10 )
199206
200207 expect ( onMutate ) . toHaveBeenCalledTimes ( 1 )
201208 } )
@@ -204,41 +211,41 @@ describe('useMutation', () => {
204211 const onError = vi . fn ( )
205212 const mutation = useMutation ( {
206213 mutationFn : ( ) =>
207- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
214+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
208215 onError,
209216 } )
210217
211218 mutation . mutate ( '' )
212219
213- await sleep ( 10 )
220+ await vi . advanceTimersByTimeAsync ( 10 )
214221
215222 expect ( onError ) . toHaveBeenCalledTimes ( 1 )
216223 } )
217224
218225 test ( 'should call onSuccess when passed as an option' , async ( ) => {
219226 const onSuccess = vi . fn ( )
220227 const mutation = useMutation ( {
221- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
228+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
222229 onSuccess,
223230 } )
224231
225232 mutation . mutate ( '' )
226233
227- await sleep ( 10 )
234+ await vi . advanceTimersByTimeAsync ( 10 )
228235
229236 expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
230237 } )
231238
232239 test ( 'should call onSettled when passed as an option' , async ( ) => {
233240 const onSettled = vi . fn ( )
234241 const mutation = useMutation ( {
235- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
242+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
236243 onSettled,
237244 } )
238245
239246 mutation . mutate ( '' )
240247
241- await sleep ( 10 )
248+ await vi . advanceTimersByTimeAsync ( 10 )
242249
243250 expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
244251 } )
@@ -247,38 +254,38 @@ describe('useMutation', () => {
247254 const onError = vi . fn ( )
248255 const mutation = useMutation ( {
249256 mutationFn : ( ) =>
250- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
257+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
251258 } )
252259
253260 mutation . mutate ( undefined , { onError } )
254261
255- await sleep ( 10 )
262+ await vi . advanceTimersByTimeAsync ( 10 )
256263
257264 expect ( onError ) . toHaveBeenCalledTimes ( 1 )
258265 } )
259266
260267 test ( 'should call onSuccess when passed as an argument of mutate function' , async ( ) => {
261268 const onSuccess = vi . fn ( )
262269 const mutation = useMutation ( {
263- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
270+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
264271 } )
265272
266273 mutation . mutate ( '' , { onSuccess } )
267274
268- await sleep ( 10 )
275+ await vi . advanceTimersByTimeAsync ( 10 )
269276
270277 expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
271278 } )
272279
273280 test ( 'should call onSettled when passed as an argument of mutate function' , async ( ) => {
274281 const onSettled = vi . fn ( )
275282 const mutation = useMutation ( {
276- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
283+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
277284 } )
278285
279286 mutation . mutate ( '' , { onSettled } )
280287
281- await sleep ( 10 )
288+ await vi . advanceTimersByTimeAsync ( 10 )
282289
283290 expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
284291 } )
@@ -287,13 +294,13 @@ describe('useMutation', () => {
287294 const onSettled = vi . fn ( )
288295 const onSettledOnFunction = vi . fn ( )
289296 const mutation = useMutation ( {
290- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
297+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
291298 onSettled,
292299 } )
293300
294301 mutation . mutate ( '' , { onSettled : onSettledOnFunction } )
295302
296- await sleep ( 10 )
303+ await vi . advanceTimersByTimeAsync ( 10 )
297304
298305 expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
299306 expect ( onSettledOnFunction ) . toHaveBeenCalledTimes ( 1 )
@@ -308,10 +315,12 @@ describe('useMutation', () => {
308315 test ( 'should resolve properly' , async ( ) => {
309316 const result = 'Mock data'
310317 const mutation = useMutation ( {
311- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
318+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
312319 } )
313320
314- await expect ( mutation . mutateAsync ( result ) ) . resolves . toBe ( result )
321+ await vi . waitFor ( ( ) =>
322+ expect ( mutation . mutateAsync ( result ) ) . resolves . toBe ( result ) ,
323+ )
315324
316325 expect ( mutation ) . toMatchObject ( {
317326 isIdle : { value : false } ,
@@ -326,10 +335,12 @@ describe('useMutation', () => {
326335 test ( 'should throw on error' , async ( ) => {
327336 const mutation = useMutation ( {
328337 mutationFn : ( ) =>
329- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
338+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
330339 } )
331340
332- await expect ( mutation . mutateAsync ( ) ) . rejects . toThrowError ( 'Some error' )
341+ await vi . waitFor ( ( ) =>
342+ expect ( mutation . mutateAsync ( ) ) . rejects . toThrowError ( 'Some error' ) ,
343+ )
333344
334345 expect ( mutation ) . toMatchObject ( {
335346 isIdle : { value : false } ,
@@ -347,15 +358,13 @@ describe('useMutation', () => {
347358 const err = new Error ( 'Expected mock error. All is well!' )
348359 const boundaryFn = vi . fn ( )
349360 const { mutate } = useMutation ( {
350- mutationFn : ( ) => {
351- return Promise . reject ( err )
352- } ,
361+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => Promise . reject ( err ) ) ,
353362 throwOnError : boundaryFn ,
354363 } )
355364
356365 mutate ( )
357366
358- await sleep ( 0 )
367+ await vi . advanceTimersByTimeAsync ( 10 )
359368
360369 expect ( boundaryFn ) . toHaveBeenCalledTimes ( 1 )
361370 expect ( boundaryFn ) . toHaveBeenCalledWith ( err )
0 commit comments