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

pullRequestDiff.logic.test.ts

86 lines3.4 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 getFileViewedRange,
6 isFileDiffCollapsed,
7 isLineInFileDiff,
8 withFileDiffFold,
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 choices = new Map([["b.ts", true]]);
47 expect(isFileDiffCollapsed("b.ts", "expanded", choices)).toBe(true);
48 expect(isFileDiffCollapsed("c.ts", "expanded", choices)).toBe(false);
49 });
50​
51 it("folds a file marked Viewed when the Code tab mounts again with no fold choices", () => {
52 expect(isFileDiffCollapsed("a.ts", "expanded", new Map(), true)).toBe(true);
53 expect(isFileDiffCollapsed("a.ts", "expanded", new Map(), false)).toBe(false);
54 });
55​
56 it("keeps a reader's choice when the file's Viewed mark loads later", () => {
57 const choices = new Map([["a.ts", false]]);
58 expect(isFileDiffCollapsed("a.ts", "expanded", choices, false)).toBe(false);
59 expect(isFileDiffCollapsed("a.ts", "expanded", choices, true)).toBe(false);
60 });
61});
62​
63describe("withFileDiffFold", () => {
64 it("records an absolute choice and keeps the same map when nothing changes", () => {
65 const folded = withFileDiffFold(new Map(), "a.ts", true);
66 expect(folded.get("a.ts")).toBe(true);
67 expect(withFileDiffFold(folded, "a.ts", true)).toBe(folded);
68 expect(withFileDiffFold(folded, "a.ts", false).get("a.ts")).toBe(false);
69 });
70});
71​
72describe("getFileViewedRange", () => {
73 const files = ["c.ts", "a.ts", "b.ts", "d.ts"].map((fileKey) => ({ fileKey }));
74​
75 it("includes both endpoints in displayed order in either direction", () => {
76 expect(getFileViewedRange(files, "b.ts", "c.ts")).toEqual(files.slice(0, 3));
77 expect(getFileViewedRange(files, "c.ts", "b.ts")).toEqual(files.slice(0, 3));
78 expect(getFileViewedRange(files, "a.ts", "a.ts")).toEqual([files[1]]);
79 });
80​
81 it("changes only the clicked file without a current anchor", () => {
82 expect(getFileViewedRange(files, "a.ts", null)).toEqual([files[1]]);
83 expect(getFileViewedRange(files, "a.ts", "removed.ts")).toEqual([files[1]]);
84 expect(getFileViewedRange(files, "removed.ts", "a.ts")).toEqual([]);
85 });
86});