@@ -209,7 +209,6 @@ describe('useFundCardSetupOnrampEventListeners', () => {
209
209
} ) ;
210
210
211
211
it ( 'clears fund button reset timeout on unmount' , ( ) => {
212
- vi . useFakeTimers ( ) ;
213
212
let eventHandler : ( event : EventMetadata ) => void = ( ) => { } ;
214
213
215
214
( setupOnrampEventListeners as Mock ) . mockImplementation ( ( { onEvent } ) => {
@@ -229,7 +228,7 @@ describe('useFundCardSetupOnrampEventListeners', () => {
229
228
< >
230
229
< button role = "button" onClick = { ( ) => setIsUnmounted ( true ) } />
231
230
{ ! isUnmounted && < Child /> }
232
- < span data-status = { submitButtonState } > { submitButtonState } </ span >
231
+ < span data-testid = " submitButtonState" > { submitButtonState } </ span >
233
232
</ >
234
233
) ;
235
234
} ;
@@ -254,9 +253,94 @@ describe('useFundCardSetupOnrampEventListeners', () => {
254
253
} ) ;
255
254
256
255
// The submitButtonState should not reset because the component is unmounted
257
- expect ( screen . queryByText ( 'default' ) ) . not . toBeInTheDocument ( ) ;
256
+ expect ( screen . queryByTestId ( 'submitButtonState' ) ) . toHaveTextContent (
257
+ 'error' ,
258
+ ) ;
259
+ } ) ;
258
260
259
- vi . useRealTimers ( ) ;
261
+ it ( 'clears previous timeout when scheduleFundButtonReset is called again' , ( ) => {
262
+ let eventHandler : ( event : EventMetadata ) => void = ( ) => { } ;
263
+
264
+ ( setupOnrampEventListeners as Mock ) . mockImplementation ( ( { onEvent } ) => {
265
+ eventHandler = onEvent ;
266
+ return ( ) => { } ;
267
+ } ) ;
268
+
269
+ const Child = ( ) => {
270
+ useFundCardSetupOnrampEventListeners ( ) ;
271
+ return null ;
272
+ } ;
273
+
274
+ const Component = ( ) => {
275
+ const { submitButtonState } = useFundContext ( ) ;
276
+ return (
277
+ < >
278
+ < Child />
279
+ < span data-testid = "submitButtonState" > { submitButtonState } </ span >
280
+ </ >
281
+ ) ;
282
+ } ;
283
+
284
+ renderComponentWithProvider ( { Component } ) ;
285
+
286
+ act ( ( ) => {
287
+ eventHandler ( {
288
+ eventName : 'error' ,
289
+ error : {
290
+ errorType : 'network_error' ,
291
+ code : 'ERROR_CODE' ,
292
+ debugMessage : 'Error message' ,
293
+ } ,
294
+ } ) ;
295
+ } ) ;
296
+
297
+ act ( ( ) => {
298
+ eventHandler ( {
299
+ eventName : 'error' ,
300
+ error : {
301
+ errorType : 'network_error' ,
302
+ code : 'ERROR_CODE' ,
303
+ debugMessage : 'Error message' ,
304
+ } ,
305
+ } ) ;
306
+ } ) ;
307
+
308
+ // Advance timer partially
309
+ act ( ( ) => {
310
+ vi . advanceTimersByTime ( FUND_BUTTON_RESET_TIMEOUT / 2 ) ;
311
+ } ) ;
312
+
313
+ // Trigger second error which should clear first timeout
314
+ act ( ( ) => {
315
+ eventHandler ( {
316
+ eventName : 'error' ,
317
+ error : {
318
+ errorType : 'network_error' ,
319
+ code : 'ERROR_CODE' ,
320
+ debugMessage : 'Another error' ,
321
+ } ,
322
+ } ) ;
323
+ } ) ;
324
+
325
+ // Advance timer to what would have been the first timeout
326
+ act ( ( ) => {
327
+ vi . advanceTimersByTime ( FUND_BUTTON_RESET_TIMEOUT / 2 ) ;
328
+ } ) ;
329
+
330
+ // submitButtonState should still be in error state because first timeout was cleared
331
+ expect ( screen . queryByTestId ( 'submitButtonState' ) ) . toHaveTextContent (
332
+ 'error' ,
333
+ ) ;
334
+
335
+ // Advance timer to complete second timeout
336
+ act ( ( ) => {
337
+ vi . advanceTimersByTime ( FUND_BUTTON_RESET_TIMEOUT ) ;
338
+ } ) ;
339
+
340
+ // Now submitButtonState should be back to default
341
+ expect ( screen . queryByTestId ( 'submitButtonState' ) ) . toHaveTextContent (
342
+ 'default' ,
343
+ ) ;
260
344
} ) ;
261
345
262
346
it ( 'handles transition_view event correctly' , ( ) => {
0 commit comments