Skip to content

Commit a9b5750

Browse files
Fixed unit test
1 parent 34246e3 commit a9b5750

3 files changed

Lines changed: 22 additions & 24 deletions

File tree

‎components/report-view.jsx‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ export default function ReportView({
5757
environment: environmentProp,
5858
platformBaseUrl: platformBaseUrlProp,
5959
widgetSettleMs = 5000,
60-
toastDurationMs = TABLE_CONFIG.copy.toastDuration
60+
toastDurationMs = TABLE_CONFIG.copy.toastDuration,
61+
// Test seams (bun's mock.module leaks across test files, so lifecycle
62+
// tests inject Deferred/spying implementations instead of module mocks).
63+
enhanceReportImpl = enhanceReport,
64+
enhanceTablesImpl = enhanceTables
6165
}) {
6266
const containerRef = useRef(null);
6367
const searchParams = useSearchParams();
@@ -173,7 +177,7 @@ export default function ReportView({
173177
container.dataset.resolvedBranch = payload.resolvedBranch;
174178

175179
if (payloadChanged) {
176-
await enhanceReport(container, payload.data);
180+
await enhanceReportImpl(container, payload.data);
177181
if (cancelled) return;
178182
lastEnhancedPayloadRef.current = payload;
179183
} else {
@@ -182,7 +186,7 @@ export default function ReportView({
182186
// them is cheap (unlike charts/graph layout, which the graft saved).
183187
bindPopupHandlers(container);
184188
}
185-
enhanceTables(container, {
189+
enhanceTablesImpl(container, {
186190
data: payload.data,
187191
tableStates,
188192
onTableAction: handleTableAction,
@@ -213,7 +217,8 @@ export default function ReportView({
213217

214218
run();
215219
return () => { cancelled = true; };
216-
}, [payload, tableStates, widgetSettleMs, username, repository, branch, handleTableAction, handleCopy]);
220+
}, [payload, tableStates, widgetSettleMs, username, repository, branch,
221+
handleTableAction, handleCopy, enhanceReportImpl, enhanceTablesImpl]);
217222

218223
return (
219224
<>

‎tests/integration/report-view-abort.test.jsx‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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

76
mock.module('next/navigation', () => ({
87
usePathname: () => '/alice/one',
@@ -14,19 +13,6 @@ mock.module('next/script', () => ({ default: () => null }));
1413
import { render, waitFor, cleanup, installRtlDom } from './rtl';
1514
import { 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-
3016
import ReportView from '../../components/report-view';
3117
import { 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'));

‎tests/unit/report-view.test.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ describe('bindPopupHandlers (template uses data attributes, no inline handlers)'
143143
it('opens a popup when a 2D button is clicked', () => {
144144
bindPopupHandlers(document.getElementById('app'));
145145
document.querySelector('[data-popup-2d]').click();
146-
expect(document.getElementById('overlay').style.display).toBe('none');
147-
expect(document.getElementById('popup-classGraph').style.display).toBe('none');
146+
expect(document.getElementById('overlay').style.display).toBe('block');
147+
expect(document.getElementById('popup-classGraph').style.display).toBe('block');
148148
});
149149

150150
it('closes popups from the close button and the overlay', () => {

0 commit comments

Comments
 (0)