Open History to see the last commit
1import { describe, expect, test } from "vite-plus/test";
2
3import {
4 createPageScrollController,
5 getTimelinePageScrollKey,
6 getPageScrollDistancePx,
7 getPageScrollMultiplier,
8 getPageScrollVelocityPxPerMs,
9 PAGE_SCROLL_ACCELERATION_MS,
10 PAGE_SCROLL_ANIMATION_MS,
11 PAGE_SCROLL_MAX_MULTIPLIER,
12} from "./pageScrollController";
13
14class TestClock {
15 private currentTime = 0;
16 private nextHandle = 1;
17 private animationFrames = new Map<number, FrameRequestCallback>();
18 private timeouts = new Map<number, { at: number; callback: () => void }>();
19
20 reducedMotion = false;
21
22 readonly env = {
23 now: () => this.currentTime,
24 requestAnimationFrame: (callback: FrameRequestCallback) => {
25 const handle = this.nextHandle;
26 this.nextHandle += 1;
27 this.animationFrames.set(handle, callback);
28 return handle;
29 },
30 cancelAnimationFrame: (handle: number) => {
31 this.animationFrames.delete(handle);
32 },
33 setTimeout: (callback: () => void, delay: number) => {
34 const handle = this.nextHandle;
35 this.nextHandle += 1;
36 this.timeouts.set(handle, { at: this.currentTime + delay, callback });
37 return handle;
38 },
39 clearTimeout: (handle: number) => {
40 this.timeouts.delete(handle);
41 },
42 prefersReducedMotion: () => this.reducedMotion,
43 };
44
45 advanceBy(ms: number, frameMs = 16) {
46 const target = this.currentTime + ms;
47
48 while (this.currentTime < target) {
49 this.currentTime = Math.min(target, this.currentTime + frameMs);
50 this.flushTimeouts();
51 this.flushAnimationFrames();
52 }
53 }
54
55 private flushTimeouts() {
56 let hasDueTimeouts = true;
57 while (hasDueTimeouts) {
58 hasDueTimeouts = false;
59
60 for (const [handle, timeout] of this.timeouts) {
61 if (timeout.at > this.currentTime) {
62 continue;
63 }
64
65 this.timeouts.delete(handle);
66 timeout.callback();
67 hasDueTimeouts = true;
68 }
69 }
70 }
71
72 private flushAnimationFrames() {
73 if (this.animationFrames.size === 0) {
74 return;
75 }
76
77 const frames = [...this.animationFrames.values()];
78 this.animationFrames.clear();
79
80 for (const callback of frames) {
81 callback(this.currentTime);
82 }
83 }
84}
85
86describe("page scroll helpers", () => {
87 const composerPageScrollEvent = (
88 overrides: Partial<Parameters<typeof getTimelinePageScrollKey>[0]> = {},
89 ) => ({
90 altKey: false,
91 clientHeight: 200,
92 ctrlKey: false,
93 defaultPrevented: false,
94 isComposing: false,
95 key: "PageDown",
96 keyCode: 34,
97 metaKey: false,
98 scrollHeight: 200,
99 scrollTop: 0,
100 shiftKey: false,
101 ...overrides,
102 });
103
104 test("leaves page keys to IME composition", () => {
105 expect(getTimelinePageScrollKey(composerPageScrollEvent({ isComposing: true }))).toBeNull();
106 expect(getTimelinePageScrollKey(composerPageScrollEvent({ keyCode: 229 }))).toBeNull();
107 });
108
109 test("leaves page keys to an overflowing composer until it reaches the boundary", () => {
110 expect(
111 getTimelinePageScrollKey(composerPageScrollEvent({ scrollHeight: 600, scrollTop: 0 })),
112 ).toBeNull();
113 expect(
114 getTimelinePageScrollKey(
115 composerPageScrollEvent({
116 key: "PageUp",
117 keyCode: 33,
118 scrollHeight: 600,
119 scrollTop: 400,
120 }),
121 ),
122 ).toBeNull();
123
124 expect(
125 getTimelinePageScrollKey(
126 composerPageScrollEvent({
127 key: "PageUp",
128 keyCode: 33,
129 scrollHeight: 600,
130 scrollTop: 0,
131 }),
132 ),
133 ).toBe("PageUp");
134 expect(
135 getTimelinePageScrollKey(composerPageScrollEvent({ scrollHeight: 600, scrollTop: 400 })),
136 ).toBe("PageDown");
137 });
138
139 test("hands off page keys within a fractional pixel of the composer boundary", () => {
140 expect(
141 getTimelinePageScrollKey(
142 composerPageScrollEvent({
143 key: "PageUp",
144 keyCode: 33,
145 scrollHeight: 600,
146 scrollTop: 0.5,
147 }),
148 ),
149 ).toBe("PageUp");
150 expect(
151 getTimelinePageScrollKey(composerPageScrollEvent({ scrollHeight: 600, scrollTop: 399.5 })),
152 ).toBe("PageDown");
153 });
154
155 test("ramps multiplier over time and caps at the max velocity", () => {
156 expect(getPageScrollMultiplier(0)).toBe(1);
157 expect(getPageScrollMultiplier(PAGE_SCROLL_ACCELERATION_MS / 2)).toBeCloseTo(1.5);
158 expect(getPageScrollMultiplier(PAGE_SCROLL_ACCELERATION_MS * 5)).toBe(
159 PAGE_SCROLL_MAX_MULTIPLIER,
160 );
161 });
162
163 test("derives the hold velocity from page size and acceleration", () => {
164 expect(
165 getPageScrollVelocityPxPerMs({
166 holdElapsedMs: 0,
167 pageScrollDistancePx: 600,
168 }),
169 ).toBeCloseTo(4);
170 expect(
171 getPageScrollVelocityPxPerMs({
172 holdElapsedMs: PAGE_SCROLL_ACCELERATION_MS * 5,
173 pageScrollDistancePx: 600,
174 }),
175 ).toBeCloseTo(8);
176 });
177});
178
179describe("createPageScrollController", () => {
180 test("keeps a single page scroll when the key is tapped", () => {
181 const clock = new TestClock();
182 const container = {
183 clientHeight: 600,
184 scrollHeight: 1_800,
185 scrollTop: 0,
186 getBoundingClientRect: () => ({ height: 600 }),
187 };
188 const controller = createPageScrollController({
189 getContainer: () => container,
190 getScrollPaddingBottomPx: () => 24,
191 env: clock.env,
192 });
193
194 controller.handleKeyDown("PageDown");
195 controller.handleKeyUp("PageDown");
196 clock.advanceBy(PAGE_SCROLL_ANIMATION_MS);
197
198 expect(container.scrollTop).toBeCloseTo(
199 getPageScrollDistancePx({
200 containerHeightPx: 600,
201 scrollPaddingBottomPx: 24,
202 }),
203 5,
204 );
205 });
206
207 test("uses the current motion preference for page taps in both directions", () => {
208 const clock = new TestClock();
209 const container = {
210 clientHeight: 600,
211 scrollHeight: 4_000,
212 scrollTop: 1_200,
213 getBoundingClientRect: () => ({ height: 600 }),
214 };
215 const controller = createPageScrollController({
216 getContainer: () => container,
217 getScrollPaddingBottomPx: () => 24,
218 env: clock.env,
219 });
220
221 for (const reducedMotion of [false, true, false]) {
222 clock.reducedMotion = reducedMotion;
223 for (const [key, destination] of [
224 ["PageDown", 1_740],
225 ["PageUp", 1_200],
226 ] as const) {
227 const start = container.scrollTop;
228 controller.handleKeyDown(key);
229 controller.handleKeyUp(key);
230
231 if (!reducedMotion) {
232 clock.advanceBy(16);
233 expect(container.scrollTop).toBeGreaterThan(Math.min(start, destination));
234 expect(container.scrollTop).toBeLessThan(Math.max(start, destination));
235 clock.advanceBy(PAGE_SCROLL_ANIMATION_MS);
236 }
237 expect(container.scrollTop).toBeCloseTo(destination, 5);
238 clock.advanceBy(PAGE_SCROLL_ANIMATION_MS * 2);
239 expect(container.scrollTop).toBeCloseTo(destination, 5);
240 }
241 }
242 });
243
244 test.each([false, true])(
245 "continues scrolling on hold without repeated keydown events and stops on keyup (reduced motion: %s)",
246 (reducedMotion) => {
247 const clock = new TestClock();
248 clock.reducedMotion = reducedMotion;
249 const container = {
250 clientHeight: 600,
251 scrollHeight: 4_000,
252 scrollTop: 0,
253 getBoundingClientRect: () => ({ height: 600 }),
254 };
255 const controller = createPageScrollController({
256 getContainer: () => container,
257 getScrollPaddingBottomPx: () => 24,
258 env: clock.env,
259 });
260 controller.handleKeyDown("PageDown");
261 clock.advanceBy(PAGE_SCROLL_ANIMATION_MS + 50);
262
263 const afterHoldStarts = container.scrollTop;
264 clock.advanceBy(200);
265
266 expect(container.scrollTop).toBeGreaterThan(afterHoldStarts);
267
268 const stoppedAt = container.scrollTop;
269 controller.handleKeyUp("PageDown");
270 clock.advanceBy(250);
271
272 expect(container.scrollTop).toBe(stoppedAt);
273 },
274 );
275
276 test("notifies once when a page scroll starts", () => {
277 const clock = new TestClock();
278 const started: string[] = [];
279 const controller = createPageScrollController({
280 getContainer: () => ({
281 clientHeight: 600,
282 scrollHeight: 1_800,
283 scrollTop: 600,
284 getBoundingClientRect: () => ({ height: 600 }),
285 }),
286 getScrollPaddingBottomPx: () => 24,
287 onScrollStart: (key) => started.push(key),
288 env: clock.env,
289 });
290
291 controller.handleKeyDown("PageUp");
292 controller.handleKeyDown("PageUp");
293
294 expect(started).toEqual(["PageUp"]);
295 });
296
297 test("does not start a page scroll at the timeline boundary", () => {
298 const clock = new TestClock();
299 const started: string[] = [];
300 const container = {
301 clientHeight: 600,
302 scrollHeight: 1_800,
303 scrollTop: 0.5,
304 getBoundingClientRect: () => ({ height: 600 }),
305 };
306 const controller = createPageScrollController({
307 getContainer: () => container,
308 getScrollPaddingBottomPx: () => 24,
309 onScrollStart: (key) => started.push(key),
310 env: clock.env,
311 });
312
313 controller.handleKeyDown("PageUp");
314 clock.advanceBy(PAGE_SCROLL_ANIMATION_MS * 2);
315
316 container.scrollTop = container.scrollHeight - container.clientHeight - 0.5;
317 controller.handleKeyDown("PageDown");
318 clock.advanceBy(PAGE_SCROLL_ANIMATION_MS * 2);
319
320 expect(started).toEqual([]);
321 expect(container.scrollTop).toBe(1_199.5);
322 });
323});