返回 DeepSeek-Reasonix
active-tab-mirror.test.tsx
根目录 / desktop / frontend / src / __tests__ / active-tab-mirror.test.tsx
1 import assert from "node:assert/strict";
2 import React, { act } from "react";
3 import { createRoot } from "react-dom/client";
4 import { JSDOM } from "jsdom";
5 import { activeTabMirror, useActiveTabMirrorCommit } from "../app-runtime/activeTabMirror";
6
7 const dom = new JSDOM("<div id='root'></div>");
8 Object.assign(globalThis, { window: dom.window, document: dom.window.document, IS_REACT_ACT_ENVIRONMENT: true });
9 const root = createRoot(document.getElementById("root")!);
10
11 function Probe({ activeTabId }: { activeTabId?: string }) {
12 useActiveTabMirrorCommit(activeTabId);
13 return null;
14 }
15
16 try {
17 await act(async () => root.render(<Probe activeTabId="A" />));
18 assert.equal(activeTabMirror().current, "A", "the mirror follows the committed active tab");
19
20 const reads: (string | undefined)[] = [];
21 const deferredRead = new Promise<void>((resolve) => {
22 setTimeout(() => {
23 reads.push(activeTabMirror().current);
24 resolve();
25 }, 0);
26 });
27 await act(async () => root.render(<Probe activeTabId="B" />));
28 await deferredRead;
29 assert.deepEqual(reads, ["B"], "an async continuation reads the replacement tab, never a stale render capture");
30
31 await act(async () => root.render(<Probe activeTabId={undefined} />));
32 assert.equal(activeTabMirror().current, undefined, "a committed empty selection clears the mirror");
33
34 await act(async () => root.render(<Probe activeTabId="B" />));
35 assert.equal(activeTabMirror().current, "B", "returning to a tab commits its identity again");
36
37 await act(async () => root.unmount());
38 assert.equal(activeTabMirror().current, undefined, "unmounting the host releases the mirror");
39
40 console.log("active tab mirror: commit-following writes, async reads and unmount release passed");
41 } finally { dom.window.close(); }
42
42 lines Plain Text