Skip to content

Commit 21f7303

Browse files
test: added a few new test files (#536)
## PR Checklist - [x] Addresses an existing open issue: makes progress on #471 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Adds a trio of test files. Doesn't exhaustively increase coverage (I want to leave that open to future contributors!), but hopefully gets us to a nicer green coverage badge in the README.md
1 parent 9d626e6 commit 21f7303

File tree

5 files changed

+182
-1
lines changed

5 files changed

+182
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Octokit } from "octokit";
2+
import { SpyInstance, describe, expect, it, vi } from "vitest";
3+
4+
import { hydrateBranchProtectionSettings } from "./hydrateBranchProtectionSettings.js";
5+
6+
const createMockOctokit = (request: SpyInstance) =>
7+
({
8+
request,
9+
} as unknown as Octokit);
10+
11+
const stubValues = { owner: "", repository: "" };
12+
13+
describe("hydrateBranchProtectionSettings", () => {
14+
it("returns false when the request receives a 403 response", async () => {
15+
const mockRequest = vi.fn().mockRejectedValue({ status: 403 });
16+
17+
const actual = await hydrateBranchProtectionSettings(
18+
createMockOctokit(mockRequest),
19+
stubValues
20+
);
21+
22+
expect(actual).toBe(false);
23+
});
24+
25+
it("throws the error when the request throws with a non-403 response", async () => {
26+
const error = { status: 404 };
27+
const mockRequest = vi.fn().mockRejectedValue(error);
28+
29+
await expect(() =>
30+
hydrateBranchProtectionSettings(
31+
createMockOctokit(mockRequest),
32+
stubValues
33+
)
34+
).rejects.toBe(error);
35+
});
36+
});
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
import { hydrateRepositoryLabels } from "./hydrateRepositoryLabels.js";
4+
5+
const mock$ = vi.fn();
6+
7+
vi.mock("execa", () => ({
8+
get $() {
9+
return mock$;
10+
},
11+
}));
12+
13+
const mockReadFileAsJson = vi.fn().mockResolvedValue([
14+
{
15+
color: "000000",
16+
description: "def ghi",
17+
name: "abc",
18+
},
19+
]);
20+
21+
vi.mock("../../../shared/readFileAsJson.js", () => ({
22+
get readFileAsJson() {
23+
return mockReadFileAsJson;
24+
},
25+
}));
26+
27+
describe("hydrateRepositoryLabels", () => {
28+
it("creates a setup label when it doesn't already exist", async () => {
29+
mock$.mockResolvedValue({
30+
stdout: JSON.stringify([
31+
{
32+
color: "000000",
33+
description: "def ghi",
34+
name: "other",
35+
},
36+
]),
37+
});
38+
39+
await hydrateRepositoryLabels();
40+
41+
expect(mock$).toHaveBeenCalledWith(
42+
["gh label ", " ", " --color ", " --description ", ""],
43+
"create",
44+
"abc",
45+
"000000",
46+
"def ghi"
47+
);
48+
});
49+
50+
it("edits a setup label when it already exists", async () => {
51+
mock$.mockResolvedValue({
52+
stdout: JSON.stringify([
53+
{
54+
color: "000000",
55+
description: "def ghi",
56+
name: "abc",
57+
},
58+
]),
59+
});
60+
61+
await hydrateRepositoryLabels();
62+
63+
expect(mock$).toHaveBeenCalledWith(
64+
["gh label ", " ", " --color ", " --description ", ""],
65+
"edit",
66+
"abc",
67+
"000000",
68+
"def ghi"
69+
);
70+
});
71+
72+
it("deletes a pre-existing label when it isn't a setup label", async () => {
73+
mock$.mockResolvedValue({
74+
stdout: JSON.stringify([
75+
{
76+
color: "000000",
77+
description: "def ghi",
78+
name: "unknown",
79+
},
80+
]),
81+
});
82+
83+
await hydrateRepositoryLabels();
84+
85+
expect(mock$).toHaveBeenCalledWith(
86+
["gh label delete ", " --yes"],
87+
"unknown"
88+
);
89+
expect(mock$).toHaveBeenCalledTimes(3);
90+
});
91+
92+
it("doesn't delete a pre-existing label when it isn't a setup label", async () => {
93+
mock$.mockResolvedValue({
94+
stdout: JSON.stringify([
95+
{
96+
color: "000000",
97+
description: "def ghi",
98+
name: "abc",
99+
},
100+
]),
101+
});
102+
103+
await hydrateRepositoryLabels();
104+
105+
expect(mock$).not.toHaveBeenCalledWith(
106+
["gh label delete ", " --yes"],
107+
"abc"
108+
);
109+
expect(mock$).toHaveBeenCalledTimes(2);
110+
});
111+
});

src/shared/getDefaultSettings.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
import { getDefaultSettings } from "./getDefaultSettings.js";
4+
5+
vi.mock("./cli/lines.js");
6+
7+
const mockGitRemoteOriginUrl = vi.fn();
8+
9+
vi.mock("git-remote-origin-url", () => ({
10+
get default() {
11+
return mockGitRemoteOriginUrl;
12+
},
13+
}));
14+
15+
describe("getDefaultSettings", () => {
16+
it("returns the retrieved owner and repository when gitRemoteOriginUrl succeeds", async () => {
17+
mockGitRemoteOriginUrl.mockResolvedValue("https://github.com/abc/def");
18+
19+
const settings = await getDefaultSettings();
20+
21+
expect(settings).toEqual({ defaultOwner: "abc", defaultRepository: "def" });
22+
});
23+
24+
it("returns arbitrary owner and repository defaults when gitRemoteOriginUrl rejects", async () => {
25+
mockGitRemoteOriginUrl.mockRejectedValue(new Error("Oh no!"));
26+
27+
const settings = await getDefaultSettings();
28+
29+
expect(settings).toEqual({
30+
defaultOwner: "UserName",
31+
defaultRepository: "my-lovely-repository",
32+
});
33+
});
34+
});
File renamed without changes.

src/shared/inputs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Octokit } from "octokit";
33
import { titleCase } from "title-case";
44

55
import { PrefillPrompter } from "./PrefillPrompter.js";
6-
import { getDefaultSettings } from "./defaults.js";
76
import { ensureRepositoryExists } from "./ensureRepositoryExists.js";
7+
import { getDefaultSettings } from "./getDefaultSettings.js";
88
import { getOctokit } from "./getOctokit.js";
99
import { optionalDefault } from "./optionalDefault.js";
1010

0 commit comments

Comments
 (0)