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

browserViewportLayout.test.ts

165 lines4.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 resizeBrowserViewportFromRail,
5 resizeFreeformViewport,
6 resolveBrowserDeviceViewportLayout,
7 resolveBrowserViewportLayout,
8 resolveResponsiveBrowserViewportSize,
9} from "./browserViewportLayout";
10​
11describe("resolveBrowserViewportLayout", () => {
12 it("fills the available surface in fill mode", () => {
13 expect(resolveBrowserViewportLayout({ width: 700, height: 500 }, { _tag: "fill" })).toEqual({
14 canvasWidth: 700,
15 canvasHeight: 500,
16 viewportX: 0,
17 viewportY: 0,
18 viewportWidth: 700,
19 viewportHeight: 500,
20 viewportScale: 1,
21 fillsPanel: true,
22 });
23 });
24​
25 it("centers a smaller fixed viewport", () => {
26 expect(
27 resolveBrowserViewportLayout(
28 { width: 700, height: 1000 },
29 { _tag: "freeform", width: 393, height: 852 },
30 ),
31 ).toMatchObject({
32 canvasWidth: 700,
33 canvasHeight: 1000,
34 viewportX: 154,
35 viewportY: 74,
36 viewportWidth: 393,
37 viewportHeight: 852,
38 });
39 });
40​
41 it("scales a larger fixed viewport down to fit without creating overflow", () => {
42 const layout = resolveBrowserViewportLayout(
43 { width: 600, height: 700 },
44 { _tag: "freeform", width: 1440, height: 900 },
45 );
46 expect(layout).toMatchObject({
47 canvasWidth: 600,
48 canvasHeight: 700,
49 viewportX: 0,
50 viewportY: 163,
51 viewportWidth: 600,
52 viewportHeight: 375,
53 });
54 expect(layout.viewportScale).toBeCloseTo(5 / 12);
55 });
56​
57 it("keeps fixed dimensions in page CSS pixels when browser zoom changes", () => {
58 expect(
59 resolveBrowserViewportLayout(
60 { width: 800, height: 700 },
61 { _tag: "freeform", width: 400, height: 300 },
62 1.5,
63 ),
64 ).toMatchObject({
65 viewportX: 100,
66 viewportY: 125,
67 viewportWidth: 600,
68 viewportHeight: 450,
69 });
70 expect(resizeFreeformViewport({ width: 400, height: 300 }, { x: 150, y: 75 }, 1.5)).toEqual({
71 width: 500,
72 height: 350,
73 });
74 });
75​
76 it("bounds freeform drag sizes and total render area", () => {
77 expect(resizeFreeformViewport({ width: 1024, height: 768 }, { x: -2000, y: -2000 })).toEqual({
78 width: 240,
79 height: 240,
80 });
81 const large = resizeFreeformViewport({ width: 1920, height: 1080 }, { x: 2000, y: 2000 });
82 expect(large.width * large.height).toBeLessThanOrEqual(3840 * 2160);
83 });
84​
85 it("resizes only the axes controlled by each edge", () => {
86 expect(
87 resizeFreeformViewport({ width: 800, height: 600 }, { x: -100, y: 500 }, 1, "west"),
88 ).toEqual({ width: 900, height: 600 });
89 expect(
90 resizeFreeformViewport({ width: 800, height: 600 }, { x: 500, y: 100 }, 1, "north"),
91 ).toEqual({ width: 800, height: 500 });
92 expect(
93 resizeFreeformViewport({ width: 800, height: 600 }, { x: -100, y: -50 }, 1, "northwest"),
94 ).toEqual({ width: 900, height: 650 });
95 });
96​
97 it("preserves a locked aspect ratio from either axis", () => {
98 expect(
99 resizeFreeformViewport({ width: 800, height: 600 }, { x: 200, y: 0 }, 1, "east", 4 / 3),
100 ).toEqual({ width: 1000, height: 750 });
101 expect(
102 resizeFreeformViewport({ width: 800, height: 600 }, { x: 0, y: 150 }, 1, "south", 4 / 3),
103 ).toEqual({ width: 1000, height: 750 });
104 });
105​
106 it("reserves persistent device-toolbar rails around the guest viewport", () => {
107 expect(
108 resolveBrowserDeviceViewportLayout(
109 { width: 1200, height: 900 },
110 { _tag: "freeform", width: 1180, height: 858 },
111 ),
112 ).toEqual({
113 canvasWidth: 1200,
114 canvasHeight: 900,
115 viewportX: 10,
116 viewportY: 32,
117 viewportWidth: 1180,
118 viewportHeight: 858,
119 viewportScale: 1,
120 fillsPanel: false,
121 });
122 });
123​
124 it("captures the available framed area when responsive mode is enabled", () => {
125 expect(resolveResponsiveBrowserViewportSize({ width: 1200, height: 900 })).toEqual({
126 width: 1180,
127 height: 858,
128 });
129 expect(resolveResponsiveBrowserViewportSize({ width: 1200, height: 900 }, 2)).toEqual({
130 width: 590,
131 height: 429,
132 });
133 });
134​
135 it("keeps the grabbed rail under the pointer across centered layout boundaries", () => {
136 const available = { width: 1120, height: 818 };
137 expect(
138 resizeBrowserViewportFromRail(
139 { width: 1120, height: 818 },
140 { x: -100, y: -50 },
141 available,
142 1,
143 "southeast",
144 ),
145 ).toEqual({ width: 920, height: 718 });
146 expect(
147 resizeBrowserViewportFromRail(
148 { width: 800, height: 600 },
149 { x: 300, y: 0 },
150 { width: 1200, height: 800 },
151 1,
152 "east",
153 ),
154 ).toEqual({ width: 1300, height: 600 });
155 expect(
156 resizeBrowserViewportFromRail(
157 { width: 560, height: 409 },
158 { x: -100, y: 0 },
159 available,
160 2,
161 "east",
162 ),
163 ).toEqual({ width: 460, height: 409 });
164 });
165});