-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathcreateWithOptions.test.ts
200 lines (167 loc) · 5.21 KB
/
createWithOptions.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { Octokit } from "octokit";
import { describe, expect, it, vi } from "vitest";
import { SpinnerTask } from "../shared/cli/spinners.js";
import { doesRepositoryExist } from "../shared/doesRepositoryExist.js";
import { Options } from "../shared/types.js";
import { addToolAllContributors } from "../steps/addToolAllContributors.js";
import { finalizeDependencies } from "../steps/finalizeDependencies.js";
import { initializeGitHubRepository } from "../steps/initializeGitHubRepository/index.js";
import { runCleanup } from "../steps/runCleanup.js";
import { createWithOptions } from "./createWithOptions.js";
const optionsBase: Options = {
access: "public",
author: "Test Author",
base: "common",
description: "Test Description",
directory: "test-directory",
email: { github: "[email protected]", npm: "[email protected]" },
funding: "Test Funding",
keywords: ["test", "keywords"],
logo: { alt: "Test Alt", src: "test.png" },
mode: "create",
owner: "Test Owner",
repository: "test-repo",
title: "Test Title",
};
const mock$ = vi.fn();
vi.mock("execa", () => ({
get $() {
return mock$;
},
}));
const mockOctokit = new Octokit();
const github = {
auth: "auth-token",
octokit: mockOctokit,
};
vi.mock("../shared/cli/spinners.js", () => ({
withSpinner: async <Return>(label: string, task: SpinnerTask<Return>) => {
return await task();
},
withSpinners: async (
label: string,
tasks: [string, SpinnerTask<unknown>][],
) => {
for (const [, task] of tasks) {
await task();
}
},
}));
vi.mock("../steps/writing/writeStructure.js");
vi.mock("../steps/writeReadme/index.js");
vi.mock("../steps/finalizeDependencies.js");
vi.mock("../steps/clearLocalGitTags.js");
vi.mock("../steps/runCleanup.js");
vi.mock("../shared/doesRepositoryExist.js", () => ({
doesRepositoryExist: vi.fn().mockResolvedValue(true),
}));
vi.mock("../steps/initializeGitHubRepository/index.js", () => ({
initializeGitHubRepository: vi.fn().mockResolvedValue(true),
}));
vi.mock("../shared/getGitHubUserAsAllContributor.js", () => ({
getGitHubUserAsAllContributor: vi.fn().mockResolvedValue({
contributions: ["code", "doc"],
name: "Test User",
}),
}));
vi.mock("../steps/addToolAllContributors.js");
describe("createWithOptions", () => {
it("calls addToolAllContributors with options when excludeAllContributors is false", async () => {
const options = {
...optionsBase,
excludeAllContributors: false,
skipAllContributorsApi: false,
};
await createWithOptions({ github, options });
expect(addToolAllContributors).toHaveBeenCalledWith(
github.octokit,
options,
);
});
it("does not call addToolAllContributors when excludeAllContributors is true", async () => {
const options = {
...optionsBase,
excludeAllContributors: true,
};
await createWithOptions({ github, options });
expect(addToolAllContributors).not.toHaveBeenCalled();
});
it("does not call addToolAllContributors when skipAllContributorsApi is true", async () => {
const options = {
...optionsBase,
skipAllContributorsApi: true,
};
await createWithOptions({ github, options });
expect(addToolAllContributors).not.toHaveBeenCalled();
});
it("does not call finalizeDependencies or runCleanup when skipInstall is true", async () => {
const options = {
...optionsBase,
skipInstall: true,
};
await createWithOptions({ github, options });
expect(finalizeDependencies).not.toHaveBeenCalled();
expect(runCleanup).not.toHaveBeenCalled();
});
it("calls finalizeDependencies and runCleanup when skipInstall is false", async () => {
const options = {
...optionsBase,
skipInstall: false,
};
await createWithOptions({ github, options });
expect(finalizeDependencies).toHaveBeenCalledWith(options);
expect(runCleanup).toHaveBeenCalled();
});
it("does not initialize GitHub repository if repository does not exist", async () => {
const options = optionsBase;
vi.mocked(doesRepositoryExist).mockResolvedValueOnce(false);
await createWithOptions({ github, options });
expect(initializeGitHubRepository).not.toHaveBeenCalled();
expect(mock$).not.toHaveBeenCalled();
});
it("executes git commands when initializing GitHub repository when doesRepositoryExist is true", async () => {
const options = optionsBase;
vi.mocked(doesRepositoryExist).mockResolvedValueOnce(true);
await createWithOptions({ github, options });
expect(mock$.mock.calls).toMatchInlineSnapshot(`
[
[
[
"git remote add origin https://github.com/",
"/",
"",
],
"Test Owner",
"test-repo",
],
[
[
"git add -A",
],
],
[
[
"git commit --message ",
" --no-gpg-sign",
],
"feat: initialized repo ✨",
],
[
[
"git push -u origin main --force",
],
],
]
`);
});
it("calls doesRepositoryExist with github and options when doesRepositoryExist is true", async () => {
const options = optionsBase;
vi.mocked(doesRepositoryExist).mockResolvedValueOnce(true);
await createWithOptions({ github, options });
expect(doesRepositoryExist).toHaveBeenCalledWith(github.octokit, options);
expect(initializeGitHubRepository).toHaveBeenCalledWith(
github.octokit,
options,
);
});
});