Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 isFileDiffCollapsed,
6 isLineInFileDiff,
7 toggleFileDiffFoldForViewed,
8} from "./pullRequestDiff.logic";
9
10function fileWithHunks(
11 hunks: ReadonlyArray<{
12 deletionStart: number;
13 deletionCount: number;
14 additionStart: number;
15 additionCount: number;
16 }>,
17): FileDiffMetadata {
18 return { name: "src/app.ts", hunks } as unknown as FileDiffMetadata;
19}
20
21describe("isLineInFileDiff", () => {
22 const file = fileWithHunks([
23 { deletionStart: 10, deletionCount: 3, additionStart: 10, additionCount: 5 },
24 { deletionStart: 40, deletionCount: 0, additionStart: 42, additionCount: 2 },
25 ]);
26
27 it("includes the first line of a hunk and excludes the one past its last", () => {
28 expect(isLineInFileDiff(file, "right", 10)).toBe(true);
29 expect(isLineInFileDiff(file, "right", 14)).toBe(true);
30 expect(isLineInFileDiff(file, "right", 15)).toBe(false);
31 expect(isLineInFileDiff(file, "left", 9)).toBe(false);
32 expect(isLineInFileDiff(file, "left", 12)).toBe(true);
33 expect(isLineInFileDiff(file, "left", 13)).toBe(false);
34 });
35
36 it("keeps the two sides apart, since one line number means two lines", () => {
37 expect(isLineInFileDiff(file, "right", 43)).toBe(true);
38 expect(isLineInFileDiff(file, "left", 40)).toBe(false);
39 });
40});
41
42describe("isFileDiffCollapsed", () => {
43 it("keeps a file the reader folded closed as the next slice arrives", () => {
44 const toggled = new Set(["b.ts"]);
45 expect(isFileDiffCollapsed("b.ts", null, toggled)).toBe(true);
46 expect(isFileDiffCollapsed("c.ts", null, toggled)).toBe(false);
47 });
48});
49
50describe("toggleFileDiffFoldForViewed", () => {
51 it("puts a file away when it is ticked off", () => {
52 expect([...toggleFileDiffFoldForViewed("a.ts", true, null, new Set())]).toEqual(["a.ts"]);
53 });
54
55 it("moves against whatever the toolbar last asked for", () => {
56 expect([...toggleFileDiffFoldForViewed("a.ts", true, "expanded", new Set())]).toEqual(["a.ts"]);
57 expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0);
58 });
59
60 it("touches only the file that was ticked", () => {
61 const toggled = new Set(["a.ts", "b.ts"]);
62 expect([...toggleFileDiffFoldForViewed("a.ts", false, null, toggled)]).toEqual(["b.ts"]);
63 });
64});