11// Phase 5 race conditions: ReportView must abort in-flight report fetches
22// when it unmounts or when its route props change, and must issue a fresh
33// fetch on remount (Technical Appendix §9).
4- import { describe , test , expect , beforeEach , afterEach , spyOn } from 'bun:test' ;
5- import { mock } from 'bun:test' ;
4+ import { describe , test , expect , beforeEach , afterEach , spyOn , mock } from 'bun:test' ;
65
76mock . module ( 'next/navigation' , ( ) => ( {
87 usePathname : ( ) => '/alice/one' ,
@@ -14,19 +13,6 @@ mock.module('next/script', () => ({ default: () => null }));
1413import { render , waitFor , cleanup , installRtlDom } from './rtl' ;
1514import { jsx as _jsx } from 'react/jsx-runtime' ;
1615
17- let resolveEnhanceReport ;
18- const enhanceTablesCalls = [ ] ;
19-
20- mock . module ( '../../lib/report-view.js' , ( ) => ( {
21- enhanceReport : ( ) => new Promise ( resolve => { resolveEnhanceReport = resolve ; } ) ,
22- bindPopupHandlers : ( ) => { } ,
23- stashStatefulDom : ( ) => new Map ( ) ,
24- graftStatefulDom : ( ) => { }
25- } ) ) ;
26- mock . module ( '../../lib/table-enhancer.js' , ( ) => ( {
27- enhanceTables : ( ...args ) => enhanceTablesCalls . push ( args )
28- } ) ) ;
29-
3016import ReportView from '../../components/report-view' ;
3117import { resetWidgetRegistry } from '../../lib/widget-loader' ;
3218
@@ -42,8 +28,6 @@ describe('ReportView fetch lifecycle', () => {
4228 beforeEach ( ( ) => {
4329 resetWidgetRegistry ( ) ;
4430 mockFetch = spyOn ( global , 'fetch' ) ;
45- resolveEnhanceReport = undefined ;
46- enhanceTablesCalls . length = 0 ;
4731 } ) ;
4832 afterEach ( ( ) => {
4933 mockFetch . mockRestore ( ) ;
@@ -126,6 +110,14 @@ describe('ReportView fetch lifecycle', () => {
126110 } ) ;
127111
128112 test ( 'does not bind table handlers after unmount while report enhancement is pending' , async ( ) => {
113+ // bun's mock.module leaks across test files, so control the render
114+ // pipeline through ReportView's injected implementations instead.
115+ let resolveEnhanceReport ;
116+ const enhanceTablesCalls = [ ] ;
117+ const enhanceReportImpl =
118+ ( ) => new Promise ( resolve => { resolveEnhanceReport = resolve ; } ) ;
119+ const enhanceTablesImpl = ( ...args ) => enhanceTablesCalls . push ( args ) ;
120+
129121 mockFetch . mockImplementation ( requested => {
130122 const url = String ( requested ) ;
131123 if ( url . endsWith ( 'assets/refactor-first-report.mustache' ) ) {
@@ -137,7 +129,8 @@ describe('ReportView fetch lifecycle', () => {
137129 return Promise . resolve ( { ok : false , status : 404 } ) ;
138130 } ) ;
139131 const utils = render ( _jsx ( ReportView , {
140- username : 'alice' , repository : 'one' , widgetSettleMs : 0
132+ username : 'alice' , repository : 'one' , widgetSettleMs : 0 ,
133+ enhanceReportImpl, enhanceTablesImpl
141134 } ) ) ;
142135
143136 await waitFor ( ( ) => expect ( resolveEnhanceReport ) . toBeTypeOf ( 'function' ) ) ;
0 commit comments