Skip to content

Commit f533d74

Browse files
Ta-noshiiclaude
andcommitted
test(web): cover window zoom changes after the preview mounts
The layout zoom hook now subscribes to window resize, and the hosted webview test stubs window as globalThis without event methods, so it threw on mount. Stub addEventListener and removeEventListener there. Add a hook test that changes the bridge's window zoom after mount, dispatches resize, and checks the layout factor follows it, plus the fallback for desktop shells without getWindowZoomFactor. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 402ccc0 commit f533d74

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎apps/web/src/browser/HostedBrowserWebview.test.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ beforeEach(() => {
7777
});
7878
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
7979
vi.stubGlobal("window", globalThis);
80+
vi.stubGlobal("addEventListener", vi.fn());
81+
vi.stubGlobal("removeEventListener", vi.fn());
8082
vi.stubGlobal("navigator", { platform: "Linux" });
8183
vi.stubGlobal(
8284
"requestAnimationFrame",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { act } from "react";
2+
import { create, type ReactTestRenderer } from "react-test-renderer";
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4+
5+
import { useBrowserLayoutZoomFactor } from "./useBrowserLayoutZoomFactor";
6+
7+
let windowZoomFactor = 1;
8+
let renderer: ReactTestRenderer | undefined;
9+
const seen: number[] = [];
10+
11+
function Probe(props: { readonly previewZoomFactor: number }) {
12+
seen.push(useBrowserLayoutZoomFactor(props.previewZoomFactor));
13+
return null;
14+
}
15+
16+
beforeEach(() => {
17+
windowZoomFactor = 1;
18+
seen.length = 0;
19+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
20+
vi.stubGlobal(
21+
"window",
22+
Object.assign(new EventTarget(), {
23+
desktopBridge: { getWindowZoomFactor: () => windowZoomFactor },
24+
}),
25+
);
26+
});
27+
28+
afterEach(async () => {
29+
await act(() => renderer?.unmount());
30+
renderer = undefined;
31+
vi.unstubAllGlobals();
32+
});
33+
34+
describe("useBrowserLayoutZoomFactor", () => {
35+
it("follows an app window zoom change made after mount", async () => {
36+
await act(() => {
37+
renderer = create(<Probe previewZoomFactor={1} />);
38+
});
39+
expect(seen.at(-1)).toBe(1);
40+
41+
// View > Zoom In once, which Chromium reports to the page as a resize.
42+
windowZoomFactor = 1.2 ** 0.5;
43+
await act(() => {
44+
window.dispatchEvent(new Event("resize"));
45+
});
46+
expect(seen.at(-1)).toBeCloseTo(1 / 1.2 ** 0.5, 6);
47+
48+
windowZoomFactor = 1;
49+
await act(() => {
50+
window.dispatchEvent(new Event("resize"));
51+
});
52+
expect(seen.at(-1)).toBe(1);
53+
});
54+
55+
it("falls back to the preview zoom alone when the desktop shell lacks the bridge method", async () => {
56+
vi.stubGlobal("window", new EventTarget());
57+
await act(() => {
58+
renderer = create(<Probe previewZoomFactor={1.25} />);
59+
});
60+
expect(seen.at(-1)).toBe(1.25);
61+
});
62+
});

0 commit comments

Comments
 (0)