Skip to content

Commit 70b152a

Browse files
committed
test(ui): isolate Markdown table clipboard boundary
With isolate:false, the shared module registry can retain clipboard imports before this test installs its module mock, leaving the spy disconnected from the production call. Stub navigator.clipboard.writeText instead so the test exercises the real copyToClipboard boundary regardless of module load order, then restore the original descriptor.
1 parent d833e6e commit 70b152a

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

‎ui/src/components/markdown-tables.test.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import {
88
} from "./markdown-tables.ts";
99
import { toSanitizedMarkdownHtml } from "./markdown.ts";
1010

11-
const { copyToClipboard } = vi.hoisted(() => ({
12-
copyToClipboard: vi.fn(async () => true),
13-
}));
14-
15-
vi.mock("../lib/clipboard.ts", () => ({ copyToClipboard }));
11+
const clipboardDescriptor = Object.getOwnPropertyDescriptor(navigator, "clipboard");
12+
const writeText = vi.fn(async (_text: string) => undefined);
1613

1714
const markdown = `Open agent:main:dashboard:table
1815
@@ -74,8 +71,11 @@ describe("Markdown table interactions", () => {
7471
TestResizeObserver.instances = [];
7572
vi.stubGlobal("MutationObserver", TestMutationObserver);
7673
vi.stubGlobal("ResizeObserver", TestResizeObserver);
77-
copyToClipboard.mockClear();
78-
copyToClipboard.mockResolvedValue(true);
74+
writeText.mockClear();
75+
Object.defineProperty(navigator, "clipboard", {
76+
configurable: true,
77+
value: { writeText },
78+
});
7979
Object.defineProperty(HTMLDialogElement.prototype, "showModal", {
8080
configurable: true,
8181
value: vi.fn(function (this: HTMLDialogElement) {
@@ -94,6 +94,11 @@ describe("Markdown table interactions", () => {
9494
afterEach(() => {
9595
vi.useRealTimers();
9696
vi.unstubAllGlobals();
97+
if (clipboardDescriptor) {
98+
Object.defineProperty(navigator, "clipboard", clipboardDescriptor);
99+
} else {
100+
Reflect.deleteProperty(navigator, "clipboard");
101+
}
97102
document.body.replaceChildren();
98103
});
99104

@@ -136,7 +141,7 @@ describe("Markdown table interactions", () => {
136141
const { owner } = interactiveOwner();
137142
const copy = owner.querySelector<HTMLButtonElement>(".markdown-table__copy")!;
138143
copy.click();
139-
expect(copyToClipboard).toHaveBeenCalledWith("Name\tValue\nAlpha\tOne");
144+
expect(writeText).toHaveBeenCalledWith("Name\tValue\nAlpha\tOne");
140145
await vi.advanceTimersByTimeAsync(0);
141146
expect(copy.getAttribute("aria-label")).toBe("Copied!");
142147

0 commit comments

Comments
 (0)