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

pullRequestDiff.logic.test.ts

56 lines2.1 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("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 // regression: #9174 diffs now expand by default; a reader's fold must survive new slices
44 it("keeps a file the reader folded closed as the next slice arrives", () => {
45 const toggled = new Set(["b.ts"]);
46 expect(isFileDiffCollapsed("b.ts", null, toggled)).toBe(true);
47 expect(isFileDiffCollapsed("c.ts", null, toggled)).toBe(false);
48 });
49});
50​
51describe("toggleFileDiffFoldForViewed", () => {
52 it("moves against whatever the toolbar last asked for", () => {
53 expect([...toggleFileDiffFoldForViewed("a.ts", true, "expanded", new Set())]).toEqual(["a.ts"]);
54 expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0);
55 });
56});