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

pullRequestFileOrder.logic.test.ts

141 lines4.2 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { orderDiffFiles } from "./pullRequestFileOrder.logic";
5​
6function file(name: string, additionLines: ReadonlyArray<string> = []): FileDiffMetadata {
7 return { name, hunks: [], additionLines, deletionLines: [] } as unknown as FileDiffMetadata;
8}
9​
10function order(files: ReadonlyArray<FileDiffMetadata>): Array<string> {
11 return orderDiffFiles(files).map((entry) => entry.name);
13​
14describe("orderDiffFiles", () => {
15 it("places source before tests and generated files across path conventions", () => {
16 const source = ["src/app.ts", "src/dist.ts", "src/testing.ts"];
17 const tests = [
18 "src/__tests__/app.ts",
19 "src/app.spec.tsx",
20 "src/app.test.ts",
21 "test/app.ts",
22 "tests/helpers/app.ts",
23 ];
24 const generated = [
25 "apps/web/package-lock.json",
26 "dist/app.js",
27 "packages/core/vendor/lib.js",
28 "pnpm-lock.yaml",
29 "public/app.min.js",
30 "src/__snapshots__/app.ts",
31 "src/api.generated.ts",
32 "src/app.test.ts.snap",
33 ];
34 expect(
35 order([...generated, ...tests, ...source].toReversed().map((path) => file(path))),
36 ).toEqual([...source, ...tests, ...generated]);
37 });
38​
39 it("resolves a specifier that climbs out of the importer's directory", () => {
40 expect(
41 order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]),
42 ).toEqual(["src/lib/b.ts", "src/ui/a.ts"]);
43 });
44​
45 it("resolves a directory specifier to that directory's index file", () => {
46 expect(
47 order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]),
48 ).toEqual(["src/lib/index.ts", "src/a.ts"]);
49 });
50​
51 it("falls back to the specifier's last segment when the path does not line up", () => {
52 expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
53 "lib/b.ts",
54 "src/a.ts",
55 ]);
56 });
57​
58 it("ignores an ambiguous last segment rather than guessing", () => {
59 expect(
60 order([
61 file("src/a.ts", ['import { b } from "~/somewhere/b";']),
62 file("one/b.ts"),
63 file("two/b.ts"),
64 ]),
65 ).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]);
66 });
67​
68 it("resolves a specifier naming an extension, including the .js beside a .ts", () => {
69 expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([
70 "src/b.ts",
71 "src/a.ts",
72 ]);
73 });
74​
75 it("reads require and bare imports too", () => {
76 expect(
77 order([
78 file("src/a.ts", ['const b = require("./b");', 'import "./c";']),
79 file("src/b.ts"),
80 file("src/c.ts"),
81 ]),
82 ).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]);
83 });
84​
85 it("falls back to path order inside a cycle", () => {
86 expect(
87 order([
88 file("src/b.ts", ['import { a } from "./a";']),
89 file("src/a.ts", ['import { b } from "./b";']),
90 ]),
91 ).toEqual(["src/a.ts", "src/b.ts"]);
92 });
93​
94 it("keeps the tiers apart and orders tests by the source they cover", () => {
95 expect(
96 order([
97 file("pnpm-lock.yaml"),
98 file("src/a.test.ts"),
99 file("src/__tests__/b.ts"),
100 file("src/a.ts", ['import { b } from "./b";']),
101 file("src/b.ts"),
102 file("dist/a.js"),
103 ]),
104 ).toEqual([
105 "src/b.ts",
106 "src/a.ts",
107 "src/__tests__/b.ts",
108 "src/a.test.ts",
109 "dist/a.js",
110 "pnpm-lock.yaml",
111 ]);
112 });
113​
114 it("puts a test with no source in the diff after the ones that have theirs", () => {
115 expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([
116 "src/a.ts",
117 "src/a.test.ts",
118 "src/orphan.spec.ts",
119 ]);
120 });
121​
122 it("pairs a test with the source whose name carries its own dotted suffix", () => {
123 expect(
124 order([
125 file("src/c.browser.test.tsx"),
126 file("src/b.test.ts"),
127 file("src/a.logic.test.ts"),
128 file("src/c.tsx"),
129 file("src/b.ts"),
130 file("src/a.logic.ts"),
131 ]),
132 ).toEqual([
133 "src/a.logic.ts",
134 "src/b.ts",
135 "src/c.tsx",
136 "src/a.logic.test.ts",
137 "src/b.test.ts",
138 "src/c.browser.test.tsx",
139 ]);
140 });
141});