Skip to content

Commit 10ddc6d

Browse files
akjclaude
andcommitted
test(web): link menu tests run in a real browser
Main moved React tests to headless Chromium and compiles the web app with the React Compiler. Port the link menu hook test, route the ChatMarkdown permissions test through the in-page link menu, and drop manual memoization from the hook. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9ecad30 commit 10ddc6d

3 files changed

Lines changed: 85 additions & 77 deletions

File tree

‎apps/web/src/components/ChatMarkdown.permissions.browser.test.tsx‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ vi.mock(import("../hooks/useLocalStorage"), async (original) => ({
9090
}));
9191
vi.mock(import("../localApi"), async (original) => ({
9292
...(await original()),
93-
readLocalApi: () =>
94-
({
95-
contextMenu: { show: state.choose },
96-
shell: { openExternal: state.openExternal },
97-
}) as never,
93+
readLocalApi: () => ({ shell: { openExternal: state.openExternal } }) as never,
94+
}));
95+
vi.mock(import("./chat/useMessageLinkMenu"), () => ({
96+
useMessageLinkMenu: () => ({ show: state.choose, menu: null }) as never,
9897
}));
9998
vi.mock(import("~/lib/openPullRequestLink"), async (original) => ({
10099
...(await original()),
@@ -202,7 +201,7 @@ it("removes host actions after revocation while keeping file preview and copying
202201
expect(state.openEditor).not.toHaveBeenCalled();
203202
});
204203

205-
it("does not launch an editor after revocation during a native context menu", async () => {
204+
it("does not launch an editor after revocation while its link menu is open", async () => {
206205
let choose: (action: string) => void = () => {
207206
throw new Error("Menu not opened");
208207
};
@@ -258,7 +257,7 @@ it.each([false, true])(
258257
);
259258

260259
it.each(["link-to-thread", "unlink-from-thread"])(
261-
"rechecks %s after a native context menu returns",
260+
"rechecks %s after its link menu returns",
262261
async (action) => {
263262
state.linkedPullRequest = action === "unlink-from-thread" ? linkedPullRequest : null;
264263
let choose: (action: string) => void = () => {

apps/web/src/components/chat/useMessageLinkMenu.test.tsx renamed to apps/web/src/components/chat/useMessageLinkMenu.browser.test.tsx

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,74 @@
1-
// @vitest-environment jsdom
2-
3-
import { act, createRef, useImperativeHandle } from "react";
1+
import { act } from "react";
42
import { createRoot, type Root } from "react-dom/client";
5-
import { create, type ReactTestRenderer } from "react-test-renderer";
63
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
74

85
import { useMessageLinkMenu } from "./useMessageLinkMenu";
96

10-
let renderer: ReactTestRenderer | undefined;
7+
describe("message link menu positioning", () => {
8+
let root: Root;
9+
let container: HTMLDivElement;
1110

12-
afterEach(async () => {
13-
await act(() => renderer?.unmount());
14-
renderer = undefined;
15-
vi.unstubAllGlobals();
16-
});
11+
beforeEach(() => {
12+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
13+
container = document.body.appendChild(document.createElement("div"));
14+
root = createRoot(container);
15+
});
16+
17+
afterEach(async () => {
18+
await act(async () => root.unmount());
19+
container.remove();
20+
vi.unstubAllGlobals();
21+
});
1722

18-
describe("message link menu positioning", () => {
1923
it.each([
2024
{ position: { x: 0, y: 0 }, expected: { x: 120, y: 260 } },
2125
{ position: { x: 145, y: 245 }, expected: { x: 145, y: 245 } },
2226
{ position: { x: 0, y: 245 }, expected: { x: 0, y: 245 } },
2327
{ position: { x: 145, y: 0 }, expected: { x: 145, y: 0 } },
24-
])("anchors a menu opened at $position to $expected", async ({ position, expected }) => {
25-
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
26-
vi.stubGlobal(
27-
"DOMRect",
28-
class {
29-
constructor(
30-
readonly x: number,
31-
readonly y: number,
32-
readonly width: number,
33-
readonly height: number,
34-
) {}
35-
},
36-
);
37-
const linkMenuRef = createRef<ReturnType<typeof useMessageLinkMenu>>();
28+
])("opens a menu requested at $position at $expected", async ({ position, expected }) => {
3829
function TestLinkMenu() {
39-
const linkMenu = useMessageLinkMenu();
40-
useImperativeHandle(linkMenuRef, () => linkMenu, [linkMenu]);
41-
return null;
30+
const { show, menu } = useMessageLinkMenu();
31+
return (
32+
<>
33+
<a
34+
href="https://example.com"
35+
style={{ position: "fixed", left: 120, top: 240, width: 80, height: 20 }}
36+
onContextMenu={(event) => {
37+
event.preventDefault();
38+
void show(
39+
[{ id: "copy-link", label: "Copy Link" }],
40+
{ x: event.clientX, y: event.clientY },
41+
event.currentTarget,
42+
);
43+
}}
44+
>
45+
Example
46+
</a>
47+
{menu}
48+
</>
49+
);
4250
}
43-
const trigger = {
44-
getBoundingClientRect: () => ({ left: 120, bottom: 260 }),
45-
} as HTMLElement;
51+
await act(async () => root.render(<TestLinkMenu />));
4652

47-
await act(() => {
48-
renderer = create(<TestLinkMenu />);
49-
});
50-
await act(() => {
51-
void linkMenuRef.current!.show([{ id: "copy-link", label: "Copy Link" }], position, trigger);
53+
// A keyboard-opened context menu reports (0, 0).
54+
await act(async () => {
55+
container.querySelector("a")!.dispatchEvent(
56+
new MouseEvent("contextmenu", {
57+
bubbles: true,
58+
cancelable: true,
59+
clientX: position.x,
60+
clientY: position.y,
61+
}),
62+
);
5263
});
5364

54-
// Evaluate the virtual anchor consumed by the popup's positioning engine.
55-
const popup = linkMenuRef.current!.menu?.props.children;
56-
expect(popup.props.anchor.getBoundingClientRect()).toMatchObject({
57-
...expected,
58-
width: 0,
59-
height: 0,
65+
// Base UI nudges the popup up to 5px off its anchor; the cases differ by far more.
66+
await vi.waitFor(() => {
67+
const menu = document.querySelector<HTMLElement>('[role="menu"]');
68+
expect(menu).not.toBeNull();
69+
const bounds = menu!.getBoundingClientRect();
70+
expect(Math.abs(bounds.left - expected.x)).toBeLessThanOrEqual(5);
71+
expect(Math.abs(bounds.top - expected.y)).toBeLessThanOrEqual(5);
6072
});
6173
});
6274
});

‎apps/web/src/components/chat/useMessageLinkMenu.tsx‎

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useRef, useState } from "react";
1+
import { useEffect, useRef, useState } from "react";
22

33
import { Menu, MenuItem, MenuPopup } from "../ui/menu";
44

@@ -44,39 +44,36 @@ export function useMessageLinkMenu() {
4444
const pending = useRef<((value: string | null) => void) | null>(null);
4545
// Set when Tab already moved focus past the link, so closing must not pull it back.
4646
const tabbedPast = useRef(false);
47-
const complete = useCallback((value: string | null) => {
47+
const complete = (value: string | null) => {
4848
const resolve = pending.current;
4949
pending.current = null;
5050
setState(null);
5151
resolve?.(value);
52-
}, []);
52+
};
5353

54-
useEffect(() => () => pending.current?.(null), []);
54+
useEffect(() => () => pending.current?.(null), [pending]);
5555

56-
const show = useCallback(
57-
<T extends string>(
58-
items: readonly { id: T; label: string }[],
59-
position: { x: number; y: number },
60-
trigger: HTMLElement,
61-
): Promise<T | null> => {
62-
pending.current?.(null);
63-
pending.current = null;
64-
if (items.length === 0) {
65-
setState(null);
66-
return Promise.resolve(null);
67-
}
68-
if (position.x === 0 && position.y === 0) {
69-
const bounds = trigger.getBoundingClientRect();
70-
position = { x: bounds.left, y: bounds.bottom };
71-
}
72-
return new Promise((resolve) => {
73-
tabbedPast.current = false;
74-
pending.current = (value) => resolve(value as T | null);
75-
setState({ items, position, trigger });
76-
});
77-
},
78-
[],
79-
);
56+
const show = <T extends string>(
57+
items: readonly { id: T; label: string }[],
58+
position: { x: number; y: number },
59+
trigger: HTMLElement,
60+
): Promise<T | null> => {
61+
pending.current?.(null);
62+
pending.current = null;
63+
if (items.length === 0) {
64+
setState(null);
65+
return Promise.resolve(null);
66+
}
67+
if (position.x === 0 && position.y === 0) {
68+
const bounds = trigger.getBoundingClientRect();
69+
position = { x: bounds.left, y: bounds.bottom };
70+
}
71+
return new Promise((resolve) => {
72+
tabbedPast.current = false;
73+
pending.current = (value) => resolve(value as T | null);
74+
setState({ items, position, trigger });
75+
});
76+
};
8077

8178
const menu = state && (
8279
<Menu

0 commit comments

Comments
 (0)