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

composerFooterLayout.test.ts

430 lines14.3 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { resolveContextStripLabelsCompact } from "./BranchToolbar.logic";
4import {
5 COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX,
6 COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX,
7 COMPOSER_RESTING_EXPANSION_MIN_PX,
8 getRestingComposerImagePreviewCounts,
9 resolveComposerTimelineInset,
10 resolveRestingComposerControlsLayout,
11 resolveRestingComposerControlsNaturalWidth,
12 shouldAnimateComposerRestingTransition,
13 shouldUseCompactComposerPrimaryActions,
14 shouldUseCompactComposerFooter,
15 shouldUseRestingComposerLayout,
16} from "./composerFooterLayout";
17​
18describe("getRestingComposerImagePreviewCounts", () => {
19 it("shows at most three thumbnails and counts the remainder", () => {
20 expect(getRestingComposerImagePreviewCounts(0)).toEqual({
21 visibleCount: 0,
22 overflowCount: 0,
23 });
24 expect(getRestingComposerImagePreviewCounts(3)).toEqual({
25 visibleCount: 3,
26 overflowCount: 0,
27 });
28 expect(getRestingComposerImagePreviewCounts(7)).toEqual({
29 visibleCount: 3,
30 overflowCount: 4,
31 });
32 });
33});
34​
35describe("shouldUseCompactComposerFooter", () => {
36 it("stays expanded without a measured width", () => {
37 expect(shouldUseCompactComposerFooter(null)).toBe(false);
38 });
39​
40 it("switches to compact mode below the breakpoint", () => {
41 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX - 1)).toBe(true);
42 });
43​
44 it("stays expanded at and above the breakpoint", () => {
45 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX)).toBe(false);
46 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX + 48)).toBe(false);
47 });
48​
49 it("uses a higher breakpoint for wide action states", () => {
50 expect(
51 shouldUseCompactComposerFooter(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX - 1, {
52 hasWideActions: true,
53 }),
54 ).toBe(true);
55 expect(
56 shouldUseCompactComposerFooter(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX, {
57 hasWideActions: true,
58 }),
59 ).toBe(false);
60 });
61});
62​
63describe("shouldUseCompactComposerPrimaryActions", () => {
64 it("matches the wide footer breakpoint", () => {
65 expect(
66 shouldUseCompactComposerPrimaryActions(
67 COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX - 1,
68 { hasWideActions: true },
69 ),
70 ).toBe(true);
71 expect(
72 shouldUseCompactComposerPrimaryActions(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX, {
73 hasWideActions: true,
74 }),
75 ).toBe(false);
76 });
77});
78​
79describe("resolveComposerTimelineInset", () => {
80 it("follows the expanded overlay height", () => {
81 expect(
82 resolveComposerTimelineInset({ currentInset: 160, overlayHeight: 140, isResting: false }),
83 ).toBe(140);
84 });
85​
86 it("keeps a larger expanded reservation while resting", () => {
87 expect(
88 resolveComposerTimelineInset({ currentInset: 200, overlayHeight: 60, isResting: true }),
89 ).toBe(200);
90 });
91​
92 it("reserves the empty expansion when no larger height is known", () => {
93 expect(
94 resolveComposerTimelineInset({ currentInset: 0, overlayHeight: 60, isResting: true }),
95 ).toBe(60 + COMPOSER_RESTING_EXPANSION_MIN_PX);
96 });
97});
98​
99describe("shouldUseRestingComposerLayout", () => {
100 const resting = {
101 isExistingThread: true,
102 isMobileViewport: false,
103 isFocused: false,
104 isScrollCollapsed: false,
105 hasExpandedChrome: false,
106 collapseOnBlur: true,
107 };
108​
109 it("uses the resting layout for an unfocused desktop composer", () => {
110 expect(shouldUseRestingComposerLayout(resting)).toBe(true);
111 });
112​
113 it("keeps an unfocused composer expanded when blur collapse is off", () => {
114 expect(shouldUseRestingComposerLayout({ ...resting, collapseOnBlur: false })).toBe(false);
115 });
116​
117 it("rests a scroll-collapsed composer even while focused", () => {
118 expect(
119 shouldUseRestingComposerLayout({ ...resting, isFocused: true, isScrollCollapsed: true }),
120 ).toBe(true);
121 });
122​
123 it("rests a scroll-collapsed composer regardless of the blur preference", () => {
124 expect(
125 shouldUseRestingComposerLayout({
126 ...resting,
127 isFocused: true,
128 isScrollCollapsed: true,
129 collapseOnBlur: false,
130 }),
131 ).toBe(true);
132 });
133​
134 it("keeps new-thread composers expanded", () => {
135 expect(shouldUseRestingComposerLayout({ ...resting, isExistingThread: false })).toBe(false);
136 });
137​
138 it("leaves responsive mobile on its existing collapse path", () => {
139 expect(shouldUseRestingComposerLayout({ ...resting, isMobileViewport: true })).toBe(false);
140 });
141​
142 it("expands when focus is anywhere in the composer", () => {
143 expect(shouldUseRestingComposerLayout({ ...resting, isFocused: true })).toBe(false);
144 });
145​
146 it("keeps drawers and composer-owned menus expanded", () => {
147 expect(shouldUseRestingComposerLayout({ ...resting, hasExpandedChrome: true })).toBe(false);
148 });
149});
150​
151describe("shouldAnimateComposerRestingTransition", () => {
152 it("does not animate layout measurements that settle during initial mount", () => {
153 expect(
154 shouldAnimateComposerRestingTransition({
155 hasCompletedInitialLayout: false,
156 stateChanged: true,
157 hasInterruptedAnimation: false,
158 }),
159 ).toBe(false);
160 });
161​
162 it("animates later resting-state changes and interrupted transitions", () => {
163 expect(
164 shouldAnimateComposerRestingTransition({
165 hasCompletedInitialLayout: true,
166 stateChanged: true,
167 hasInterruptedAnimation: false,
168 }),
169 ).toBe(true);
170 expect(
171 shouldAnimateComposerRestingTransition({
172 hasCompletedInitialLayout: true,
173 stateChanged: false,
174 hasInterruptedAnimation: true,
175 }),
176 ).toBe(true);
177 });
178});
179​
180describe("resolveRestingComposerControlsLayout", () => {
181 // Picker 140 natural / 96 minimum, plus a 9px separator. Traits 60,
182 // mode 140, overflow 24, gap 4.
183 const base = {
184 gap: 4,
185 naturalFixedWidth: 149,
186 minimumFixedWidth: 105,
187 blockWidths: [60, 140],
188 overflowWidth: 24,
189 };
190​
191 it("shows everything when the host has room", () => {
192 // 149 + 60 + 140 + 4 * 2 = 357
193 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
194 hiddenCount: 0,
195 visible: true,
196 });
197 });
198​
199 it("moves trailing blocks into the overflow menu until the rest fits", () => {
200 // 149 + 60 + 24 + 4 * 2 = 241
201 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 356 })).toEqual({
202 hiddenCount: 1,
203 visible: true,
204 });
205 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
206 hiddenCount: 1,
207 visible: true,
208 });
209 // 149 + 24 + 4 = 177
210 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
211 hiddenCount: 2,
212 visible: true,
213 });
214 });
215​
216 it("shrinks the picker after moving every trailing block into overflow", () => {
217 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 176 })).toEqual({
218 hiddenCount: 2,
219 visible: true,
220 });
221 // 105 + 24 + 4 = 133
222 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 133 })).toEqual({
223 hiddenCount: 2,
224 visible: true,
225 });
226 });
227​
228 it("hides the whole cluster below the picker's minimum readable width", () => {
229 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 132 })).toEqual({
230 hiddenCount: 2,
231 visible: false,
232 });
233 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 0 })).toEqual({
234 hiddenCount: 2,
235 visible: false,
236 });
237 });
238​
239 it("uses the same thresholds while shrinking and growing", () => {
240 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
241 hiddenCount: 2,
242 visible: true,
243 });
244 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
245 hiddenCount: 1,
246 visible: true,
247 });
248 });
249​
250 it("supports a single leading control without overflow blocks", () => {
251 expect(
252 resolveRestingComposerControlsLayout({
253 ...base,
254 naturalFixedWidth: 140,
255 minimumFixedWidth: 140,
256 blockWidths: [],
257 overflowWidth: 0,
258 hostWidth: 139,
259 }),
260 ).toEqual({ hiddenCount: 0, visible: false });
261 });
262});
263​
264describe("context strip labels and resting composer controls", () => {
265 // Widths captured from a desktop renderer that crashed with React error
266 // 185. The strip is 724px wide. Its expanded labels need 327px, and the
267 // rest of its chrome needs 125px. The composer controls sit in the host
268 // that takes whatever is left.
269 const stripWidth = 724;
270 const labelWidth = 327;
271 const chromeWidth = 125;
272 const measurement = {
273 gap: 4,
274 naturalFixedWidth: 96.3828125 + 5 + 4,
275 minimumFixedWidth: 52 + 5 + 4,
276 blockWidths: [130.6953125, 119.671875],
277 overflowWidth: 28,
278 };
279 const naturalWidth = resolveRestingComposerControlsNaturalWidth(measurement);
280​
281 function hostWidth(compact: boolean): number {
282 return stripWidth - chromeWidth - (compact ? 0 : labelWidth);
283 }
284​
285 it("keeps the labels compact when the full controls only fit beside compact labels", () => {
286 // Compact labels leave 599px, so the composer shows every block.
287 const layout = resolveRestingComposerControlsLayout({
288 ...measurement,
289 hostWidth: hostWidth(true),
290 });
291 expect(layout).toEqual({ hiddenCount: 0, visible: true });
292​
293 // The strip reserves the natural controls width, so expanding the
294 // labels is off the table: 125 + 327 + 364 > 724.
295 const compact = resolveContextStripLabelsCompact({
296 compact: true,
297 neededWidth: chromeWidth + labelWidth + naturalWidth,
298 availableWidth: stripWidth,
299 });
300 expect(compact).toBe(true);
301​
302 // The next pass sees the same inputs and lands on the same answer.
303 expect(
304 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(compact) }),
305 ).toEqual(layout);
306 });
307​
308 it("does not settle when the strip only reserves the visible controls", () => {
309 // Regression guard for the alternating layout. Reserving only the
310 // controls left visible after two blocks moved into overflow makes the
311 // strip expand its labels, which shrinks the host below what the full
312 // controls need, which hides the blocks again.
313 const hiddenControlsWidth = 137;
314 const expands = !resolveContextStripLabelsCompact({
315 compact: true,
316 neededWidth: chromeWidth + labelWidth + hiddenControlsWidth,
317 availableWidth: stripWidth,
318 });
319 expect(expands).toBe(true);
320 expect(
321 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(false) }),
322 ).toEqual({ hiddenCount: 2, visible: true });
323 });
324});
325​
326describe("resolveRestingComposerControlsLayout hysteresis", () => {
327 // Same cluster as above: picker 140 natural / 96 minimum plus a 9px
328 // separator, traits 60, mode 140, overflow 24, gap 4.
329 const base = {
330 gap: 4,
331 naturalFixedWidth: 149,
332 minimumFixedWidth: 105,
333 blockWidths: [60, 140],
334 overflowWidth: 24,
335 };
336​
337 it("keeps a block in overflow when re-showing it would leave no slack", () => {
338 // 149 + 60 + 140 + 4 * 2 = 357 fills the host exactly.
339 expect(
340 resolveRestingComposerControlsLayout({
341 ...base,
342 hostWidth: 357,
343 previous: { hiddenCount: 1, visible: true },
344 }),
345 ).toEqual({ hiddenCount: 1, visible: true });
346 });
347​
348 it("re-shows a block once the host clears the slack margin", () => {
349 expect(
350 resolveRestingComposerControlsLayout({
351 ...base,
352 hostWidth: 358,
353 previous: { hiddenCount: 1, visible: true },
354 }),
355 ).toEqual({ hiddenCount: 0, visible: true });
356 });
357​
358 it("restores the blocks that fit when the full cluster has no slack", () => {
359 const partlyRestored = resolveRestingComposerControlsLayout({
360 ...base,
361 hostWidth: 357,
362 previous: { hiddenCount: 2, visible: true },
363 });
364 expect(partlyRestored).toEqual({ hiddenCount: 1, visible: true });
365 expect(
366 resolveRestingComposerControlsLayout({ ...base, hostWidth: 357, previous: partlyRestored }),
367 ).toEqual(partlyRestored);
368 expect(
369 resolveRestingComposerControlsLayout({ ...base, hostWidth: 358, previous: partlyRestored }),
370 ).toEqual({ hiddenCount: 0, visible: true });
371 });
372​
373 it("requires slack before partially restoring a cluster", () => {
374 // One inline block, the picker, and overflow need 149 + 60 + 24 + 8 = 241px.
375 const previous = { hiddenCount: 2, visible: true };
376 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241, previous })).toEqual(
377 previous,
378 );
379 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 242, previous })).toEqual({
380 hiddenCount: 1,
381 visible: true,
382 });
383 });
384​
385 it("still resolves from scratch when there is no previous layout", () => {
386 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
387 hiddenCount: 0,
388 visible: true,
389 });
390 });
391​
392 it("settles when the measured picker width jitters below a pixel", () => {
393 // Regression guard for React error #185 ("Maximum update depth
394 // exceeded"). The composer re-measures on every render, so the picker's
395 // recovered natural width can land a fraction of a pixel apart between
396 // renders. Without hysteresis that flips hiddenCount forever.
397 let layout = { hiddenCount: 0, visible: true };
398 const settled: string[] = [];
399 for (let index = 0; index < 10; index += 1) {
400 layout = resolveRestingComposerControlsLayout({
401 ...base,
402 // The picker's recovered natural width lands a half pixel apart
403 // between renders.
404 naturalFixedWidth: index % 2 === 0 ? 149 : 149.5,
405 hostWidth: 357,
406 previous: layout,
407 });
408 if (index >= 2) settled.push(`${layout.hiddenCount}:${layout.visible}`);
409 }
410 expect(new Set(settled).size).toBe(1);
411 });
412​
413 it("keeps the cluster hidden until its minimum width clears the slack", () => {
414 // 105 + 24 + 4 = 133 is the exact minimum for the hidden cluster.
415 expect(
416 resolveRestingComposerControlsLayout({
417 ...base,
418 hostWidth: 133,
419 previous: { hiddenCount: 2, visible: false },
420 }),
421 ).toEqual({ hiddenCount: 2, visible: false });
422 expect(
423 resolveRestingComposerControlsLayout({
424 ...base,
425 hostWidth: 134,
426 previous: { hiddenCount: 2, visible: false },
427 }),
428 ).toEqual({ hiddenCount: 2, visible: true });
429 });
430});