Skip to content
Source
Sign inSign up

codeViewContextExpansion.browser.test.ts

59 lines2.1 KB

1import { CodeView, DEFAULT_THEMES, parsePatchFiles, preloadHighlighter } from "@pierre/diffs";
2import { expect, it, vi } from "vite-plus/test";
3​
4const OLD_LINES = Array.from({ length: 60 }, (_, index) => `line ${index + 1}`);
5const NEW_LINES = OLD_LINES.with(0, "changed 1").with(59, "changed 60");
6const PATCH = `diff --git a/notes.txt b/notes.txt
7--- a/notes.txt
8+++ b/notes.txt
9@@ -1,4 +1,4 @@
10-line 1
11+changed 1
12 line 2
13 line 3
14 line 4
15@@ -57,4 +57,4 @@
16 line 57
17 line 58
18 line 59
19-line 60
20+changed 60
21`;
22​
23function parsePartialDiff() {
24 return parsePatchFiles(PATCH, { cacheKeyPrefix: "notes" })[0]!.files[0]!;
26​
27// A diff refresh re-parses an unchanged patch into a new object with the same cacheKey.
28// Upstream keeps rendering the old object and then discards the file contents loaded for it.
29it("expands hidden context after the host re-supplies an unchanged partial diff", async () => {
30 await preloadHighlighter({ themes: Object.values(DEFAULT_THEMES), langs: ["text"] });
31 const root = document.createElement("div");
32 root.style.cssText = "width: 800px; height: 600px; overflow: auto;";
33 document.body.append(root);
34 const view = new CodeView<undefined>({
35 diffStyle: "unified",
36 loadDiffFiles: async () => ({
37 oldFile: { name: "notes.txt", contents: `${OLD_LINES.join("\n")}\n` },
38 newFile: { name: "notes.txt", contents: `${NEW_LINES.join("\n")}\n` },
39 }),
40 });
41 using _view = {
42 [Symbol.dispose]: () => {
43 view.cleanUp();
44 root.remove();
45 },
46 };
47 view.setup(root);
48 const shadowRoot = () => root.querySelector("diffs-container")?.shadowRoot;
49 const lines = () =>
50 Array.from(shadowRoot()?.querySelectorAll("[data-line]") ?? [], (line) => line.textContent);
51​
52 view.setItems([{ id: "notes", type: "diff", fileDiff: parsePartialDiff(), version: 1 }]);
53 await vi.waitFor(() => expect(lines()).toContain("line 57"));
54 view.setItems([{ id: "notes", type: "diff", fileDiff: parsePartialDiff(), version: 2 }]);
55​
56 shadowRoot()!.querySelector<HTMLElement>("[data-expand-button]")!.click();
57​
58 await vi.waitFor(() => expect(lines()).toContain("line 30"));
59});