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

pullRequestDiff.logic.test.ts

52 lines2.1 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { getFileViewedRange, isLineInFileDiff } from "./pullRequestDiff.logic";
5​
6function fileWithHunks(
7 hunks: ReadonlyArray<{
8 deletionStart: number;
9 deletionCount: number;
10 additionStart: number;
11 additionCount: number;
12 }>,
13): FileDiffMetadata {
14 return { name: "src/app.ts", hunks } as unknown as FileDiffMetadata;
16​
17describe("isLineInFileDiff", () => {
18 const file = fileWithHunks([
19 { deletionStart: 10, deletionCount: 3, additionStart: 10, additionCount: 5 },
20 { deletionStart: 40, deletionCount: 0, additionStart: 42, additionCount: 2 },
21 ]);
22​
23 it("includes the first line of a hunk and excludes the one past its last", () => {
24 expect(isLineInFileDiff(file, "right", 10)).toBe(true);
25 expect(isLineInFileDiff(file, "right", 14)).toBe(true);
26 expect(isLineInFileDiff(file, "right", 15)).toBe(false);
27 expect(isLineInFileDiff(file, "left", 9)).toBe(false);
28 expect(isLineInFileDiff(file, "left", 12)).toBe(true);
29 expect(isLineInFileDiff(file, "left", 13)).toBe(false);
30 });
31​
32 it("keeps the two sides apart, since one line number means two lines", () => {
33 expect(isLineInFileDiff(file, "right", 43)).toBe(true);
34 expect(isLineInFileDiff(file, "left", 40)).toBe(false);
35 });
36});
37​
38describe("getFileViewedRange", () => {
39 const files = ["c.ts", "a.ts", "b.ts", "d.ts"].map((fileKey) => ({ fileKey }));
40​
41 it("includes both endpoints in displayed order in either direction", () => {
42 expect(getFileViewedRange(files, "b.ts", "c.ts")).toEqual(files.slice(0, 3));
43 expect(getFileViewedRange(files, "c.ts", "b.ts")).toEqual(files.slice(0, 3));
44 expect(getFileViewedRange(files, "a.ts", "a.ts")).toEqual([files[1]]);
45 });
46​
47 it("changes only the clicked file without a current anchor", () => {
48 expect(getFileViewedRange(files, "a.ts", null)).toEqual([files[1]]);
49 expect(getFileViewedRange(files, "a.ts", "removed.ts")).toEqual([files[1]]);
50 expect(getFileViewedRange(files, "removed.ts", "a.ts")).toEqual([]);
51 });
52});