@@ -12,10 +12,6 @@ import { sleep } from '@tanstack/query-test-utils'
12
12
import { QueryClient , injectMutation , provideTanStackQuery } from '..'
13
13
import { expectSignals , setFixtureSignalInputs } from './test-utils'
14
14
15
- const MUTATION_DURATION = 1000
16
-
17
- const resolveMutations = ( ) => vi . advanceTimersByTimeAsync ( MUTATION_DURATION )
18
-
19
15
describe ( 'injectMutation' , ( ) => {
20
16
let queryClient : QueryClient
21
17
@@ -49,19 +45,19 @@ describe('injectMutation', () => {
49
45
} )
50
46
} )
51
47
52
- test ( 'should change state after invoking mutate' , ( ) => {
48
+ test ( 'should change state after invoking mutate' , async ( ) => {
53
49
const result = 'Mock data'
54
50
55
51
const mutation = TestBed . runInInjectionContext ( ( ) => {
56
52
return injectMutation ( ( ) => ( {
57
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
53
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
58
54
} ) )
59
55
} )
60
56
61
57
TestBed . tick ( )
62
58
63
59
mutation . mutate ( result )
64
- vi . advanceTimersByTime ( 1 )
60
+ await vi . advanceTimersByTimeAsync ( 0 )
65
61
66
62
expectSignals ( mutation , {
67
63
isIdle : false ,
@@ -77,13 +73,13 @@ describe('injectMutation', () => {
77
73
const mutation = TestBed . runInInjectionContext ( ( ) => {
78
74
return injectMutation ( ( ) => ( {
79
75
mutationFn : ( ) =>
80
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
76
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
81
77
} ) )
82
78
} )
83
79
84
80
mutation . mutate ( )
85
81
86
- await resolveMutations ( )
82
+ await vi . advanceTimersByTimeAsync ( 11 )
87
83
88
84
expectSignals ( mutation , {
89
85
isIdle : false ,
@@ -99,13 +95,13 @@ describe('injectMutation', () => {
99
95
const result = 'Mock data'
100
96
const mutation = TestBed . runInInjectionContext ( ( ) => {
101
97
return injectMutation ( ( ) => ( {
102
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
98
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
103
99
} ) )
104
100
} )
105
101
106
102
mutation . mutate ( result )
107
103
108
- await resolveMutations ( )
104
+ await vi . advanceTimersByTimeAsync ( 11 )
109
105
110
106
expectSignals ( mutation , {
111
107
isIdle : false ,
@@ -142,19 +138,19 @@ describe('injectMutation', () => {
142
138
const mutation = TestBed . runInInjectionContext ( ( ) => {
143
139
return injectMutation ( ( ) => ( {
144
140
mutationFn : ( ) =>
145
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
141
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
146
142
} ) )
147
143
} )
148
144
149
145
mutation . mutate ( )
150
146
151
- await resolveMutations ( )
147
+ await vi . advanceTimersByTimeAsync ( 11 )
152
148
153
149
expect ( mutation . isError ( ) ) . toBe ( true )
154
150
155
151
mutation . reset ( )
156
152
157
- await resolveMutations ( )
153
+ await vi . advanceTimersByTimeAsync ( 0 )
158
154
159
155
expectSignals ( mutation , {
160
156
isIdle : true ,
@@ -175,14 +171,14 @@ describe('injectMutation', () => {
175
171
const onMutate = vi . fn ( )
176
172
const mutation = TestBed . runInInjectionContext ( ( ) => {
177
173
return injectMutation ( ( ) => ( {
178
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
174
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
179
175
onMutate,
180
176
} ) )
181
177
} )
182
178
183
179
mutation . mutate ( '' )
184
180
185
- await resolveMutations ( )
181
+ await vi . advanceTimersByTimeAsync ( 0 )
186
182
187
183
expect ( onMutate ) . toHaveBeenCalledTimes ( 1 )
188
184
} )
@@ -192,14 +188,14 @@ describe('injectMutation', () => {
192
188
const mutation = TestBed . runInInjectionContext ( ( ) => {
193
189
return injectMutation ( ( ) => ( {
194
190
mutationFn : ( _params : string ) =>
195
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
191
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
196
192
onError,
197
193
} ) )
198
194
} )
199
195
200
196
mutation . mutate ( '' )
201
197
202
- await resolveMutations ( )
198
+ await vi . advanceTimersByTimeAsync ( 11 )
203
199
204
200
expect ( onError ) . toHaveBeenCalledTimes ( 1 )
205
201
} )
@@ -208,14 +204,14 @@ describe('injectMutation', () => {
208
204
const onSuccess = vi . fn ( )
209
205
const mutation = TestBed . runInInjectionContext ( ( ) => {
210
206
return injectMutation ( ( ) => ( {
211
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
207
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
212
208
onSuccess,
213
209
} ) )
214
210
} )
215
211
216
212
mutation . mutate ( '' )
217
213
218
- await resolveMutations ( )
214
+ await vi . advanceTimersByTimeAsync ( 11 )
219
215
220
216
expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
221
217
} )
@@ -224,14 +220,14 @@ describe('injectMutation', () => {
224
220
const onSettled = vi . fn ( )
225
221
const mutation = TestBed . runInInjectionContext ( ( ) => {
226
222
return injectMutation ( ( ) => ( {
227
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
223
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
228
224
onSettled,
229
225
} ) )
230
226
} )
231
227
232
228
mutation . mutate ( '' )
233
229
234
- await resolveMutations ( )
230
+ await vi . advanceTimersByTimeAsync ( 11 )
235
231
236
232
expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
237
233
} )
@@ -241,13 +237,13 @@ describe('injectMutation', () => {
241
237
const mutation = TestBed . runInInjectionContext ( ( ) => {
242
238
return injectMutation ( ( ) => ( {
243
239
mutationFn : ( _params : string ) =>
244
- sleep ( 0 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
240
+ sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
245
241
} ) )
246
242
} )
247
243
248
244
mutation . mutate ( '' , { onError } )
249
245
250
- await resolveMutations ( )
246
+ await vi . advanceTimersByTimeAsync ( 11 )
251
247
252
248
expect ( onError ) . toHaveBeenCalledTimes ( 1 )
253
249
} )
@@ -256,13 +252,13 @@ describe('injectMutation', () => {
256
252
const onSuccess = vi . fn ( )
257
253
const mutation = TestBed . runInInjectionContext ( ( ) => {
258
254
return injectMutation ( ( ) => ( {
259
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
255
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
260
256
} ) )
261
257
} )
262
258
263
259
mutation . mutate ( '' , { onSuccess } )
264
260
265
- await resolveMutations ( )
261
+ await vi . advanceTimersByTimeAsync ( 11 )
266
262
267
263
expect ( onSuccess ) . toHaveBeenCalledTimes ( 1 )
268
264
} )
@@ -271,13 +267,13 @@ describe('injectMutation', () => {
271
267
const onSettled = vi . fn ( )
272
268
const mutation = TestBed . runInInjectionContext ( ( ) => {
273
269
return injectMutation ( ( ) => ( {
274
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
270
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
275
271
} ) )
276
272
} )
277
273
278
274
mutation . mutate ( '' , { onSettled } )
279
275
280
- await resolveMutations ( )
276
+ await vi . advanceTimersByTimeAsync ( 11 )
281
277
282
278
expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
283
279
} )
@@ -287,14 +283,14 @@ describe('injectMutation', () => {
287
283
const onSettledOnFunction = vi . fn ( )
288
284
const mutation = TestBed . runInInjectionContext ( ( ) => {
289
285
return injectMutation ( ( ) => ( {
290
- mutationFn : ( params : string ) => sleep ( 0 ) . then ( ( ) => params ) ,
286
+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
291
287
onSettled,
292
288
} ) )
293
289
} )
294
290
295
291
mutation . mutate ( '' , { onSettled : onSettledOnFunction } )
296
292
297
- await resolveMutations ( )
293
+ await vi . advanceTimersByTimeAsync ( 11 )
298
294
299
295
expect ( onSettled ) . toHaveBeenCalledTimes ( 1 )
300
296
expect ( onSettledOnFunction ) . toHaveBeenCalledTimes ( 1 )
@@ -317,7 +313,7 @@ describe('injectMutation', () => {
317
313
318
314
mutation = injectMutation ( ( ) => ( {
319
315
mutationKey : [ 'fake' , this . name ( ) ] ,
320
- mutationFn : ( ) => sleep ( 0 ) . then ( ( ) => this . name ( ) ) ,
316
+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => this . name ( ) ) ,
321
317
} ) )
322
318
323
319
mutate ( ) : void {
@@ -332,7 +328,7 @@ describe('injectMutation', () => {
332
328
const button = debugElement . query ( By . css ( 'button' ) )
333
329
button . triggerEventHandler ( 'click' )
334
330
335
- await resolveMutations ( )
331
+ await vi . advanceTimersByTimeAsync ( 11 )
336
332
fixture . detectChanges ( )
337
333
338
334
const text = debugElement . query ( By . css ( 'span' ) ) . nativeElement . textContent
@@ -358,7 +354,7 @@ describe('injectMutation', () => {
358
354
359
355
mutation = injectMutation ( ( ) => ( {
360
356
mutationKey : [ 'fake' , this . name ( ) ] ,
361
- mutationFn : ( ) => sleep ( 0 ) . then ( ( ) => this . name ( ) ) ,
357
+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => this . name ( ) ) ,
362
358
} ) )
363
359
364
360
mutate ( ) : void {
@@ -374,15 +370,15 @@ describe('injectMutation', () => {
374
370
const span = debugElement . query ( By . css ( 'span' ) )
375
371
376
372
button . triggerEventHandler ( 'click' )
377
- await resolveMutations ( )
373
+ await vi . advanceTimersByTimeAsync ( 11 )
378
374
fixture . detectChanges ( )
379
375
380
376
expect ( span . nativeElement . textContent ) . toEqual ( 'value' )
381
377
382
378
setFixtureSignalInputs ( fixture , { name : 'updatedValue' } )
383
379
384
380
button . triggerEventHandler ( 'click' )
385
- await resolveMutations ( )
381
+ await vi . advanceTimersByTimeAsync ( 11 )
386
382
fixture . detectChanges ( )
387
383
388
384
expect ( span . nativeElement . textContent ) . toEqual ( 'updatedValue' )
@@ -412,7 +408,7 @@ describe('injectMutation', () => {
412
408
413
409
mutate ( )
414
410
415
- await resolveMutations ( )
411
+ await vi . advanceTimersByTimeAsync ( 0 )
416
412
417
413
expect ( boundaryFn ) . toHaveBeenCalledTimes ( 1 )
418
414
expect ( boundaryFn ) . toHaveBeenCalledWith ( err )
0 commit comments