@@ -270,6 +270,79 @@ describe('ReportView', () => {
270270 } ) ;
271271} ) ;
272272
273+ // Widget gate: the render effect races widget readiness against a fallback
274+ // setTimeout. The fallback handle must be cleared when the race settles and
275+ // again if the effect is torn down first, instead of keeping the closure
276+ // (and its references) alive until widgetSettleMs elapses.
277+ describe ( 'widget settle fallback timer' , ( ) => {
278+ const SETTLE_MS = 4321 ; // distinctive delay so the fallback is identifiable
279+
280+ function trackTimers ( ) {
281+ const realSet = globalThis . setTimeout ;
282+ const realClear = globalThis . clearTimeout ;
283+ const created = [ ] ;
284+ const cleared = new Set ( ) ;
285+ globalThis . setTimeout = ( fn , delay , ...args ) => {
286+ const handle = realSet ( fn , delay , ...args ) ;
287+ created . push ( { handle, delay } ) ;
288+ return handle ;
289+ } ;
290+ globalThis . clearTimeout = handle => {
291+ cleared . add ( handle ) ;
292+ return realClear ( handle ) ;
293+ } ;
294+ return {
295+ handlesFor : delay => created . filter ( t => t . delay === delay ) . map ( t => t . handle ) ,
296+ isCleared : handle => cleared . has ( handle ) ,
297+ restore ( ) {
298+ globalThis . setTimeout = realSet ;
299+ globalThis . clearTimeout = realClear ;
300+ }
301+ } ;
302+ }
303+
304+ test ( 'clears the fallback timer once the race settles' , async ( ) => {
305+ const timers = trackTimers ( ) ;
306+ try {
307+ // Widgets already ready, so waitForWidget resolves synchronously and
308+ // creates no timers; the only SETTLE_MS timer is the race fallback.
309+ markWidgetReady ( 'chart' ) ;
310+ markWidgetReady ( 'vizdom' ) ;
311+ respondJsonFor ( sampleJson ) ;
312+ await renderReport ( { widgetSettleMs : SETTLE_MS } ) ;
313+
314+ const handles = timers . handlesFor ( SETTLE_MS ) ;
315+ expect ( handles ) . toHaveLength ( 1 ) ;
316+ expect ( timers . isCleared ( handles [ 0 ] ) ) . toBe ( true ) ;
317+ } finally {
318+ timers . restore ( ) ;
319+ }
320+ } ) ;
321+
322+ test ( 'clears a still-pending fallback timer when the effect is torn down' , async ( ) => {
323+ const timers = trackTimers ( ) ;
324+ try {
325+ respondJsonFor ( sampleJson ) ; // widgets never become ready
326+ const utils = render ( _jsx ( ReportView , {
327+ username : 'junit-team' ,
328+ repository : 'junit4' ,
329+ widgetSettleMs : SETTLE_MS
330+ } ) ) ;
331+ // waitForWidget timers (chart, vizdom) + the race fallback, in order.
332+ await waitFor ( ( ) => {
333+ expect ( timers . handlesFor ( SETTLE_MS ) . length ) . toBeGreaterThanOrEqual ( 3 ) ;
334+ } ) ;
335+ const fallbackTimer = timers . handlesFor ( SETTLE_MS ) . at ( - 1 ) ;
336+ expect ( timers . isCleared ( fallbackTimer ) ) . toBe ( false ) ;
337+
338+ utils . unmount ( ) ;
339+ expect ( timers . isCleared ( fallbackTimer ) ) . toBe ( true ) ;
340+ } finally {
341+ timers . restore ( ) ;
342+ }
343+ } ) ;
344+ } ) ;
345+
273346// ---------------------------------------------------------------------------
274347// Enhanced tables (plan: implement-paginated-tables-with-sticky-headers):
275348// pagination, sorting, search/filter, CSV export and copy-to-clipboard wired
0 commit comments