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