Open History to see the last commit
1import {
2 CodeView,
3 DEFAULT_THEMES,
4 parseDiffFromFile,
5 preloadHighlighter,
6 type CodeViewItem,
7} from "@pierre/diffs";
8import { expect, it, vi } from "vite-plus/test";
9
10const LINES = Array.from({ length: 80 }, (_, index) => `line ${index + 1}`);
11
12function contents(changes: Record<number, string>) {
13 return `${LINES.map((line, index) => changes[index] ?? line).join("\n")}\n`;
14}
15
16function currentMatchText() {
17 return Array.from(CSS.highlights.get("diffs-search-current") ?? [], String);
18}
19
20// Upstream find skips folded files and collapsed context, and drops focus on close. Our @pierre/diffs patch covers all three.
21it("finds and reveals matches in folded files and collapsed context, then hands focus back", async () => {
22 await preloadHighlighter({ themes: Object.values(DEFAULT_THEMES), langs: ["text"] });
23 let items: CodeViewItem<undefined>[] = [
24 {
25 id: "folded",
26 type: "diff",
27 collapsed: true,
28 fileDiff: parseDiffFromFile(
29 { name: "folded.txt", contents: "old\n" },
30 { name: "folded.txt", contents: "NEEDLE added\n" },
31 ),
32 },
33 {
34 id: "context",
35 type: "diff",
36 fileDiff: parseDiffFromFile(
37 { name: "context.txt", contents: contents({ 39: "needle in context" }) },
38 {
39 name: "context.txt",
40 contents: contents({ 0: "changed", 39: "needle in context", 79: "changed" }),
41 },
42 ),
43 },
44 ];
45 const root = document.createElement("div");
46 root.style.cssText = "width: 800px; height: 300px; overflow: auto;";
47 document.body.append(root);
48 const search = { matchCount: 0, currentMatchIndex: undefined as number | undefined };
49 const view = new CodeView<undefined>({
50 diffStyle: "unified",
51 searchOptions: { scrollBehavior: "instant" },
52 onSearchStateChange: (state) => {
53 search.matchCount = state?.matchCount ?? 0;
54 search.currentMatchIndex = state?.currentMatchIndex;
55 },
56 onRevealSearchMatch: (item) => {
57 items = items.map((candidate) =>
58 candidate.id === item.id ? { ...candidate, collapsed: false, version: 1 } : candidate,
59 );
60 view.setItems(items);
61 },
62 });
63 using _view = {
64 [Symbol.dispose]: () => {
65 view.cleanUp();
66 root.remove();
67 },
68 };
69 view.setup(root);
70 view.setItems(items);
71
72 view.openSearch({ query: "needle" });
73 await vi.waitFor(() => expect(search).toEqual({ matchCount: 2, currentMatchIndex: 0 }));
74 await vi.waitFor(() => expect(currentMatchText()).toEqual(["NEEDLE"]));
75
76 view.navigateSearch("next");
77 await vi.waitFor(() => expect(currentMatchText()).toEqual(["needle"]));
78 expect(search.currentMatchIndex).toBe(1);
79
80 // Focus that leaves the viewer on close takes the find shortcut with it.
81 const input = root.querySelector("[data-search-panel]")?.shadowRoot?.querySelector("input");
82 input!.dispatchEvent(
83 new KeyboardEvent("keydown", { key: "Escape", bubbles: true, composed: true }),
84 );
85 expect(view.isSearchOpen()).toBe(false);
86 expect(document.activeElement).toBe(root);
87});