Skip to content
Source
Sign inSign up

gitActions.test.ts

96 lines2.9 KB

1import { SourceControlProviderKind, type VcsStatusResult } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { sourceControlClients } from "../sourceControlClients.ts";
5import {
6 buildMenuItems,
7 getGitActionDisabledReason,
8 resolveDefaultBranchActionDialogCopy,
9 resolveQuickAction,
10} from "./gitActions.ts";
11​
12function status(overrides: Partial<VcsStatusResult> = {}): VcsStatusResult {
13 return {
14 isRepo: true,
15 hasPrimaryRemote: true,
16 isDefaultRef: false,
17 refName: "feature/test",
18 hasWorkingTreeChanges: false,
19 workingTree: { files: [], insertions: 0, deletions: 0 },
20 hasUpstream: true,
21 aheadCount: 0,
22 behindCount: 0,
23 pr: null,
24 ...overrides,
25 };
27​
28const gitLab = {
29 sourceControlProvider: {
30 kind: SourceControlProviderKind.make("gitlab"),
31 name: "GitLab",
32 baseUrl: "https://gitlab.com",
33 },
34};
35​
36const openPr = {
37 number: 7,
38 title: "Feature",
39 url: "https://example.com/pr/7",
40 baseRef: "main",
41 headRef: "feature/test",
42 state: "open" as const,
43};
44​
45describe("git action copy", () => {
46 it("says PR for a GitHub ref", () => {
47 expect(resolveQuickAction(status({ aheadCount: 1 }), false).label).toBe("Push & create PR");
48 expect(resolveQuickAction(status({ pr: openPr }), false).label).toBe("View PR");
49 const [, , prItem] = buildMenuItems(status(), false);
50 expect(prItem?.label).toBe("Create PR");
51 expect(
52 getGitActionDisabledReason({
53 item: prItem!,
54 gitStatus: status(),
55 isBusy: false,
56 hasOriginRemote: true,
57 }),
58 ).toBe("No local commits to include in a PR.");
59 });
60​
61 it("says MR for a GitLab ref", () => {
62 expect(resolveQuickAction(status({ ...gitLab, aheadCount: 1 }), false).label).toBe(
63 "Push & create MR",
64 );
65 expect(
66 resolveQuickAction(status({ ...gitLab, hasWorkingTreeChanges: true }), false).label,
67 ).toBe("Commit, push & MR");
68 expect(resolveQuickAction(status({ ...gitLab, pr: openPr }), false).label).toBe("View MR");
69 const [, , prItem] = buildMenuItems(status(gitLab), false);
70 expect(prItem?.label).toBe("Create MR");
71 expect(
72 getGitActionDisabledReason({
73 item: prItem!,
74 gitStatus: status(gitLab),
75 isBusy: false,
76 hasOriginRemote: true,
77 }),
78 ).toBe("No local commits to include in an MR.");
79 });
80​
81 it("names the host's change request in the default-branch dialog", () => {
82 const input = { action: "commit_push_pr", branchName: "main", includesCommit: true } as const;
83 expect(
84 resolveDefaultBranchActionDialogCopy({
85 ...input,
86 terminology: sourceControlClients.get("github").changeRequest,
87 }).title,
88 ).toBe("Commit, push & create PR from default branch?");
89 expect(
90 resolveDefaultBranchActionDialogCopy({
91 ...input,
92 terminology: sourceControlClients.get("gitlab").changeRequest,
93 }).continueLabel,
94 ).toBe("Commit, push & create MR");
95 });
96});