Skip to content

Commit c0f530e

Browse files
test(web): every React test runs in a real browser
Convert the last jsdom suites (GitActionsControl, its lifecycle suite, and the BranchToolbar panel) to browser tests that click through the real control, then drop jsdom and react-test-renderer from apps/web. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4b1024d commit c0f530e

13 files changed

Lines changed: 522 additions & 738 deletions

‎apps/web/package.json‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,13 @@
8484
"@types/mdast": "^4.0.4",
8585
"@types/react": "~19.3.0",
8686
"@types/react-dom": "~19.2.3",
87-
"@types/react-test-renderer": "19.1.0",
8887
"@types/use-sync-external-store": "^0.0.6",
8988
"@vercel/config": "^0.3.0",
9089
"@vitejs/plugin-react": "^6.1.2",
9190
"@vitest/browser-playwright": "5.0.1",
9291
"compression": "^1.8.2",
93-
"jsdom": "^30.0.1",
9492
"oxc-transform-react": "~0.152.0",
9593
"playwright": "1.60.0",
96-
"react-test-renderer": "19.2.6",
9794
"tailwindcss": "^4.0.0",
9895
"unified": "catalog:",
9996
"vite": "catalog:",
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2+
import { act, type ComponentProps } from "react";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5+
import { page, userEvent } from "vite-plus/test/browser";
6+
7+
import "~/index.css";
8+
9+
const state = vi.hoisted(() => ({
10+
worktreePath: "/tmp/worktree" as string | null,
11+
showContextMenu: vi.fn(),
12+
writeTextToClipboard: vi.fn(),
13+
}));
14+
15+
vi.mock(import("../localApi"), async (original) => ({
16+
...(await original()),
17+
readLocalApi: (() => ({ contextMenu: { show: state.showContextMenu } })) as never,
18+
}));
19+
vi.mock(import("../hooks/useCopyToClipboard"), async (original) => ({
20+
...(await original()),
21+
writeTextToClipboard: state.writeTextToClipboard,
22+
}));
23+
vi.mock(import("./BranchToolbarBranchSelector"), async (original) => ({
24+
...(await original()),
25+
BranchToolbarBranchSelector: (() => null) as never,
26+
}));
27+
vi.mock(import("../state/entities"), async (original) => ({
28+
...(await original()),
29+
useQuietThreadShell: (() => ({
30+
environmentId: "local",
31+
projectId: "project",
32+
worktreePath: state.worktreePath,
33+
})) as never,
34+
useProject: (() => ({ workspaceRoot: "/tmp/project" })) as never,
35+
useThreadShellsForProjectRefs: () => [],
36+
}));
37+
38+
import { BranchToolbar } from "./BranchToolbar";
39+
40+
let root: Root;
41+
let container: HTMLDivElement;
42+
43+
beforeEach(() => {
44+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
45+
state.worktreePath = "/tmp/worktree";
46+
state.showContextMenu.mockReset().mockResolvedValue("copy-path");
47+
state.writeTextToClipboard.mockReset().mockResolvedValue(true);
48+
container = document.createElement("div");
49+
document.body.append(container);
50+
root = createRoot(container);
51+
});
52+
53+
afterEach(async () => {
54+
await act(async () => root.unmount());
55+
container.remove();
56+
vi.unstubAllGlobals();
57+
});
58+
59+
function render(props: Partial<ComponentProps<typeof BranchToolbar>>) {
60+
return act(async () =>
61+
root.render(
62+
<BranchToolbar
63+
layout="panel"
64+
panelSection="workspace"
65+
environmentId={EnvironmentId.make("local")}
66+
threadId={ThreadId.make("thread")}
67+
showGitControls
68+
envMode="local"
69+
envLocked={false}
70+
startFromOrigin={false}
71+
onStartFromOriginChange={vi.fn()}
72+
onEnvModeChange={vi.fn()}
73+
{...props}
74+
/>,
75+
),
76+
);
77+
}
78+
79+
it("keeps machine choices usable when the combined row's workspace is locked", async () => {
80+
const onEnvironmentChange = vi.fn();
81+
await render({
82+
onEnvironmentChange,
83+
availableEnvironments: ["local", "remote"].map((id) => ({
84+
environmentId: EnvironmentId.make(id),
85+
projectId: ProjectId.make("project"),
86+
label: id,
87+
isPrimary: id === "local",
88+
machine: "server",
89+
})),
90+
});
91+
const trigger = container.querySelector("button")!;
92+
expect(trigger.textContent).toBe("local");
93+
expect(container.querySelectorAll("button")).toHaveLength(1);
94+
95+
// A ctrl-click is a macOS secondary click; it must not open the menu.
96+
await act(async () => {
97+
trigger.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, ctrlKey: true }));
98+
});
99+
expect(document.querySelector('[role="menu"]')).toBeNull();
100+
101+
await userEvent.click(trigger);
102+
await expect
103+
.element(page.getByRole("menuitemradio", { name: "New worktree" }))
104+
.toHaveAttribute("aria-disabled", "true");
105+
await userEvent.click(page.getByRole("menuitemradio", { name: "remote" }));
106+
expect(onEnvironmentChange).toHaveBeenCalledWith("remote");
107+
});
108+
109+
it.each([
110+
["local", null, "/tmp/project"],
111+
["worktree", null, null],
112+
["worktree", "/tmp/worktree", "/tmp/worktree"],
113+
] as const)(
114+
"copies only an existing workspace path with mode %s and worktree %s",
115+
async (envMode, worktreePath, copiedPath) => {
116+
state.worktreePath = worktreePath;
117+
await render({ envMode });
118+
const event = new MouseEvent("contextmenu", { bubbles: true, cancelable: true });
119+
await act(async () => {
120+
container.querySelector('[aria-label="Run context"]')!.dispatchEvent(event);
121+
});
122+
expect(event.defaultPrevented).toBe(copiedPath !== null);
123+
if (copiedPath === null) {
124+
expect(state.showContextMenu).not.toHaveBeenCalled();
125+
expect(state.writeTextToClipboard).not.toHaveBeenCalled();
126+
} else {
127+
await vi.waitFor(() =>
128+
expect(state.writeTextToClipboard).toHaveBeenCalledWith(copiedPath, "workspace path"),
129+
);
130+
}
131+
},
132+
);

‎apps/web/src/components/BranchToolbar.panel.test.tsx‎

Lines changed: 0 additions & 138 deletions
This file was deleted.

0 commit comments

Comments
 (0)