Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BrowserDeviceToolbar.test.ts

49 lines1.7 KB

1import type { PreviewViewportSetting } from "@v12/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4import {
5 commitViewportAndAspectRatio,
6 reconcileLockedAspectRatio,
7} from "./browserDeviceToolbarState";
8​
9describe("commitViewportAndAspectRatio", () => {
10 it("commits the aspect ratio only after the viewport succeeds", async () => {
11 let resolveChange: (() => void) | undefined;
12 const onChange = vi.fn(
13 () =>
14 new Promise<void>((resolve) => {
15 resolveChange = resolve;
16 }),
17 );
18 const onAspectRatioChange = vi.fn();
19 const setting: PreviewViewportSetting = { _tag: "freeform", width: 900, height: 600 };
20​
21 const commit = commitViewportAndAspectRatio(setting, 1.5, onChange, onAspectRatioChange);
22 expect(onAspectRatioChange).not.toHaveBeenCalled();
23​
24 resolveChange?.();
25 await commit;
26 expect(onAspectRatioChange).toHaveBeenCalledWith(1.5);
27 });
28​
29 it("keeps the previous aspect ratio when the viewport commit fails", async () => {
30 const onAspectRatioChange = vi.fn();
31 await expect(
32 commitViewportAndAspectRatio(
33 { _tag: "fill" },
34 null,
35 async () => Promise.reject(new Error("resize failed")),
36 onAspectRatioChange,
37 ),
38 ).rejects.toThrow("resize failed");
39 expect(onAspectRatioChange).not.toHaveBeenCalled();
40 });
41});
42​
43describe("reconcileLockedAspectRatio", () => {
44 it("tracks external viewport ratios only while the lock remains active", () => {
45 expect(reconcileLockedAspectRatio(1.5, 16 / 9)).toBe(16 / 9);
46 expect(reconcileLockedAspectRatio(null, 16 / 9)).toBeNull();
47 expect(reconcileLockedAspectRatio(1.5, null)).toBeNull();
48 });
49});