Skip to content
This repository was archived by the owner on Jun 10, 2022. It is now read-only.

Commit f4f5ca9

Browse files
committedNov 23, 2020
test: add delete-branch tests
1 parent 2593011 commit f4f5ca9

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
 

‎spec/commands/delete-branch-spec.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/** @babel */
2+
3+
import {Directory} from "atom";
4+
import deleteBranch from "../../lib/commands/delete-branch";
5+
import Notifications from "../../lib/Notifications";
6+
import {getFilePath, statusBar, mockGit, mockDialog, removeGitRoot, createGitRoot, fileStatus, files} from "../mocks";
7+
8+
describe("delete-branch", function () {
9+
10+
beforeEach(async function () {
11+
await atom.packages.activatePackage("git-menu");
12+
this.gitRoot = await createGitRoot();
13+
14+
this.repo = await atom.project.repositoryForDirectory(new Directory(this.gitRoot));
15+
16+
this.statuses = [fileStatus("M ", files.t1), fileStatus("M ", files.t2)];
17+
this.filePaths = getFilePath(this.gitRoot, [files.t1]);
18+
this.branches = [{name: "deleteBranch", selected: true}, {name: "master", selected: false}];
19+
this.git = mockGit({
20+
rootDir: Promise.resolve(this.gitRoot),
21+
branches: Promise.resolve(this.branches),
22+
checkoutBranch: Promise.resolve("checkoutBranch result"),
23+
delete: Promise.resolve("delete result"),
24+
deleteBranch: Promise.resolve("deleteBranch result"),
25+
abort: Promise.resolve("abort result"),
26+
});
27+
this.dialogReturn = [
28+
{name: "deleteBranch", selected: false},
29+
true,
30+
false,
31+
false,
32+
];
33+
this.dialog = mockDialog({
34+
activate: () => Promise.resolve(this.dialogReturn),
35+
});
36+
});
37+
38+
afterEach(async function () {
39+
await removeGitRoot(this.gitRoot);
40+
});
41+
42+
describe("dialog", function () {
43+
44+
it("should call dialog with correct props", async function () {
45+
spyOn(this, "dialog").and.callThrough();
46+
try {
47+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
48+
} catch (ex) {
49+
// do nothing
50+
}
51+
expect(this.dialog).toHaveBeenCalledWith({
52+
branches: this.branches,
53+
root: this.gitRoot,
54+
});
55+
});
56+
57+
it("should call dialog.activate()", async function () {
58+
spyOn(this.dialog.prototype, "activate").and.callThrough();
59+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
60+
expect(this.dialog.prototype.activate).toHaveBeenCalled();
61+
});
62+
});
63+
64+
describe("cancel", function () {
65+
66+
it("should reject without an error", async function () {
67+
this.dialog = mockDialog({
68+
activate: () => Promise.reject(),
69+
});
70+
let error;
71+
try {
72+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
73+
} catch (ex) {
74+
error = !ex;
75+
}
76+
expect(error).toBeTruthy();
77+
});
78+
});
79+
80+
describe("delete", function () {
81+
it("should return on not local or remote", async function () {
82+
spyOn(this.git, "deleteBranch").and.callThrough();
83+
this.dialogReturn[1] = false;
84+
expect(this.git.deleteBranch).not.toHaveBeenCalled();
85+
});
86+
87+
it("should show deleting branch... in status bar", async function () {
88+
spyOn(statusBar, "show").and.callThrough();
89+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
90+
expect(statusBar.show).toHaveBeenCalledWith("Deleting Branch...");
91+
});
92+
93+
it("should not call git.checkoutBranch if root branch is not selected", async function () {
94+
spyOn(this.git, "checkoutBranch").and.callThrough();
95+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
96+
expect(this.git.checkoutBranch).not.toHaveBeenCalled();
97+
});
98+
99+
it("should call git.checkoutBranch if root branch is selected", async function () {
100+
this.dialogReturn[0].selected = true;
101+
spyOn(this.git, "checkoutBranch").and.callThrough();
102+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
103+
expect(this.git.checkoutBranch).toHaveBeenCalledWith(this.gitRoot, "master");
104+
});
105+
106+
it("should call git.deleteBranch with force", async function () {
107+
this.dialogReturn[2] = true;
108+
this.dialogReturn[3] = true;
109+
spyOn(this.git, "deleteBranch").and.callThrough();
110+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
111+
expect(this.git.deleteBranch).toHaveBeenCalledWith(this.gitRoot, this.dialogReturn[0].name, false, true);
112+
expect(this.git.deleteBranch).toHaveBeenCalledWith(this.gitRoot, this.dialogReturn[0].name, true, true);
113+
});
114+
115+
it("should call git.deleteBranch on local", async function () {
116+
this.dialogReturn[1] = true;
117+
this.dialogReturn[2] = false;
118+
spyOn(this.git, "deleteBranch").and.callThrough();
119+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
120+
expect(this.git.deleteBranch).toHaveBeenCalledWith(this.gitRoot, this.dialogReturn[0].name, false, false);
121+
expect(this.git.deleteBranch).toHaveBeenCalledTimes(1);
122+
});
123+
124+
it("should call git.deleteBranch on remote", async function () {
125+
this.dialogReturn[1] = false;
126+
this.dialogReturn[2] = true;
127+
spyOn(this.git, "deleteBranch").and.callThrough();
128+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
129+
expect(this.git.deleteBranch).toHaveBeenCalledWith(this.gitRoot, this.dialogReturn[0].name, true, false);
130+
expect(this.git.deleteBranch).toHaveBeenCalledTimes(1);
131+
});
132+
133+
it("should call git.deleteBranch on local and remote", async function () {
134+
this.dialogReturn[1] = true;
135+
this.dialogReturn[2] = true;
136+
spyOn(this.git, "deleteBranch").and.callThrough();
137+
await deleteBranch.command(this.filePaths, statusBar, this.git, Notifications, this.dialog);
138+
expect(this.git.deleteBranch).toHaveBeenCalledWith(this.gitRoot, this.dialogReturn[0].name, true, false);
139+
expect(this.git.deleteBranch).toHaveBeenCalledWith(this.gitRoot, this.dialogReturn[0].name, false, false);
140+
expect(this.git.deleteBranch).toHaveBeenCalledTimes(2);
141+
});
142+
});
143+
});

0 commit comments

Comments
 (0)
This repository has been archived.