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

previewMiniPlayerLayout.test.ts

214 lines6.1 KB

1import { FILL_PREVIEW_VIEWPORT } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 clampPreviewMiniPlayerPosition,
6 defaultPreviewMiniPlayerPosition,
7 PREVIEW_MINI_PLAYER_EDGE_GAP,
8 resizePreviewMiniPlayer,
9 resolvePreviewMiniPlayerFrame,
10 resolvePreviewMiniPlayerSourceSize,
11} from "./previewMiniPlayerLayout";
12​
13const container = { width: 1_000, height: 700 };
14const source = { width: 1_600, height: 1_000 };
15​
16describe("resolvePreviewMiniPlayerSourceSize", () => {
17 it("uses the device viewport scaled by zoom", () => {
18 expect(
19 resolvePreviewMiniPlayerSourceSize({ _tag: "freeform", width: 390, height: 844 }, null, 2),
20 ).toEqual({ width: 780, height: 1_688 });
21 });
22​
23 it("uses the size the fill viewport had when it was floated", () => {
24 expect(
25 resolvePreviewMiniPlayerSourceSize(
26 FILL_PREVIEW_VIEWPORT,
27 { x: 0, y: 0, width: 640, height: 400, scale: 0.5, scrollLeft: 0, scrollTop: 0 },
28 1,
29 ),
30 ).toEqual({ width: 1_280, height: 800 });
31 });
32});
33​
34describe("resolvePreviewMiniPlayerFrame", () => {
35 it("opens at the source aspect ratio in the top-right corner", () => {
36 expect(
37 resolvePreviewMiniPlayerFrame({ width: null, position: null, source, container }),
38 ).toEqual({ x: 668, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 320, height: 200 });
39 });
40​
41 it("keeps a tall source at the minimum width instead of the default box", () => {
42 expect(
43 resolvePreviewMiniPlayerFrame({
44 width: null,
45 position: null,
46 source: { width: 390, height: 844 },
47 container,
48 }),
49 ).toEqual({ x: 748, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 240, height: 519 });
50 });
51​
52 it("derives height from the stored width", () => {
53 expect(
54 resolvePreviewMiniPlayerFrame({ width: 480, position: { x: 100, y: 80 }, source, container }),
55 ).toEqual({ x: 100, y: 80, width: 480, height: 300 });
56 });
57​
58 it("shrinks to the space above the composer without losing the stored width", () => {
59 const frame = resolvePreviewMiniPlayerFrame({
60 width: 800,
61 position: { x: 100, y: 80 },
62 source,
63 container,
64 bottomInset: 300,
65 });
66 expect(frame).toEqual({ x: 100, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 602, height: 376 });
67 });
68​
69 it("never grows past the source's own rendered size", () => {
70 expect(
71 resolvePreviewMiniPlayerFrame({
72 width: 900,
73 position: { x: 12, y: 12 },
74 source: { width: 480, height: 320 },
75 container,
76 }),
77 ).toMatchObject({ width: 480, height: 320 });
78 });
79});
80​
81describe("resizePreviewMiniPlayer", () => {
82 const start = { x: 300, y: 200, width: 320, height: 200 };
83​
84 it("keeps the aspect ratio when dragging the right edge", () => {
85 expect(
86 resizePreviewMiniPlayer({
87 start,
88 direction: "east",
89 delta: { x: 160, y: 0 },
90 source,
91 container,
92 }),
93 ).toEqual({ x: 300, y: 200, width: 480, height: 300 });
94 });
95​
96 it("anchors the right edge when dragging from the left", () => {
97 expect(
98 resizePreviewMiniPlayer({
99 start,
100 direction: "west",
101 delta: { x: -160, y: 0 },
102 source,
103 container,
104 }),
105 ).toEqual({ x: 140, y: 200, width: 480, height: 300 });
106 });
107​
108 it("anchors the bottom edge when dragging the top up", () => {
109 expect(
110 resizePreviewMiniPlayer({
111 start,
112 direction: "north",
113 delta: { x: 0, y: -100 },
114 source,
115 container,
116 }),
117 ).toEqual({ x: 300, y: 100, width: 480, height: 300 });
118 });
119​
120 it("follows the dominant axis on a corner drag", () => {
121 expect(
122 resizePreviewMiniPlayer({
123 start,
124 direction: "southeast",
125 delta: { x: 20, y: 100 },
126 source,
127 container,
128 }),
129 ).toEqual({ x: 300, y: 200, width: 480, height: 300 });
130 });
131​
132 it("stops at the container edge in the drag direction", () => {
133 expect(
134 resizePreviewMiniPlayer({
135 start: { x: 600, y: 12, width: 320, height: 200 },
136 direction: "east",
137 delta: { x: 500, y: 0 },
138 source,
139 container,
140 }),
141 ).toEqual({ x: 600, y: 12, width: 388, height: 243 });
142 });
143​
144 it("shifts the player when the free axis would overflow", () => {
145 expect(
146 resizePreviewMiniPlayer({
147 start: { x: 12, y: 400, width: 320, height: 200 },
148 direction: "east",
149 delta: { x: 300, y: 0 },
150 source,
151 container,
152 bottomInset: 0,
153 }),
154 ).toEqual({ x: 12, y: 300, width: 620, height: 388 });
155 });
156​
157 it("respects the minimum size", () => {
158 expect(
159 resizePreviewMiniPlayer({
160 start,
161 direction: "southeast",
162 delta: { x: -300, y: -300 },
163 source,
164 container,
165 }),
166 ).toEqual({ x: 300, y: 200, width: 240, height: 150 });
167 });
168});
169​
170describe("defaultPreviewMiniPlayerPosition", () => {
171 it("keeps the top-right fallback when the inline details panel is closed", () => {
172 expect(
173 defaultPreviewMiniPlayerPosition({
174 fallback: { x: 664, y: 16 },
175 parentRect: { left: 300, top: 60 },
176 playerWidth: 320,
177 detailsCardRect: null,
178 }),
179 ).toEqual({ x: 664, y: 16 });
180 });
181​
182 it("slides under the inline details card with right edges aligned", () => {
183 expect(
184 defaultPreviewMiniPlayerPosition({
185 fallback: { x: 664, y: 16 },
186 parentRect: { left: 300, top: 60 },
187 playerWidth: 320,
188 detailsCardRect: { right: 1280, bottom: 320 },
189 }),
190 ).toEqual({
191 x: 1280 - 300 - 320,
192 y: 320 - 60 + PREVIEW_MINI_PLAYER_EDGE_GAP,
193 });
194 });
195});
196​
197describe("clampPreviewMiniPlayerPosition", () => {
198 it("keeps a dragged player within the chat viewport", () => {
199 expect(
200 clampPreviewMiniPlayerPosition({ x: 900, y: -40 }, container, { width: 360, height: 240 }),
201 ).toEqual({ x: 628, y: PREVIEW_MINI_PLAYER_EDGE_GAP });
202 });
203​
204 it("keeps the player above a growing composer inset", () => {
205 expect(
206 clampPreviewMiniPlayerPosition(
207 { x: 500, y: 448 },
208 container,
209 { width: 360, height: 240 },
210 160,
211 ),
212 ).toEqual({ x: 500, y: 288 });
213 });
214});