|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { afterEach, it } from "node:test"; |
| 3 | + |
| 4 | +import { JSDOM } from "jsdom"; |
| 5 | +import React from "react"; |
| 6 | +import { act } from "react"; |
| 7 | +import { createRoot } from "react-dom/client"; |
| 8 | + |
| 9 | +import { |
| 10 | + CHANNEL_SWITCH_MEASURE, |
| 11 | + beginChannelSwitchTrace, |
| 12 | + resetChannelSwitchTrace, |
| 13 | + settleChannelSwitchTrace, |
| 14 | +} from "../../shared/lib/channelSwitchPerf.ts"; |
| 15 | +import { useChannelSwitchTraceMarks } from "./useChannelSwitchTraceMarks.ts"; |
| 16 | + |
| 17 | +// These tests run the hook under the real react-dom development build, whose |
| 18 | +// StrictMode replays every effect (setup → cleanup → setup) on mount — the |
| 19 | +// exact dev-runtime lifecycle that used to abandon a just-opened trace and |
| 20 | +// break the Performance-panel workflow. |
| 21 | + |
| 22 | +const originalDocument = globalThis.document; |
| 23 | +const originalWindow = globalThis.window; |
| 24 | +const originalActEnvironment = globalThis.IS_REACT_ACT_ENVIRONMENT; |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + resetChannelSwitchTrace(); |
| 28 | + performance.clearMeasures?.(CHANNEL_SWITCH_MEASURE); |
| 29 | + if (originalDocument === undefined) delete globalThis.document; |
| 30 | + else globalThis.document = originalDocument; |
| 31 | + if (originalWindow === undefined) delete globalThis.window; |
| 32 | + else globalThis.window = originalWindow; |
| 33 | + if (originalActEnvironment === undefined) |
| 34 | + delete globalThis.IS_REACT_ACT_ENVIRONMENT; |
| 35 | + else globalThis.IS_REACT_ACT_ENVIRONMENT = originalActEnvironment; |
| 36 | +}); |
| 37 | + |
| 38 | +function setupDom() { |
| 39 | + const dom = new JSDOM( |
| 40 | + "<!doctype html><html><body><div id='root'></div></body></html>", |
| 41 | + ); |
| 42 | + const frames = []; |
| 43 | + dom.window.requestAnimationFrame = (cb) => frames.push(cb) && frames.length; |
| 44 | + dom.window.cancelAnimationFrame = () => {}; |
| 45 | + Object.assign(globalThis, { |
| 46 | + document: dom.window.document, |
| 47 | + IS_REACT_ACT_ENVIRONMENT: true, |
| 48 | + window: dom.window, |
| 49 | + }); |
| 50 | + const flushFrames = () => { |
| 51 | + // Drain chained rAFs until quiescent. |
| 52 | + for (let i = 0; i < 20 && frames.length > 0; i += 1) { |
| 53 | + for (const cb of frames.splice(0, frames.length)) cb(); |
| 54 | + } |
| 55 | + }; |
| 56 | + return { dom, flushFrames }; |
| 57 | +} |
| 58 | + |
| 59 | +function Harness({ channelId, isTimelineLoading }) { |
| 60 | + useChannelSwitchTraceMarks({ |
| 61 | + activeChannelId: channelId, |
| 62 | + activeChannelType: "stream", |
| 63 | + isTimelineLoading, |
| 64 | + }); |
| 65 | + return null; |
| 66 | +} |
| 67 | + |
| 68 | +function renderHarness(root, props) { |
| 69 | + return act(async () => |
| 70 | + root.render( |
| 71 | + React.createElement( |
| 72 | + React.StrictMode, |
| 73 | + null, |
| 74 | + React.createElement(Harness, props), |
| 75 | + ), |
| 76 | + ), |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +const measures = () => |
| 81 | + performance |
| 82 | + .getEntriesByName(CHANNEL_SWITCH_MEASURE) |
| 83 | + .map((entry) => entry.detail?.channelId); |
| 84 | + |
| 85 | +it("a trace survives StrictMode's effect replay and still settles", async () => { |
| 86 | + const { dom, flushFrames } = setupDom(); |
| 87 | + performance.clearMeasures?.(CHANNEL_SWITCH_MEASURE); |
| 88 | + beginChannelSwitchTrace("chan-strict"); |
| 89 | + const root = createRoot(document.getElementById("root")); |
| 90 | + await renderHarness(root, { |
| 91 | + channelId: "chan-strict", |
| 92 | + isTimelineLoading: true, |
| 93 | + }); |
| 94 | + await renderHarness(root, { |
| 95 | + channelId: "chan-strict", |
| 96 | + isTimelineLoading: false, |
| 97 | + }); |
| 98 | + flushFrames(); |
| 99 | + assert.deepEqual(measures(), ["chan-strict"]); |
| 100 | + await act(async () => root.unmount()); |
| 101 | + dom.window.close(); |
| 102 | +}); |
| 103 | + |
| 104 | +it("a real route exit still abandons: a history-back settle records nothing", async () => { |
| 105 | + const { dom, flushFrames } = setupDom(); |
| 106 | + performance.clearMeasures?.(CHANNEL_SWITCH_MEASURE); |
| 107 | + beginChannelSwitchTrace("chan-exit"); |
| 108 | + const root = createRoot(document.getElementById("root")); |
| 109 | + await renderHarness(root, { |
| 110 | + channelId: "chan-exit", |
| 111 | + isTimelineLoading: true, |
| 112 | + }); |
| 113 | + // Leaving the channel surface unmounts the hook; with no re-setup to |
| 114 | + // cancel it, the scheduled abandon must fire. |
| 115 | + await act(async () => root.unmount()); |
| 116 | + await new Promise((resolve) => setImmediate(resolve)); |
| 117 | + // History-back re-enters without goChannel; its settle must find no trace. |
| 118 | + settleChannelSwitchTrace("chan-exit"); |
| 119 | + flushFrames(); |
| 120 | + assert.deepEqual(measures(), []); |
| 121 | + dom.window.close(); |
| 122 | +}); |
| 123 | + |
| 124 | +it("an A→B switch's deferred abandon of A never kills B's trace", async () => { |
| 125 | + const { dom, flushFrames } = setupDom(); |
| 126 | + performance.clearMeasures?.(CHANNEL_SWITCH_MEASURE); |
| 127 | + beginChannelSwitchTrace("chan-a"); |
| 128 | + const root = createRoot(document.getElementById("root")); |
| 129 | + await renderHarness(root, { channelId: "chan-a", isTimelineLoading: true }); |
| 130 | + beginChannelSwitchTrace("chan-b"); |
| 131 | + await renderHarness(root, { channelId: "chan-b", isTimelineLoading: true }); |
| 132 | + await renderHarness(root, { channelId: "chan-b", isTimelineLoading: false }); |
| 133 | + flushFrames(); |
| 134 | + assert.deepEqual(measures(), ["chan-b"]); |
| 135 | + await act(async () => root.unmount()); |
| 136 | + dom.window.close(); |
| 137 | +}); |
0 commit comments