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

composerFooterLayout.test.ts

279 lines9.2 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 getRestingComposerImagePreviewCounts,
8 resolveRestingComposerControlsLayout,
9 resolveRestingComposerControlsNaturalWidth,
10 shouldAnimateComposerRestingTransition,
11 shouldUseCompactComposerPrimaryActions,
12 shouldUseCompactComposerFooter,
13 shouldUseRestingComposerLayout,
14} from "./composerFooterLayout";
15​
16describe("getRestingComposerImagePreviewCounts", () => {
17 it("shows at most three thumbnails and counts the remainder", () => {
18 expect(getRestingComposerImagePreviewCounts(0)).toEqual({
19 visibleCount: 0,
20 overflowCount: 0,
21 });
22 expect(getRestingComposerImagePreviewCounts(3)).toEqual({
23 visibleCount: 3,
24 overflowCount: 0,
25 });
26 expect(getRestingComposerImagePreviewCounts(7)).toEqual({
27 visibleCount: 3,
28 overflowCount: 4,
29 });
30 });
31});
32​
33describe("shouldUseCompactComposerFooter", () => {
34 it("stays expanded without a measured width", () => {
35 expect(shouldUseCompactComposerFooter(null)).toBe(false);
36 });
37​
38 it("switches to compact mode below the breakpoint", () => {
39 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX - 1)).toBe(true);
40 });
41​
42 it("stays expanded at and above the breakpoint", () => {
43 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX)).toBe(false);
44 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX + 48)).toBe(false);
45 });
46​
47 it("uses a higher breakpoint for wide action states", () => {
48 expect(
49 shouldUseCompactComposerFooter(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX - 1, {
50 hasWideActions: true,
51 }),
52 ).toBe(true);
53 expect(
54 shouldUseCompactComposerFooter(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX, {
55 hasWideActions: true,
56 }),
57 ).toBe(false);
58 });
59});
60​
61describe("shouldUseCompactComposerPrimaryActions", () => {
62 it("matches the wide footer breakpoint", () => {
63 expect(
64 shouldUseCompactComposerPrimaryActions(
65 COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX - 1,
66 { hasWideActions: true },
67 ),
68 ).toBe(true);
69 expect(
70 shouldUseCompactComposerPrimaryActions(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX, {
71 hasWideActions: true,
72 }),
73 ).toBe(false);
74 });
75});
76​
77describe("shouldUseRestingComposerLayout", () => {
78 const resting = {
79 isExistingThread: true,
80 isMobileViewport: false,
81 isFocused: false,
82 hasExpandedChrome: false,
83 };
84​
85 it("uses the resting layout for an unfocused desktop composer", () => {
86 expect(shouldUseRestingComposerLayout(resting)).toBe(true);
87 });
88​
89 it("keeps new-thread composers expanded", () => {
90 expect(shouldUseRestingComposerLayout({ ...resting, isExistingThread: false })).toBe(false);
91 });
92​
93 it("leaves responsive mobile on its existing collapse path", () => {
94 expect(shouldUseRestingComposerLayout({ ...resting, isMobileViewport: true })).toBe(false);
95 });
96​
97 it("expands when focus is anywhere in the composer", () => {
98 expect(shouldUseRestingComposerLayout({ ...resting, isFocused: true })).toBe(false);
99 });
100​
101 it("keeps drawers and composer-owned menus expanded", () => {
102 expect(shouldUseRestingComposerLayout({ ...resting, hasExpandedChrome: true })).toBe(false);
103 });
104});
105​
106describe("shouldAnimateComposerRestingTransition", () => {
107 it("does not animate layout measurements that settle during initial mount", () => {
108 expect(
109 shouldAnimateComposerRestingTransition({
110 hasCompletedInitialLayout: false,
111 stateChanged: true,
112 hasInterruptedAnimation: false,
113 }),
114 ).toBe(false);
115 });
116​
117 it("animates later resting-state changes and interrupted transitions", () => {
118 expect(
119 shouldAnimateComposerRestingTransition({
120 hasCompletedInitialLayout: true,
121 stateChanged: true,
122 hasInterruptedAnimation: false,
123 }),
124 ).toBe(true);
125 expect(
126 shouldAnimateComposerRestingTransition({
127 hasCompletedInitialLayout: true,
128 stateChanged: false,
129 hasInterruptedAnimation: true,
130 }),
131 ).toBe(true);
132 });
133});
134​
135describe("resolveRestingComposerControlsLayout", () => {
136 // Picker 140 natural / 96 minimum, plus a 9px separator. Traits 60,
137 // mode 140, overflow 24, gap 4.
138 const base = {
139 gap: 4,
140 naturalFixedWidth: 149,
141 minimumFixedWidth: 105,
142 blockWidths: [60, 140],
143 overflowWidth: 24,
144 };
145​
146 it("shows everything when the host has room", () => {
147 // 149 + 60 + 140 + 4 * 2 = 357
148 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
149 hiddenCount: 0,
150 visible: true,
151 });
152 });
153​
154 it("moves trailing blocks into the overflow menu until the rest fits", () => {
155 // 149 + 60 + 24 + 4 * 2 = 241
156 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 356 })).toEqual({
157 hiddenCount: 1,
158 visible: true,
159 });
160 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
161 hiddenCount: 1,
162 visible: true,
163 });
164 // 149 + 24 + 4 = 177
165 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
166 hiddenCount: 2,
167 visible: true,
168 });
169 });
170​
171 it("shrinks the picker after moving every trailing block into overflow", () => {
172 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 176 })).toEqual({
173 hiddenCount: 2,
174 visible: true,
175 });
176 // 105 + 24 + 4 = 133
177 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 133 })).toEqual({
178 hiddenCount: 2,
179 visible: true,
180 });
181 });
182​
183 it("hides the whole cluster below the picker's minimum readable width", () => {
184 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 132 })).toEqual({
185 hiddenCount: 2,
186 visible: false,
187 });
188 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 0 })).toEqual({
189 hiddenCount: 2,
190 visible: false,
191 });
192 });
193​
194 it("uses the same thresholds while shrinking and growing", () => {
195 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
196 hiddenCount: 2,
197 visible: true,
198 });
199 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
200 hiddenCount: 1,
201 visible: true,
202 });
203 });
204​
205 it("supports a single leading control without overflow blocks", () => {
206 expect(
207 resolveRestingComposerControlsLayout({
208 ...base,
209 naturalFixedWidth: 140,
210 minimumFixedWidth: 140,
211 blockWidths: [],
212 overflowWidth: 0,
213 hostWidth: 139,
214 }),
215 ).toEqual({ hiddenCount: 0, visible: false });
216 });
217});
218​
219describe("context strip labels and resting composer controls", () => {
220 // Widths captured from a desktop renderer that crashed with React error
221 // 185. The strip is 724px wide. Its expanded labels need 327px, and the
222 // rest of its chrome needs 125px. The composer controls sit in the host
223 // that takes whatever is left.
224 const stripWidth = 724;
225 const labelWidth = 327;
226 const chromeWidth = 125;
227 const measurement = {
228 gap: 4,
229 naturalFixedWidth: 96.3828125 + 5 + 4,
230 minimumFixedWidth: 52 + 5 + 4,
231 blockWidths: [130.6953125, 119.671875],
232 overflowWidth: 28,
233 };
234 const naturalWidth = resolveRestingComposerControlsNaturalWidth(measurement);
235​
236 function hostWidth(compact: boolean): number {
237 return stripWidth - chromeWidth - (compact ? 0 : labelWidth);
238 }
239​
240 it("keeps the labels compact when the full controls only fit beside compact labels", () => {
241 // Compact labels leave 599px, so the composer shows every block.
242 const layout = resolveRestingComposerControlsLayout({
243 ...measurement,
244 hostWidth: hostWidth(true),
245 });
246 expect(layout).toEqual({ hiddenCount: 0, visible: true });
247​
248 // The strip reserves the natural controls width, so expanding the
249 // labels is off the table: 125 + 327 + 364 > 724.
250 const compact = resolveContextStripLabelsCompact({
251 compact: true,
252 neededWidth: chromeWidth + labelWidth + naturalWidth,
253 availableWidth: stripWidth,
254 });
255 expect(compact).toBe(true);
256​
257 // The next pass sees the same inputs and lands on the same answer.
258 expect(
259 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(compact) }),
260 ).toEqual(layout);
261 });
262​
263 it("does not settle when the strip only reserves the visible controls", () => {
264 // Regression guard for the alternating layout. Reserving only the
265 // controls left visible after two blocks moved into overflow makes the
266 // strip expand its labels, which shrinks the host below what the full
267 // controls need, which hides the blocks again.
268 const hiddenControlsWidth = 137;
269 const expands = !resolveContextStripLabelsCompact({
270 compact: true,
271 neededWidth: chromeWidth + labelWidth + hiddenControlsWidth,
272 availableWidth: stripWidth,
273 });
274 expect(expands).toBe(true);
275 expect(
276 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(false) }),
277 ).toEqual({ hiddenCount: 2, visible: true });
278 });
279});