@@ -31,14 +31,13 @@ describe('mutationObserver', () => {
31
31
32
32
unsubscribe1 ( )
33
33
34
- await vi . waitFor ( ( ) => {
35
- // 1 call: loading
36
- expect ( subscription1Handler ) . toBeCalledTimes ( 1 )
37
- // 2 calls: loading, success
38
- expect ( subscription2Handler ) . toBeCalledTimes ( 2 )
39
- } )
34
+ expect ( subscription1Handler ) . toBeCalledTimes ( 1 )
35
+ expect ( subscription2Handler ) . toBeCalledTimes ( 1 )
36
+
37
+ await vi . advanceTimersByTimeAsync ( 20 )
38
+ expect ( subscription1Handler ) . toBeCalledTimes ( 1 )
39
+ expect ( subscription2Handler ) . toBeCalledTimes ( 2 )
40
40
41
- // Clean-up
42
41
unsubscribe2 ( )
43
42
} )
44
43
@@ -53,15 +52,14 @@ describe('mutationObserver', () => {
53
52
const unsubscribe = mutation . subscribe ( subscriptionHandler )
54
53
55
54
mutation . mutate ( 'input' )
56
- await vi . waitFor ( ( ) =>
57
- expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 1 ) ,
58
- )
55
+
56
+ await vi . advanceTimersByTimeAsync ( 5 )
57
+ expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 1 )
59
58
60
59
unsubscribe ( )
61
60
62
- await vi . waitFor ( ( ) =>
63
- expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 0 ) ,
64
- )
61
+ await vi . advanceTimersByTimeAsync ( 10 )
62
+ expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 0 )
65
63
} )
66
64
67
65
test ( 'reset should remove observer to trigger GC' , async ( ) => {
@@ -75,15 +73,14 @@ describe('mutationObserver', () => {
75
73
const unsubscribe = mutation . subscribe ( subscriptionHandler )
76
74
77
75
mutation . mutate ( 'input' )
78
- await vi . waitFor ( ( ) =>
79
- expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 1 ) ,
80
- )
76
+
77
+ await vi . advanceTimersByTimeAsync ( 5 )
78
+ expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 1 )
81
79
82
80
mutation . reset ( )
83
81
84
- await vi . waitFor ( ( ) =>
85
- expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 0 ) ,
86
- )
82
+ await vi . advanceTimersByTimeAsync ( 10 )
83
+ expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 0 )
87
84
88
85
unsubscribe ( )
89
86
} )
@@ -101,12 +98,11 @@ describe('mutationObserver', () => {
101
98
102
99
mutation . mutate ( 'input' )
103
100
104
- await vi . waitFor ( ( ) =>
105
- expect ( mutation . getCurrentResult ( ) ) . toMatchObject ( {
106
- status : 'success' ,
107
- data : 'input' ,
108
- } ) ,
109
- )
101
+ await vi . advanceTimersByTimeAsync ( 5 )
102
+ expect ( mutation . getCurrentResult ( ) ) . toMatchObject ( {
103
+ status : 'success' ,
104
+ data : 'input' ,
105
+ } )
110
106
111
107
mutation . setOptions ( {
112
108
mutationKey : [ ...key , '2' ] ,
@@ -132,17 +128,16 @@ describe('mutationObserver', () => {
132
128
133
129
mutationObserver . mutate ( 'input' )
134
130
135
- await vi . waitFor ( ( ) =>
136
- expect (
137
- queryClient . getMutationCache ( ) . find ( { mutationKey : [ ...key , '1' ] } ) ,
138
- ) . toMatchObject ( {
139
- options : { mutationKey : [ ...key , '1' ] } ,
140
- state : {
141
- status : 'success' ,
142
- data : 'input' ,
143
- } ,
144
- } ) ,
145
- )
131
+ await vi . advanceTimersByTimeAsync ( 5 )
132
+ expect (
133
+ queryClient . getMutationCache ( ) . find ( { mutationKey : [ ...key , '1' ] } ) ,
134
+ ) . toMatchObject ( {
135
+ options : { mutationKey : [ ...key , '1' ] } ,
136
+ state : {
137
+ status : 'success' ,
138
+ data : 'input' ,
139
+ } ,
140
+ } )
146
141
147
142
mutationObserver . setOptions ( {
148
143
mutationKey : [ ...key , '2' ] ,
@@ -173,15 +168,14 @@ describe('mutationObserver', () => {
173
168
174
169
mutationObserver . mutate ( 'input' )
175
170
176
- await vi . waitFor ( ( ) =>
177
- expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
178
- options : { meta : { a : 1 } } ,
179
- state : {
180
- status : 'success' ,
181
- data : 'input' ,
182
- } ,
183
- } ) ,
184
- )
171
+ await vi . advanceTimersByTimeAsync ( 5 )
172
+ expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
173
+ options : { meta : { a : 1 } } ,
174
+ state : {
175
+ status : 'success' ,
176
+ data : 'input' ,
177
+ } ,
178
+ } )
185
179
186
180
mutationObserver . setOptions ( {
187
181
meta : { a : 2 } ,
@@ -251,14 +245,14 @@ describe('mutationObserver', () => {
251
245
const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
252
246
253
247
mutationObserver . mutate ( 'input' ) . catch ( ( ) => undefined )
254
- await vi . waitFor ( ( ) =>
255
- expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
256
- options : { meta : { a : 1 } } ,
257
- state : {
258
- status : 'error' ,
259
- } ,
260
- } ) ,
261
- )
248
+
249
+ await vi . advanceTimersByTimeAsync ( 5 )
250
+ expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
251
+ options : { meta : { a : 1 } } ,
252
+ state : {
253
+ status : 'error' ,
254
+ } ,
255
+ } )
262
256
263
257
mutationObserver . setOptions ( {
264
258
meta : { a : 2 } ,
0 commit comments