|
1 | 1 | import type { CodeViewScrollTarget } from "@pierre/diffs"; |
2 | | -import { describe, expect, it } from "vite-plus/test"; |
| 2 | +import { describe, expect, it, vi } from "vite-plus/test"; |
3 | 3 |
|
4 | 4 | import { createCodeViewScrollRestore } from "./useCodeViewScrollRestore"; |
5 | 5 |
|
@@ -9,10 +9,13 @@ const LINE_HEIGHT = 20; |
9 | 9 | function viewer(tops: Record<string, number>, collapsed: readonly string[] = []) { |
10 | 10 | const items = new Map(Object.entries(tops)); |
11 | 11 | const instance = { |
| 12 | + scrollTop: 0, |
| 13 | + rendered: null as string[] | null, |
12 | 14 | getHeight: () => 500, |
| 15 | + getScrollTop: () => instance.scrollTop, |
13 | 16 | getTopForItem: (id: string) => items.get(id), |
14 | 17 | getRenderedItems: () => |
15 | | - [...items.keys()].map((id) => ({ |
| 18 | + (instance.rendered ?? [...items.keys()]).map((id) => ({ |
16 | 19 | id, |
17 | 20 | instance: { |
18 | 21 | getNumericScrollAnchor: (localTop: number) => { |
@@ -46,13 +49,32 @@ function mount( |
46 | 49 | return restore; |
47 | 50 | } |
48 | 51 |
|
| 52 | +function setup() { |
| 53 | + const frames: FrameRequestCallback[] = []; |
| 54 | + vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => frames.push(callback)); |
| 55 | + const runFrames = () => { |
| 56 | + while (frames.length > 0) frames.shift()?.(0); |
| 57 | + }; |
| 58 | + const scroll = ( |
| 59 | + restore: ReturnType<typeof mount>, |
| 60 | + view: ReturnType<typeof viewer>, |
| 61 | + top: number, |
| 62 | + ) => { |
| 63 | + view.instance.scrollTop = top; |
| 64 | + restore.onScroll(top, view.instance); |
| 65 | + runFrames(); |
| 66 | + }; |
| 67 | + return { runFrames, scroll }; |
| 68 | +} |
| 69 | + |
49 | 70 | describe("createCodeViewScrollRestore", () => { |
50 | 71 | it("puts a remounted viewer back on its scope's file and line, not its pixel offset", () => { |
| 72 | + const { scroll } = setup(); |
51 | 73 | const first = viewer({ a: 0, b: 4000, c: 9000 }); |
52 | 74 | // 104px into file b: line 7 is the first under the sticky header, 16px below its edge. |
53 | | - mount("thread-a", first).onScroll(4104, first.instance); |
| 75 | + scroll(mount("thread-a", first), first, 4104); |
54 | 76 | const other = viewer({ a: 0, b: 4000 }); |
55 | | - mount("thread-b", other).onScroll(1200, other.instance); |
| 77 | + scroll(mount("thread-b", other), other, 1200); |
56 | 78 |
|
57 | 79 | // The remounted viewer estimates file a shorter, so file b starts elsewhere. |
58 | 80 | const again = viewer({ a: 0, b: 2500, c: 6000 }); |
@@ -62,16 +84,35 @@ describe("createCodeViewScrollRestore", () => { |
62 | 84 | ]); |
63 | 85 | }); |
64 | 86 |
|
| 87 | + it("reads the anchor from the viewport Pierre renders after the scroll, not the one before", () => { |
| 88 | + const { runFrames } = setup(); |
| 89 | + const first = viewer({ a: 0, b: 4000, c: 8000 }); |
| 90 | + first.instance.rendered = ["a"]; |
| 91 | + const restore = mount("thread-e", first); |
| 92 | + // A scrollbar jump notifies before the viewer renders file c. |
| 93 | + first.instance.scrollTop = 8104; |
| 94 | + restore.onScroll(8104, first.instance); |
| 95 | + first.instance.rendered = ["c"]; |
| 96 | + runFrames(); |
| 97 | + |
| 98 | + const again = viewer({ a: 0, b: 2500, c: 6000 }); |
| 99 | + mount("thread-e", again); |
| 100 | + expect(again.scrolls).toEqual([ |
| 101 | + { type: "line", id: "c", align: "start", lineNumber: 7, offset: 16 }, |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
65 | 105 | it("loads more files until the anchored file has loaded, then falls back to the file", () => { |
| 106 | + const { scroll } = setup(); |
66 | 107 | const first = viewer({ a: 0, b: 4000 }); |
67 | | - mount("thread-c", first).onScroll(4104, first.instance); |
| 108 | + scroll(mount("thread-c", first), first, 4104); |
68 | 109 |
|
69 | 110 | const loading = viewer({ a: 0 }); |
70 | 111 | let batches = 0; |
71 | 112 | const restore = mount("thread-c", loading, false, () => { |
72 | 113 | batches += 1; |
73 | 114 | }); |
74 | | - restore.onScroll(0, loading.instance); |
| 115 | + scroll(restore, loading, 0); |
75 | 116 | restore.flush(); |
76 | 117 | expect(loading.scrolls).toEqual([]); |
77 | 118 | expect(batches).toBe(2); |
@@ -92,9 +133,10 @@ describe("createCodeViewScrollRestore", () => { |
92 | 133 | }); |
93 | 134 |
|
94 | 135 | it("forgets a position that a reveal or collapsing every file replaced", () => { |
| 136 | + const { scroll } = setup(); |
95 | 137 | const first = viewer({ a: 0, b: 4000 }); |
96 | 138 | const restore = mount("thread-d", first); |
97 | | - restore.onScroll(4104, first.instance); |
| 139 | + scroll(restore, first, 4104); |
98 | 140 | restore.discard(); |
99 | 141 |
|
100 | 142 | const again = viewer({ a: 0, b: 4000 }); |
|