Skip to content

Commit 8cd9641

Browse files
test(web): burst-merged jsdom tests run in a real browser (#21318)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e704190 commit 8cd9641

8 files changed

Lines changed: 35 additions & 115 deletions

apps/web/src/components/CommandPalette.focus.test.tsx renamed to apps/web/src/components/CommandPalette.focus.browser.test.tsx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @vitest-environment jsdom
2-
31
import { act, useImperativeHandle, useRef } from "react";
42
import { createRoot, type Root } from "react-dom/client";
53
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
@@ -73,18 +71,6 @@ let container: HTMLDivElement;
7371
beforeEach(() => {
7472
vi.useFakeTimers();
7573
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
76-
vi.stubGlobal(
77-
"ResizeObserver",
78-
class {
79-
observe() {}
80-
disconnect() {}
81-
unobserve() {}
82-
},
83-
);
84-
Object.defineProperty(Element.prototype, "getAnimations", {
85-
configurable: true,
86-
value: () => [],
87-
});
8874
container = document.createElement("div");
8975
document.body.append(container);
9076
root = createRoot(container);

apps/web/src/components/ComposerPromptEditorTiptap.input.test.tsx renamed to apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @vitest-environment jsdom
2-
31
import type { Editor } from "@tiptap/core";
42
import { act, createRef } from "react";
53
import { createRoot, type Root } from "react-dom/client";
@@ -43,7 +41,6 @@ async function mountEditor(value: string, richTextEnabled = true) {
4341
);
4442
const element = container.querySelector<HTMLElement & { editor: Editor }>(".tiptap");
4543
if (!element) throw new Error("Composer did not mount");
46-
element.editor.view.setProps({ handleScrollToSelection: () => true });
4744
return element.editor;
4845
}
4946

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @vitest-environment jsdom
2-
31
import { describe, expect, it } from "vite-plus/test";
42

53
import { sanitizeMermaidSvg } from "./MermaidDiagram";

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @vitest-environment jsdom
2-
31
import { act, memo, useRef } from "react";
42
import { createRoot, type Root } from "react-dom/client";
53
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";

apps/web/src/components/preview/PreviewPanelShell.a11y.test.tsx renamed to apps/web/src/components/preview/PreviewPanelShell.a11y.browser.test.tsx

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// @vitest-environment jsdom
2-
31
import { Dialog } from "@base-ui/react/dialog";
42
import { act } from "react";
53
import { createRoot, type Root } from "react-dom/client";
64
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5+
import { userEvent } from "vite-plus/test/browser";
76

87
import { PanelLayoutControls } from "../chat/PanelLayoutControls";
98
import { PreviewPanelShell, type PreviewPanelMode } from "./PreviewPanelShell";
@@ -13,15 +12,6 @@ let container: HTMLDivElement;
1312

1413
beforeEach(() => {
1514
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
16-
vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
17-
vi.stubGlobal(
18-
"ResizeObserver",
19-
class {
20-
observe() {}
21-
unobserve() {}
22-
disconnect() {}
23-
},
24-
);
2515
container = document.createElement("div");
2616
document.body.append(container);
2717
root = createRoot(container);
@@ -31,7 +21,6 @@ afterEach(async () => {
3121
await act(async () => root.unmount());
3222
container.remove();
3323
vi.unstubAllGlobals();
34-
vi.useRealTimers();
3524
});
3625

3726
async function renderPanel(
@@ -76,17 +65,20 @@ async function renderPanel(
7665
</Dialog.Root>,
7766
);
7867
});
79-
await act(async () => vi.advanceTimersToNextFrame());
68+
await act(nextFrame);
69+
}
70+
71+
function nextFrame() {
72+
return new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
8073
}
8174

75+
// Tabbing past the popup's last control wraps through Base UI's focus guard
76+
// to the first tabbable descendant, which skips anything inert.
8277
async function enterFocusScope() {
83-
// Base UI's focus guard models Tab wrapping into the scope. Its production
84-
// focus manager chooses the first tabbable descendant, including inert rules
85-
// that jsdom's native focus() does not implement.
86-
const guards = document.querySelectorAll<HTMLElement>("[data-base-ui-focus-guard]");
87-
expect(guards.length).toBe(2);
88-
await act(async () => guards[1]!.focus());
89-
await act(async () => vi.advanceTimersToNextFrame());
78+
const toggle = document.querySelector<HTMLElement>('[aria-label="Toggle right panel"]')!;
79+
await act(async () => toggle.focus());
80+
await act(() => userEvent.tab());
81+
await act(nextFrame);
9082
}
9183

9284
describe("right panel keyboard access", () => {

apps/web/src/components/pullRequest/PullRequestCandidatePicker.test.tsx renamed to apps/web/src/components/pullRequest/PullRequestCandidatePicker.browser.test.tsx

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// @vitest-environment jsdom
2-
31
import { act } from "react";
42
import { createRoot, type Root } from "react-dom/client";
53
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4+
import { userEvent } from "vite-plus/test/browser";
65

76
import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
87

@@ -11,14 +10,6 @@ let container: HTMLDivElement;
1110

1211
beforeEach(() => {
1312
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
14-
vi.stubGlobal(
15-
"ResizeObserver",
16-
class {
17-
observe() {}
18-
unobserve() {}
19-
disconnect() {}
20-
},
21-
);
2213
container = document.createElement("div");
2314
document.body.append(container);
2415
root = createRoot(container);
@@ -28,7 +19,6 @@ afterEach(async () => {
2819
await act(async () => root.unmount());
2920
container.remove();
3021
vi.unstubAllGlobals();
31-
vi.useRealTimers();
3222
});
3323

3424
describe("disabled candidate picker", () => {
@@ -69,27 +59,20 @@ describe("disabled candidate picker", () => {
6959
'button[aria-label="Request reviewers"]',
7060
)!;
7161
if (input === "keyboard") {
72-
await act(async () => {
73-
document.dispatchEvent(new KeyboardEvent("keydown", { key: "Tab", bubbles: true }));
74-
trigger.focus();
75-
});
62+
await act(() => userEvent.tab());
7663
expect(document.activeElement).toBe(trigger);
7764
} else {
78-
vi.useFakeTimers();
79-
await act(async () => {
80-
trigger.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
81-
trigger.dispatchEvent(new MouseEvent("mouseenter"));
82-
trigger.dispatchEvent(new MouseEvent("mousemove", { bubbles: true }));
83-
await vi.advanceTimersByTimeAsync(1000);
84-
});
65+
await act(() => userEvent.hover(trigger));
8566
}
86-
expect(
87-
document.querySelector('[data-slot="tooltip-popup"][data-open]')?.textContent,
88-
).toContain("Only collaborators can request reviewers.");
67+
await expect
68+
.poll(() => document.querySelector('[data-slot="tooltip-popup"][data-open]')?.textContent)
69+
.toContain("Only collaborators can request reviewers.");
8970
await act(async () => {
90-
trigger.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
91-
trigger.dispatchEvent(new KeyboardEvent("keydown", { key: " ", bubbles: true }));
92-
trigger.click();
71+
trigger.focus();
72+
await userEvent.keyboard("{Enter}");
73+
await userEvent.keyboard(" ");
74+
// Playwright skips aria-disabled targets; the click must still land to prove it is inert.
75+
await userEvent.click(trigger, { force: true });
9376
});
9477
expect(onOpenChange).not.toHaveBeenCalled();
9578
expect(onSelect).not.toHaveBeenCalled();

apps/web/src/components/settings/FontFamilyPicker.test.tsx renamed to apps/web/src/components/settings/FontFamilyPicker.browser.test.tsx

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// @vitest-environment jsdom
2-
31
import { act } from "react";
42
import { createRoot, type Root } from "react-dom/client";
53
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4+
import { userEvent } from "vite-plus/test/browser";
65

76
const families = vi.hoisted(() => {
87
const families = [
@@ -32,23 +31,6 @@ let container: HTMLDivElement;
3231

3332
beforeEach(async () => {
3433
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
35-
vi.stubGlobal(
36-
"ResizeObserver",
37-
class {
38-
observe() {}
39-
disconnect() {}
40-
unobserve() {}
41-
},
42-
);
43-
// jsdom has no layout; give LegendList a 30px viewport and 30px rows so it
44-
// renders a window of rows the way it does in a browser.
45-
vi.spyOn(Element.prototype, "getBoundingClientRect").mockReturnValue(
46-
DOMRect.fromRect({ width: 288, height: 30 }),
47-
);
48-
Object.defineProperty(Element.prototype, "getAnimations", {
49-
configurable: true,
50-
value: () => [],
51-
});
5234
container = document.createElement("div");
5335
document.body.append(container);
5436
root = createRoot(container);
@@ -68,14 +50,6 @@ function highlightedOptions(): string[] {
6850
);
6951
}
7052

71-
async function typeQuery(input: HTMLInputElement, value: string) {
72-
const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
73-
await act(async () => {
74-
setValue.call(input, value);
75-
input.dispatchEvent(new Event("input", { bubbles: true }));
76-
});
77-
}
78-
7953
describe("FontFamilyPicker", () => {
8054
it("highlights, moves through, and selects matches after filtering", async () => {
8155
const onSelect = vi.fn();
@@ -93,14 +67,11 @@ describe("FontFamilyPicker", () => {
9367

9468
const input = document.querySelector<HTMLInputElement>('input[placeholder="Search fonts…"]');
9569
expect(input).not.toBeNull();
96-
const press = (key: string) =>
97-
act(async () => {
98-
input!.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));
99-
});
70+
const press = (key: string) => act(() => userEvent.keyboard(`{${key}}`));
10071

10172
// Both matches were already rendered further down the unfiltered list.
102-
await typeQuery(input!, "co");
103-
expect(highlightedOptions()).toEqual(["Courier New"]);
73+
await act(() => userEvent.type(input!, "co"));
74+
await expect.poll(highlightedOptions).toEqual(["Courier New"]);
10475

10576
await press("ArrowDown");
10677
expect(highlightedOptions()).toEqual(["Fira Code"]);

apps/web/src/components/usage/UsageLimitsPooled.test.tsx renamed to apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @vitest-environment jsdom
2-
31
import {
42
EnvironmentId,
53
ProviderDriverKind,
@@ -10,6 +8,10 @@ import type { LimitPresentations } from "@t3tools/shared/usageLimits";
108
import { act } from "react";
119
import { createRoot, type Root } from "react-dom/client";
1210
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
11+
import { userEvent } from "vite-plus/test/browser";
12+
13+
// Real pointer clicks need the dialog's stacking styles to land above Base UI's modal overlay.
14+
import "~/index.css";
1315

1416
const consume = vi.hoisted(() => vi.fn());
1517
vi.mock("../../state/session", () => ({
@@ -71,14 +73,6 @@ let container: HTMLDivElement;
7173

7274
beforeEach(async () => {
7375
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
74-
vi.stubGlobal(
75-
"ResizeObserver",
76-
class {
77-
observe() {}
78-
disconnect() {}
79-
unobserve() {}
80-
},
81-
);
8276
consume.mockReset();
8377
container = document.createElement("div");
8478
document.body.append(container);
@@ -97,7 +91,7 @@ async function click(label: string) {
9791
(node) => node.textContent === label || node.getAttribute("aria-label")?.startsWith(label),
9892
);
9993
expect(button).toBeDefined();
100-
await act(() => button!.click());
94+
await act(() => userEvent.click(button!));
10195
}
10296

10397
it.each([
@@ -123,8 +117,9 @@ it.each([
123117
await click("Use reset");
124118
await click("Use credit");
125119
expect(consume).toHaveBeenCalledOnce();
126-
expect(document.querySelector('[role="alertdialog"]')).toBeNull();
127-
expect(document.querySelector('[data-slot="popover-popup"]')).toBeNull();
120+
// Both popups unmount once their exit animations finish.
121+
await expect.poll(() => document.querySelector('[role="alertdialog"]')).toBeNull();
122+
await expect.poll(() => document.querySelector('[data-slot="popover-popup"]')).toBeNull();
128123
const status = document.querySelector('[role="status"]');
129124
expect(status?.textContent).toBe("Alpha Using reset credit…");
130125
await click("Alpha:");

0 commit comments

Comments
 (0)