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

pullRequestDiff.logic.test.ts

104 lines4.0 KB

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;
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("places a line inside a hunk, on the side that hunk counts", () => {
28 expect(isLineInFileDiff(file, "right", 12)).toBe(true);
29 expect(isLineInFileDiff(file, "left", 11)).toBe(true);
30 });
31​
32 it("includes the first line of a hunk and excludes the one past its last", () => {
33 expect(isLineInFileDiff(file, "right", 10)).toBe(true);
34 expect(isLineInFileDiff(file, "right", 14)).toBe(true);
35 expect(isLineInFileDiff(file, "right", 15)).toBe(false);
36 expect(isLineInFileDiff(file, "left", 9)).toBe(false);
37 expect(isLineInFileDiff(file, "left", 12)).toBe(true);
38 expect(isLineInFileDiff(file, "left", 13)).toBe(false);
39 });
40​
41 it("keeps the two sides apart, since one line number means two lines", () => {
42 expect(isLineInFileDiff(file, "right", 43)).toBe(true);
43 expect(isLineInFileDiff(file, "left", 40)).toBe(false);
44 });
45​
46 it("places nothing in a file whose hunks the host withheld", () => {
47 expect(isLineInFileDiff(fileWithHunks([]), "right", 1)).toBe(false);
48 });
49});
50​
51describe("isFileDiffCollapsed", () => {
52 const NO_TOGGLES: ReadonlySet<string> = new Set();
53​
54 it("opens every file before the reader has touched anything", () => {
55 expect(isFileDiffCollapsed("a.ts", null, NO_TOGGLES)).toBe(false);
56 expect(isFileDiffCollapsed("b.ts", null, NO_TOGGLES)).toBe(false);
57 });
58​
59 it("opens every file once the toolbar has asked for it", () => {
60 expect(isFileDiffCollapsed("a.ts", "expanded", NO_TOGGLES)).toBe(false);
61 expect(isFileDiffCollapsed("b.ts", "expanded", NO_TOGGLES)).toBe(false);
62 });
63​
64 it("folds every file again on the second press", () => {
65 expect(isFileDiffCollapsed("a.ts", "folded", NO_TOGGLES)).toBe(true);
66 expect(isFileDiffCollapsed("b.ts", "folded", NO_TOGGLES)).toBe(true);
67 });
68​
69 it("keeps a file the reader folded closed as the next slice arrives", () => {
70 const toggled = new Set(["b.ts"]);
71 expect(isFileDiffCollapsed("b.ts", null, toggled)).toBe(true);
72 expect(isFileDiffCollapsed("c.ts", null, toggled)).toBe(false);
73 });
74​
75 it("still answers to a toggle after either toolbar press", () => {
76 expect(isFileDiffCollapsed("a.ts", "expanded", new Set(["a.ts"]))).toBe(true);
77 expect(isFileDiffCollapsed("a.ts", "folded", new Set(["a.ts"]))).toBe(false);
78 });
79});
80​
81describe("toggleFileDiffFoldForViewed", () => {
82 it("puts a file away when it is ticked off", () => {
83 expect([...toggleFileDiffFoldForViewed("a.ts", true, null, new Set())]).toEqual(["a.ts"]);
84 });
85​
86 it("brings a file back when the tick is taken off", () => {
87 expect([...toggleFileDiffFoldForViewed("a.ts", false, null, new Set(["a.ts"]))]).toEqual([]);
88 });
89​
90 it("leaves the fold alone when it already says what the tick does", () => {
91 const folded = new Set(["a.ts"]);
92 expect(toggleFileDiffFoldForViewed("a.ts", true, null, folded)).toBe(folded);
93 });
94​
95 it("moves against whatever the toolbar last asked for", () => {
96 expect([...toggleFileDiffFoldForViewed("a.ts", true, "expanded", new Set())]).toEqual(["a.ts"]);
97 expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0);
98 });
99​
100 it("touches only the file that was ticked", () => {
101 const toggled = new Set(["a.ts", "b.ts"]);
102 expect([...toggleFileDiffFoldForViewed("a.ts", false, null, toggled)]).toEqual(["b.ts"]);
103 });
104});