Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3import { createUpwardScrollDetector, isTimelineScrollTarget } from "./timelineScrollTarget";
4
5class ScrollElement extends EventTarget {
6 scrollTop = 0;
7 scrollHeight = 100;
8 clientHeight = 100;
9 overflowY = "visible";
10 overscrollBehaviorY = "auto";
11
12 constructor(readonly parentElement: ScrollElement | null = null) {
13 super();
14 }
15
16 contains(target: ScrollElement): boolean {
17 return (
18 target === this || (target.parentElement !== null && this.contains(target.parentElement))
19 );
20 }
21}
22
23function targetsTimeline(target: EventTarget | null, timeline: ScrollElement, deltaY: number) {
24 return isTimelineScrollTarget(target, timeline as unknown as HTMLElement, deltaY);
25}
26
27function setup() {
28 const timeline = Object.assign(new ScrollElement(), {
29 overflowY: "auto",
30 scrollHeight: 1500,
31 clientHeight: 500,
32 scrollTop: 1000,
33 });
34 const group = Object.assign(new ScrollElement(timeline), {
35 overflowY: "auto",
36 scrollHeight: 300,
37 scrollTop: 80,
38 });
39 return { timeline, group, content: new ScrollElement(group) };
40}
41
42beforeEach(() => {
43 vi.stubGlobal("Element", ScrollElement);
44 vi.stubGlobal("getComputedStyle", (element: ScrollElement) => element);
45});
46afterEach(() => vi.unstubAllGlobals());
47
48describe("timeline scroll targets", () => {
49 it.each([-30, 30])("keeps a nested tool group's scroll out of the timeline: %i", (deltaY) => {
50 const { timeline, group, content } = setup();
51 expect(targetsTimeline(content, timeline, deltaY)).toBe(false);
52 expect(targetsTimeline(group, timeline, deltaY)).toBe(false);
53 });
54
55 it.each([
56 { scrollTop: 0, deltaY: -30 },
57 { scrollTop: 200, deltaY: 30 },
58 ])("allows chaining only past the matching edge: %j", ({ scrollTop, deltaY }) => {
59 const { timeline, group, content } = setup();
60 group.scrollTop = scrollTop;
61 expect(targetsTimeline(content, timeline, deltaY)).toBe(true);
62 expect(targetsTimeline(content, timeline, -deltaY)).toBe(false);
63 });
64});
65
66describe("timeline scroll follow opt-out", () => {
67 it("detects iframe-only upward scrolling after following streaming content", () => {
68 const scrolledUp = createUpwardScrollDetector({ top: 1000, height: 1500 });
69 expect(scrolledUp({ top: 1000, height: 1800 })).toBe(false);
70 expect(scrolledUp({ top: 1300, height: 1800 })).toBe(false);
71 expect(scrolledUp({ top: 1200, height: 1700 })).toBe(false);
72
73 // Sandboxed frames deliver no wheel or touch event to the parent timeline.
74 expect(scrolledUp({ top: 1199.5, height: 1700 })).toBe(false);
75 expect(scrolledUp({ top: 1199, height: 1700 })).toBe(false);
76 expect(scrolledUp({ top: 1198.5, height: 1700 })).toBe(true);
77 });
78
79 it("preserves upward new-turn positioning, then detects the reader leaving that anchor", () => {
80 const scrolledUp = createUpwardScrollDetector({ top: 1400, height: 2400 });
81 // The caller must distinguish its animated positioning from iframe scroll chaining.
82 const positioning = { top: 1200, height: 2400, programmatic: true };
83 expect(scrolledUp(positioning)).toBe(false);
84 expect(scrolledUp({ ...positioning, top: 1100 })).toBe(false);
85
86 expect(scrolledUp({ top: 1100, height: 2400 })).toBe(false);
87 expect(scrolledUp({ top: 950, height: 2400 })).toBe(true);
88 });
89});