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

timelineScrollAnchoring.test.tsx

169 lines4.4 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import {
3 getAnchoredTurnMetrics,
4 getRowBottom,
5 keepTimelineEndVisibleAfterOverlayGrowth,
6} from "./timelineScrollAnchoring";
7​
8function buildState({
9 positions,
10 sizes,
11 scroll = 0,
12 scrollLength = 700,
13}: {
14 readonly positions: readonly number[];
15 readonly sizes: readonly number[];
16 readonly scroll?: number;
17 readonly scrollLength?: number;
18}) {
19 return {
20 data: positions.map((_, index) => index),
21 scroll,
22 scrollLength,
23 positionAtIndex: (index: number) => positions[index],
24 sizeAtIndex: (index: number) => sizes[index],
25 };
27​
28describe("timeline scroll anchoring", () => {
29 it("keeps the live edge visible when the composer overlay grows", () => {
30 const scrollToEnd = vi.fn();
31​
32 keepTimelineEndVisibleAfterOverlayGrowth({
33 timeline: { scrollToEnd },
34 previousOverlayHeight: 120,
35 overlayHeight: 180,
36 followingEnd: true,
37 });
38​
39 expect(scrollToEnd).toHaveBeenCalledOnce();
40 expect(scrollToEnd).toHaveBeenCalledWith({ animated: false });
41 });
42​
43 it("leaves the scroll position alone while the user reads history", () => {
44 const scrollToEnd = vi.fn();
45​
46 keepTimelineEndVisibleAfterOverlayGrowth({
47 timeline: { scrollToEnd },
48 previousOverlayHeight: 120,
49 overlayHeight: 180,
50 followingEnd: false,
51 });
52​
53 expect(scrollToEnd).not.toHaveBeenCalled();
54 });
55​
56 it("measures row bottoms from LegendList row position and size", () => {
57 const state = buildState({
58 positions: [0, 120],
59 sizes: [80, 40],
60 });
61​
62 expect(getRowBottom(state, 1)).toBe(160);
63 });
64​
65 it("treats the active turn as fitting when it fits above the composer", () => {
66 const state = buildState({
67 positions: [0, 300, 460],
68 sizes: [240, 80, 140],
69 scrollLength: 760,
70 });
71​
72 const metrics = getAnchoredTurnMetrics({
73 state,
74 anchorIndex: 1,
75 composerOverlayHeight: 180,
76 anchorOffset: 16,
77 });
78​
79 expect(metrics?.turnHeight).toBe(300);
80 expect(metrics?.usableViewportHeight).toBe(564);
81 expect(metrics?.overflowsUsableViewport).toBe(false);
82 expect(metrics?.targetScrollToRevealEnd).toBe(36);
83 expect(metrics?.scrollDeltaToRevealEnd).toBe(36);
84 });
85​
86 it("targets the real row end instead of any temporary reserved tail", () => {
87 const state = buildState({
88 positions: [0, 1720, 1880],
89 sizes: [1600, 80, 120],
90 scroll: 1900,
91 scrollLength: 760,
92 });
93​
94 const metrics = getAnchoredTurnMetrics({
95 state,
96 anchorIndex: 1,
97 composerOverlayHeight: 180,
98 anchorOffset: 16,
99 });
100​
101 expect(metrics?.lastBottom).toBe(2000);
102 expect(metrics?.targetScrollToRevealEnd).toBe(1436);
103 expect(metrics?.scrollDeltaToRevealEnd).toBe(0);
104 });
105​
106 it("reports overflow only for the current anchored turn", () => {
107 const state = buildState({
108 positions: [0, 900, 1180],
109 sizes: [800, 220, 300],
110 scroll: 900,
111 scrollLength: 760,
112 });
113​
114 const metrics = getAnchoredTurnMetrics({
115 state,
116 anchorIndex: 1,
117 composerOverlayHeight: 180,
118 anchorOffset: 16,
119 });
120​
121 expect(metrics?.turnHeight).toBe(580);
122 expect(metrics?.usableViewportHeight).toBe(564);
123 expect(metrics?.overflowsUsableViewport).toBe(true);
124 });
125​
126 it("returns the minimal positive scroll delta needed to reveal the turn end", () => {
127 const state = buildState({
128 positions: [0, 900, 1180],
129 sizes: [800, 220, 360],
130 scroll: 900,
131 scrollLength: 760,
132 });
133​
134 const metrics = getAnchoredTurnMetrics({
135 state,
136 anchorIndex: 1,
137 composerOverlayHeight: 180,
138 anchorOffset: 16,
139 });
140​
141 expect(metrics?.lastBottom).toBe(1540);
142 expect(metrics?.visibleUsableBottom).toBe(1464);
143 expect(metrics?.scrollDeltaToRevealEnd).toBe(76);
144 });
145​
146 it("subtracts composer height from usable viewport height", () => {
147 const state = buildState({
148 positions: [0, 300],
149 sizes: [120, 470],
150 scrollLength: 700,
151 });
152​
153 const withoutComposer = getAnchoredTurnMetrics({
154 state,
155 anchorIndex: 1,
156 composerOverlayHeight: 0,
157 anchorOffset: 16,
158 });
159 const withComposer = getAnchoredTurnMetrics({
160 state,
161 anchorIndex: 1,
162 composerOverlayHeight: 220,
163 anchorOffset: 16,
164 });
165​
166 expect(withoutComposer?.overflowsUsableViewport).toBe(false);
167 expect(withComposer?.overflowsUsableViewport).toBe(true);
168 });
169});