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

useCodeViewFileReveal.test.ts

72 lines2.0 KB

1import type { CodeViewScrollTarget } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { createCodeViewFileReveal } from "./useCodeViewFileReveal";
5​
6function viewer(initialItems: ReadonlyArray<string>) {
7 const items = new Set(initialItems);
8 const scrolls: CodeViewScrollTarget[] = [];
9 return {
10 items,
11 scrolls,
12 getInstance: () => ({}),
13 getItem: (id: string) => (items.has(id) ? {} : undefined),
14 scrollTo: (target: CodeViewScrollTarget) => scrolls.push(target),
15 };
17​
18describe("createCodeViewFileReveal", () => {
19 it("scrolls at once to a file the viewer holds", () => {
20 const reveal = createCodeViewFileReveal();
21 const view = viewer(["a"]);
22 reveal.attach(view);
23​
24 reveal.request("a");
25​
26 expect(view.scrolls).toEqual([{ type: "item", id: "a", align: "start" }]);
27 });
28​
29 it("keeps a missing file pending until a flush finds it, then drops it", () => {
30 const reveal = createCodeViewFileReveal();
31 const view = viewer([]);
32 reveal.attach(view);
33​
34 reveal.request("a");
35 reveal.flush();
36 expect(view.scrolls).toEqual([]);
37​
38 view.items.add("a");
39 reveal.flush();
40 reveal.flush();
41 expect(view.scrolls).toHaveLength(1);
42 });
43​
44 it("applies a reveal asked before the viewer mounted once it holds the file", () => {
45 const reveal = createCodeViewFileReveal();
46 const view = viewer([]);
47​
48 reveal.request("a");
49 reveal.attach(view);
50 expect(view.scrolls).toEqual([]);
51​
52 view.items.add("a");
53 reveal.flush();
54 expect(view.scrolls).toEqual([{ type: "item", id: "a", align: "start" }]);
55 });
56​
57 it("drops a pending reveal when its viewer detaches, and positions the next viewer on request", () => {
58 const reveal = createCodeViewFileReveal();
59 const first = viewer([]);
60 const second = viewer(["a"]);
61 reveal.attach(first);
62​
63 reveal.request("a");
64 reveal.attach(null);
65 reveal.attach(second);
66 reveal.flush();
67 expect(second.scrolls).toEqual([]);
68​
69 reveal.request("a");
70 expect(second.scrolls).toHaveLength(1);
71 });
72});