Skip to content

Commit 30c2a98

Browse files
fix(web): update panel tests for compiled browser runs
Use React DOM and the browser test project after removal of react-test-renderer. Keep mock terminal surfaces non-thenable and express Device cleanup with compiler-supported promise callbacks.
1 parent 090f510 commit 30c2a98

9 files changed

Lines changed: 161 additions & 108 deletions

apps/web/src/components/RightPanelTabs.browserProfile.test.tsx renamed to apps/web/src/components/RightPanelTabs.browserProfile.browser.test.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// @vitest-environment jsdom
2-
31
import {
42
BUILT_IN_BROWSER_PROFILES,
53
DEFAULT_BROWSER_PROFILE_ID,
64
INCOGNITO_BROWSER_PROFILE_ID,
75
} from "@t3tools/contracts";
86
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
97
import { act } from "react";
8+
import { userEvent } from "vite-plus/test/browser";
109
import { createRoot, type Root } from "react-dom/client";
1110
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
1211

@@ -24,7 +23,7 @@ let opened: string[];
2423

2524
beforeEach(() => {
2625
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
27-
// Browser is desktop-only; the launcher offers it once the preview bridge exists.
26+
// Expose the preview bridge so the launcher offers Browser.
2827
vi.stubGlobal("desktopBridge", { preview: {} });
2928
vi.stubGlobal(
3029
"ResizeObserver",
@@ -34,8 +33,6 @@ beforeEach(() => {
3433
disconnect() {}
3534
},
3635
);
37-
// jsdom lacks the Web Animations API that the tab bar's scroll area waits on.
38-
Element.prototype.getAnimations ??= () => [];
3936
opened = [];
4037
container = document.createElement("div");
4138
document.body.append(container);
@@ -118,11 +115,7 @@ function menuItem(label: string): HTMLElement {
118115

119116
async function click(element: Element) {
120117
await act(async () => {
121-
element.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));
122-
element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
123-
element.dispatchEvent(new MouseEvent("pointerup", { bubbles: true }));
124-
element.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
125-
element.dispatchEvent(new MouseEvent("click", { bubbles: true }));
118+
await userEvent.click(element);
126119
});
127120
}
128121

apps/web/src/components/RightPanelTabs.terminal.test.tsx renamed to apps/web/src/components/RightPanelTabs.terminal.browser.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @vitest-environment jsdom
2-
31
import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
42
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
53
import { act } from "react";
@@ -28,8 +26,6 @@ beforeEach(() => {
2826
disconnect() {}
2927
},
3028
);
31-
// jsdom lacks the Web Animations API that the tab bar's scroll area waits on.
32-
Element.prototype.getAnimations ??= () => [];
3329
useRightPanelStore.setState({ byThreadKey: {} });
3430
container = document.createElement("div");
3531
document.body.append(container);

apps/web/src/panels/bundledPanels.test.tsx renamed to apps/web/src/panels/bundledPanels.browser.test.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
22
import { act, Suspense } from "react";
3-
import { create } from "react-test-renderer";
4-
import { describe, expect, it, vi } from "vite-plus/test";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
55

66
const loaded = vi.hoisted(() => ({
77
diff: 0,
@@ -49,11 +49,25 @@ const host: PanelHost = {
4949
sendAnnotation: () => undefined,
5050
};
5151

52+
let root: Root;
53+
let container: HTMLDivElement;
54+
beforeEach(() => {
55+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
56+
container = document.createElement("div");
57+
document.body.append(container);
58+
root = createRoot(container);
59+
});
60+
afterEach(async () => {
61+
await act(() => root.unmount());
62+
container.remove();
63+
vi.unstubAllGlobals();
64+
});
65+
5266
describe("bundled side panels", () => {
5367
it("loads only the selected panel body and lends it the host", async () => {
5468
expect(loaded).toMatchObject({ diff: 0, preview: 0, terminal: 0, device: 0 });
5569
await act(async () => {
56-
create(
70+
root.render(
5771
<PanelHostContext value={host}>
5872
<Suspense fallback={null}>
5973
<RegisteredSidePanel id="preview" tabId="tab-1" />
@@ -74,7 +88,7 @@ describe("bundled side panels", () => {
7488
activeTerminalId: "term-1",
7589
};
7690
await act(async () => {
77-
create(
91+
root.render(
7892
<PanelHostContext value={host}>
7993
<Suspense fallback={null}>
8094
<RegisteredSidePanel

apps/web/src/panels/device/DeviceSidePanel.test.tsx renamed to apps/web/src/panels/device/DeviceSidePanel.browser.test.tsx

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
type ScopedThreadRef,
66
} from "@t3tools/contracts";
77
import { act, type ReactNode } from "react";
8-
import { create, type ReactTestRenderer } from "react-test-renderer";
9-
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
8+
import { createRoot, type Root } from "react-dom/client";
9+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
1010

1111
import type { RightPanelSurface } from "~/rightPanelStore";
1212

@@ -79,7 +79,11 @@ vi.mock("~/rightPanelStore", () => ({
7979
}));
8080
vi.mock("~/components/device/DeviceHostUpdates", () => ({ DeviceHostUpdates: () => null }));
8181
vi.mock("~/components/device/DeviceLoadingView", () => ({ DeviceLoadingView: () => null }));
82-
vi.mock("~/components/device/DeviceWorkspace", () => ({ DeviceWorkspace: () => null }));
82+
vi.mock("~/components/device/DeviceWorkspace", () => ({
83+
DeviceWorkspace: ({ onPowerOff }: { onPowerOff: () => void }) => (
84+
<button onClick={onPowerOff}>Power off</button>
85+
),
86+
}));
8387
vi.mock("~/components/preview/PreviewPanelShell", () => ({
8488
PreviewPanelShell: ({ children }: { children: ReactNode }) => children,
8589
}));
@@ -118,36 +122,49 @@ const render = (threadRef: ScopedThreadRef, surface = picker) => (
118122
</PanelHostContext>
119123
);
120124
const mount = async (threadRef: ScopedThreadRef, surface = picker) => {
121-
const renderer = await act(async () => create(render(threadRef, surface)));
125+
await act(async () => root.render(render(threadRef, surface)));
122126
// Wait for the registered lazy body to load and replace the Suspense fallback.
123127
await act(async () => {
124128
await import("./DeviceSidePanel");
125129
});
126-
return renderer;
127130
};
128131
const deferred = () => {
129132
let settle!: (result: CommandResult) => void;
130133
const promise = new Promise<CommandResult>((resolve) => (settle = resolve));
131134
return { promise, settle };
132135
};
133-
const startPhone = (renderer: ReactTestRenderer) =>
136+
const startPhone = () =>
134137
act(async () => {
135-
renderer.root.findByProps({ "aria-label": "Start Phone" }).props.onClick();
138+
container.querySelector<HTMLButtonElement>('[aria-label="Start Phone"]')!.click();
136139
});
137-
const errors = (renderer: ReactTestRenderer) => renderer.root.findAllByProps({ role: "alert" });
140+
const errors = () => container.querySelectorAll('[role="alert"]');
138141
const opened = { _tag: "Success", value: { hostId: "local", deviceId: "phone" } } as const;
139142

140143
beforeEach(() => {
141144
vi.clearAllMocks();
142145
});
143146

147+
let root: Root;
148+
let container: HTMLDivElement;
149+
beforeEach(() => {
150+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
151+
container = document.createElement("div");
152+
document.body.append(container);
153+
root = createRoot(container);
154+
});
155+
afterEach(async () => {
156+
await act(() => root.unmount());
157+
container.remove();
158+
vi.unstubAllGlobals();
159+
});
160+
144161
describe("registered Device panel", () => {
145162
it("opens a picked device when the pick settles in the same thread", async () => {
146163
const pick = deferred();
147164
mocks.open.mockReturnValue(pick.promise);
148-
const renderer = await mount(picking);
165+
await mount(picking);
149166

150-
await startPhone(renderer);
167+
await startPhone();
151168
expect(mocks.open).toHaveBeenCalledWith({
152169
environmentId: picking.environmentId,
153170
input: { threadId: picking.threadId, hostId: "local", deviceId: "phone", platform: "ios" },
@@ -164,21 +181,23 @@ describe("registered Device panel", () => {
164181

165182
it("keeps the next thread's picker usable while an earlier pick is pending", async () => {
166183
mocks.open.mockReturnValue(deferred().promise);
167-
const renderer = await mount(picking);
184+
await mount(picking);
168185

169-
await startPhone(renderer);
170-
await act(async () => renderer.update(render(next)));
186+
await startPhone();
187+
await act(async () => root.render(render(next)));
171188

172-
expect(renderer.root.findByProps({ "aria-label": "Start Phone" }).props.disabled).toBe(false);
189+
expect(container.querySelector<HTMLButtonElement>('[aria-label="Start Phone"]')!.disabled).toBe(
190+
false,
191+
);
173192
});
174193

175194
it("opens a pick that settles after a switch in the thread it started in", async () => {
176195
const pick = deferred();
177196
mocks.open.mockReturnValue(pick.promise);
178-
const renderer = await mount(picking);
197+
await mount(picking);
179198

180-
await startPhone(renderer);
181-
await act(async () => renderer.update(render(next)));
199+
await startPhone();
200+
await act(async () => root.render(render(next)));
182201
await act(async () => pick.settle(opened));
183202

184203
expect(mocks.openDevice).toHaveBeenCalledExactlyOnceWith(picking, {
@@ -187,58 +206,60 @@ describe("registered Device panel", () => {
187206
platform: "ios",
188207
name: "Phone",
189208
});
190-
expect(renderer.root.findByProps({ "aria-label": "Start Phone" }).props.disabled).toBe(false);
209+
expect(container.querySelector<HTMLButtonElement>('[aria-label="Start Phone"]')!.disabled).toBe(
210+
false,
211+
);
191212
});
192213

193214
it("hides a failed pick that settles after the panel moved, even back again", async () => {
194215
const pick = deferred();
195216
mocks.open.mockReturnValue(pick.promise);
196-
const renderer = await mount(picking);
217+
await mount(picking);
197218

198-
await startPhone(renderer);
199-
await act(async () => renderer.update(render(next)));
200-
await act(async () => renderer.update(render(picking)));
219+
await startPhone();
220+
await act(async () => root.render(render(next)));
221+
await act(async () => root.render(render(picking)));
201222
await act(async () => pick.settle({ _tag: "Failure", cause: new Error("boom") }));
202223

203-
expect(errors(renderer)).toHaveLength(0);
224+
expect(errors()).toHaveLength(0);
204225
expect(mocks.openDevice).not.toHaveBeenCalled();
205226
});
206227

207228
it("does not carry an operation error into another thread", async () => {
208229
mocks.open.mockResolvedValue({ _tag: "Failure", cause: new Error("boom") });
209-
const renderer = await mount(picking);
230+
await mount(picking);
210231

211-
await startPhone(renderer);
212-
expect(errors(renderer)).toHaveLength(1);
213-
await act(async () => renderer.update(render(next)));
232+
await startPhone();
233+
expect(errors()).toHaveLength(1);
234+
await act(async () => root.render(render(next)));
214235

215-
expect(errors(renderer)).toHaveLength(0);
236+
expect(errors()).toHaveLength(0);
216237
});
217238

218239
it("closes a powered-off surface in its own thread after the panel moved", async () => {
219240
const powerOff = deferred();
220241
mocks.close.mockReturnValue(powerOff.promise);
221-
const renderer = await mount(picking, streaming);
242+
await mount(picking, streaming);
222243

223244
await act(async () => {
224-
renderer.root.findByProps({ hostLabel: "This Mac" }).props.onPowerOff();
245+
container.querySelector<HTMLButtonElement>("button")!.click();
225246
});
226247
expect(mocks.close).toHaveBeenCalledWith({
227248
environmentId: picking.environmentId,
228249
input: { threadId: picking.threadId, hostId: "local", deviceId: "phone", shutdown: true },
229250
});
230-
await act(async () => renderer.update(render(next, streaming)));
251+
await act(async () => root.render(render(next, streaming)));
231252
await act(async () => powerOff.settle(opened));
232253

233254
expect(mocks.closeSurface).toHaveBeenCalledExactlyOnceWith(picking, streaming.id);
234255
});
235256

236257
it("closes the surface when a power-off settles in the same thread", async () => {
237258
mocks.close.mockResolvedValue(opened);
238-
const renderer = await mount(picking, streaming);
259+
await mount(picking, streaming);
239260

240261
await act(async () => {
241-
renderer.root.findByProps({ hostLabel: "This Mac" }).props.onPowerOff();
262+
container.querySelector<HTMLButtonElement>("button")!.click();
242263
});
243264

244265
expect(mocks.closeSurface).toHaveBeenCalledExactlyOnceWith(picking, streaming.id);

‎apps/web/src/panels/device/DeviceSidePanel.tsx‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,37 +78,38 @@ export default function DeviceSidePanel(props: {
7878

7979
const grouped = groupDevices(state);
8080

81-
const selectDevice = async (value: string) => {
81+
const selectDevice = (value: string) => {
8282
const device = state.devices.find((candidate) => deviceKey(candidate) === value);
8383
if (!device) return;
8484
setOperationError(null);
8585
setPendingDevice(device);
8686
const stillCurrent = isCurrentScope();
87-
try {
88-
const result = await open({
89-
environmentId,
90-
input: {
91-
threadId,
92-
hostId: device.hostId,
93-
deviceId: device.id,
94-
platform: device.platform,
95-
},
87+
return open({
88+
environmentId,
89+
input: {
90+
threadId,
91+
hostId: device.hostId,
92+
deviceId: device.id,
93+
platform: device.platform,
94+
},
95+
})
96+
.then((result) => {
97+
// The server opened the device for the starting thread, so its tab opens
98+
// there even after a switch; only this panel's own state is scope-guarded.
99+
if (result._tag === "Failure") {
100+
if (stillCurrent()) setOperationError(formatEnvironmentQueryError(result.cause));
101+
} else {
102+
useRightPanelStore.getState().openDevice(threadRef, {
103+
hostId: result.value.hostId,
104+
deviceId: result.value.deviceId,
105+
platform: device.platform,
106+
name: device.name,
107+
});
108+
}
109+
})
110+
.finally(() => {
111+
if (stillCurrent()) setPendingDevice(null);
96112
});
97-
// The server opened the device for the starting thread, so its tab opens
98-
// there even after a switch; only this panel's own state is scope-guarded.
99-
if (result._tag === "Failure") {
100-
if (stillCurrent()) setOperationError(formatEnvironmentQueryError(result.cause));
101-
} else {
102-
useRightPanelStore.getState().openDevice(threadRef, {
103-
hostId: result.value.hostId,
104-
deviceId: result.value.deviceId,
105-
platform: device.platform,
106-
name: device.name,
107-
});
108-
}
109-
} finally {
110-
if (stillCurrent()) setPendingDevice(null);
111-
}
112113
};
113114

114115
const floatActive = () => {

0 commit comments

Comments
 (0)