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

pullRequestDiff.logic.test.ts

73 lines2.8 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 getFileViewedRange,
6 isFileDiffCollapsed,
7 isLineInFileDiff,
8 toggleFileDiffFoldForViewed,
9} from "./pullRequestDiff.logic";
10​
11function fileWithHunks(
12 hunks: ReadonlyArray<{
13 deletionStart: number;
14 deletionCount: number;
15 additionStart: number;
16 additionCount: number;
17 }>,
18): FileDiffMetadata {
19 return { name: "src/app.ts", hunks } as unknown as FileDiffMetadata;
21​
22describe("isLineInFileDiff", () => {
23 const file = fileWithHunks([
24 { deletionStart: 10, deletionCount: 3, additionStart: 10, additionCount: 5 },
25 { deletionStart: 40, deletionCount: 0, additionStart: 42, additionCount: 2 },
26 ]);
27​
28 it("includes the first line of a hunk and excludes the one past its last", () => {
29 expect(isLineInFileDiff(file, "right", 10)).toBe(true);
30 expect(isLineInFileDiff(file, "right", 14)).toBe(true);
31 expect(isLineInFileDiff(file, "right", 15)).toBe(false);
32 expect(isLineInFileDiff(file, "left", 9)).toBe(false);
33 expect(isLineInFileDiff(file, "left", 12)).toBe(true);
34 expect(isLineInFileDiff(file, "left", 13)).toBe(false);
35 });
36​
37 it("keeps the two sides apart, since one line number means two lines", () => {
38 expect(isLineInFileDiff(file, "right", 43)).toBe(true);
39 expect(isLineInFileDiff(file, "left", 40)).toBe(false);
40 });
41});
42​
43describe("isFileDiffCollapsed", () => {
44 // regression: #9174 diffs now expand by default; a reader's fold must survive new slices
45 it("keeps a file the reader folded closed as the next slice arrives", () => {
46 const toggled = new Set(["b.ts"]);
47 expect(isFileDiffCollapsed("b.ts", null, toggled)).toBe(true);
48 expect(isFileDiffCollapsed("c.ts", null, toggled)).toBe(false);
49 });
50});
51​
52describe("toggleFileDiffFoldForViewed", () => {
53 it("moves against whatever the toolbar last asked for", () => {
54 expect([...toggleFileDiffFoldForViewed("a.ts", true, "expanded", new Set())]).toEqual(["a.ts"]);
55 expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0);
56 });
57});
58​
59describe("getFileViewedRange", () => {
60 const files = ["c.ts", "a.ts", "b.ts", "d.ts"].map((fileKey) => ({ fileKey }));
61​
62 it("includes both endpoints in displayed order in either direction", () => {
63 expect(getFileViewedRange(files, "b.ts", "c.ts")).toEqual(files.slice(0, 3));
64 expect(getFileViewedRange(files, "c.ts", "b.ts")).toEqual(files.slice(0, 3));
65 expect(getFileViewedRange(files, "a.ts", "a.ts")).toEqual([files[1]]);
66 });
67​
68 it("changes only the clicked file without a current anchor", () => {
69 expect(getFileViewedRange(files, "a.ts", null)).toEqual([files[1]]);
70 expect(getFileViewedRange(files, "a.ts", "removed.ts")).toEqual([files[1]]);
71 expect(getFileViewedRange(files, "removed.ts", "a.ts")).toEqual([]);
72 });
73});