|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { resolveBrowserViewportLayout } from "./browserViewportLayout"; |
| 4 | +import { previewPageScale } from "./usePreviewPageScale"; |
| 5 | + |
| 6 | +// Electron's zoom steps are half a zoom level: factor 1.2 ** (level). |
| 7 | +const ZOOMED_IN_ONE_STEP = 1.2 ** 0.5; |
| 8 | +const ZOOMED_OUT_ONE_STEP = 1.2 ** -0.5; |
| 9 | + |
| 10 | +/** The guest's CSS width for a webview laid out at `hostWidth` window CSS pixels. */ |
| 11 | +const guestWidth = (hostWidth: number, windowZoomFactor: number) => hostWidth * windowZoomFactor; |
| 12 | + |
| 13 | +describe("previewPageScale", () => { |
| 14 | + it.each([ |
| 15 | + ["zoomed in", ZOOMED_IN_ONE_STEP], |
| 16 | + ["at actual size", 1], |
| 17 | + ["zoomed out", ZOOMED_OUT_ONE_STEP], |
| 18 | + ])("gives the page the requested viewport with the app window %s", (_, windowZoomFactor) => { |
| 19 | + const layout = resolveBrowserViewportLayout( |
| 20 | + { width: 1600, height: 1000 }, |
| 21 | + { _tag: "freeform", width: 1280, height: 800 }, |
| 22 | + previewPageScale(1, windowZoomFactor), |
| 23 | + ); |
| 24 | + |
| 25 | + expect(guestWidth(layout.viewportWidth, windowZoomFactor)).toBeCloseTo(1280); |
| 26 | + expect(guestWidth(layout.viewportHeight, windowZoomFactor)).toBeCloseTo(800); |
| 27 | + }); |
| 28 | + |
| 29 | + it("keeps the tab's own zoom on top of the window's", () => { |
| 30 | + expect(previewPageScale(1.5, ZOOMED_IN_ONE_STEP)).toBeCloseTo(1.5 / ZOOMED_IN_ONE_STEP); |
| 31 | + }); |
| 32 | + |
| 33 | + it("treats a missing or invalid window zoom as actual size", () => { |
| 34 | + expect(previewPageScale(1.25, Number.NaN)).toBe(1.25); |
| 35 | + expect(previewPageScale(1.25, 0)).toBe(1.25); |
| 36 | + }); |
| 37 | +}); |
0 commit comments