@@ -228,7 +228,7 @@ describe("useResourceWatcher", () => {
228228 expect ( mockAddEventListener ) . toHaveBeenCalledWith ( "visibilitychange" , expect . any ( Function ) ) ;
229229 } ) ;
230230
231- it ( "should restart watching with short interval when app becomes visible" , async ( ) => {
231+ it ( "should switch to short interval when app becomes visible without restarting watcher " , async ( ) => {
232232 const { startWatchingResource } = useResourceWatcher ( mockWatchHandler ) ;
233233
234234 startWatchingResource ( ) ;
@@ -242,22 +242,22 @@ describe("useResourceWatcher", () => {
242242 mockDocument . visibilityState = "hidden" ;
243243 visibilityChangeHandler ( ) ;
244244
245- // Clear the handler calls from the visibility change restart
245+ // Clear the handler calls
246246 mockWatchHandler . mockClear ( ) ;
247247
248248 // Simulate app becoming visible again
249249 mockDocument . visibilityState = "visible" ;
250250 visibilityChangeHandler ( ) ;
251251
252- // Should immediately call the handler (from startWatchingResource )
252+ // Should NOT immediately call the handler (watcher already running, no restart )
253253 await flushPromises ( ) ;
254- expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 1 ) ;
254+ expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 0 ) ;
255255
256- // Should continue with short polling interval
256+ // But should continue with short polling interval on next scheduled poll
257257 jest . advanceTimersByTime ( 3000 ) ;
258258 await flushPromises ( ) ;
259259
260- expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 2 ) ;
260+ expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 1 ) ;
261261 } ) ;
262262 } ) ;
263263
@@ -494,25 +494,25 @@ describe("useResourceWatcher", () => {
494494 mockDocument . visibilityState = "hidden" ;
495495 visibilityChangeHandler ( ) ;
496496
497- // Change to visible - this restarts the watcher
497+ // Change to visible - this changes currentPollingInterval back to short but doesn't restart
498498 mockDocument . visibilityState = "visible" ;
499499 visibilityChangeHandler ( ) ;
500500 await flushPromises ( ) ;
501- expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 2 ) ; // + 1 from becoming visible
501+ expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 1 ) ; // No additional call from becoming visible
502502
503- // Change to hidden again - this changes currentPollingInterval but doesn't restart timer
504- mockDocument . visibilityState = "hidden" ;
505- visibilityChangeHandler ( ) ;
506-
507- // The timer that was started when becoming visible is still running with short interval
503+ // The timer continues with the short interval
508504 jest . advanceTimersByTime ( 3000 ) ;
509505 await flushPromises ( ) ;
510- expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 3 ) ; // + 1 from short interval timer
506+ expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 2 ) ; // + 1 from short interval timer
507+
508+ // Change to hidden again
509+ mockDocument . visibilityState = "hidden" ;
510+ visibilityChangeHandler ( ) ;
511511
512- // Now the next timer should be using the long interval
512+ // The next timer should use the long interval
513513 jest . advanceTimersByTime ( 10000 ) ;
514514 await flushPromises ( ) ;
515- expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 4 ) ; // + 1 from long interval timer
515+ expect ( mockWatchHandler ) . toHaveBeenCalledTimes ( 3 ) ; // + 1 from long interval timer
516516 } ) ;
517517 } ) ;
518518} ) ;
0 commit comments