-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.spec.js
267 lines (238 loc) · 7.1 KB
/
cli.spec.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const path = require("path");
const { spawnSync } = require("child_process");
const { description, name } = require("@pack");
const Developer = require("Models/developer");
const { developers, setupDatabase, turnOffDatabase } = require("Fix/db");
const appFilePath = path.join(__dirname, "../bin/index.js");
beforeEach(setupDatabase);
afterAll(turnOffDatabase);
describe("When invoqued", () => {
describe("Without any options", () => {
it("Displays help", () => {
const app = spawnSync("node", [appFilePath]);
expect(app.output.toString()).toContain(description);
expect(app.output.toString()).toContain(name);
});
});
describe("With an unexpected option", () => {
it("Throws a helpful error", () => {
const app = spawnSync("node", [appFilePath, "-bla"]);
expect(app.stdout.toString()).toBeFalsy();
expect(app.stderr.toString()).toContain("unknown option");
});
});
});
// TODO: Tests for importJSON command
// TODO: Better output checks in tests
// TODO: Understand why files that are actually covered by the suit get 0% of coverage
describe("addDeveloper", () => {
const command = "addDeveloper";
const { name, email, phone, category, dates } = developers[1];
describe("With all the expected options", () => {
it("Adds the new developer to the database", async () => {
let developer = await Developer.findOne({ email });
expect(developer).toBeNull();
const app = spawnSync("node", [
appFilePath,
command,
"-n",
name,
"-e",
email,
"-p",
phone,
"-c",
category,
"-d",
dates.join(","),
]);
expect(app.status).toBe(1);
developer = await Developer.findOne({ email });
expect(developer).not.toBeNull();
expect(developer.dates).toHaveLength(developers[1].dates.length);
});
});
describe("Without dates", () => {
it("Adds the new developer assuming that they're attending to the whole event", async () => {
let developer = await Developer.findOne({ email });
expect(developer).toBeNull();
const app = spawnSync("node", [
appFilePath,
command,
"-n",
name,
"-e",
email,
"-p",
phone,
"-c",
category,
]);
expect(app.status).toBe(1);
developer = await Developer.findOne({ email });
expect(developer).not.toBeNull();
expect(developer.dates).toHaveLength(4);
});
});
describe("With missing argument", () => {
it("Throws a helpful error and doesn't add a new developer", async () => {
const app = spawnSync("node", [
appFilePath,
command,
"-e",
email,
"-p",
phone,
"-c",
category,
]);
expect(app.status).toBe(0);
expect(app.stdout.toString()).toBe("");
expect(app.stderr.toString()).toContain("Path `name` is required");
const developer = await Developer.findOne({ email });
expect(developer).toBeNull();
});
});
describe("With invalid", () => {
describe("Name", () => {
it("Explains the problem and doesn't add the developer", async () => {
const app = spawnSync("node", [
appFilePath,
command,
"-n",
"",
"-e",
email,
"-p",
phone,
"-c",
category,
]);
expect(app.status).toBe(0);
expect(app.stdout.toString()).toBe("");
expect(app.stderr.toString()).toContain("Path `name` is required");
const developer = await Developer.findOne({ email });
expect(developer).toBeNull();
});
});
describe("Email", () => {
it("Explains the problem and doesn't add the developer", async () => {
const prevNum = await Developer.countDocuments({});
const app = spawnSync("node", [
appFilePath,
command,
"-n",
name,
"-e",
"not an email",
"-p",
phone,
"-c",
category,
]);
expect(app.status).toBe(0);
expect(app.stdout.toString()).toBe("");
expect(app.stderr.toString()).toContain(
"The email address isn't valid"
);
const postNum = await Developer.countDocuments({});
expect(prevNum).toBe(postNum);
});
});
describe("Telephone number", () => {
it("Explains the problem and doesn't add the developer", async () => {
const prevNum = await Developer.countDocuments({});
const app = spawnSync("node", [
appFilePath,
command,
"-n",
name,
"-e",
email,
"-p",
"563",
"-c",
category,
]);
expect(app.status).toBe(0);
expect(app.stdout.toString()).toBe("");
expect(app.stderr.toString()).toContain("The phone number isn't valid");
const postNum = await Developer.countDocuments({});
expect(prevNum).toBe(postNum);
});
});
describe("Category", () => {
it("Explains the problem and doesn't add the developer", async () => {
const prevNum = await Developer.countDocuments({});
const app = spawnSync("node", [
appFilePath,
command,
"-n",
name,
"-e",
email,
"-p",
phone,
"-c",
"whatever",
]);
expect(app.status).toBe(0);
expect(app.stdout.toString()).toBe("");
expect(app.stderr.toString()).toContain(
"not a valid enum value for path `category`"
);
const postNum = await Developer.countDocuments({});
expect(prevNum).toBe(postNum);
});
});
describe("Dates", () => {
it("Explains the problem and doesn't add the developer", async () => {
const prevNum = await Developer.countDocuments({});
const app = spawnSync("node", [
appFilePath,
command,
"-n",
name,
"-e",
email,
"-p",
phone,
"-c",
category,
"-d",
"2021-03-09",
]);
expect(app.status).toBe(0);
expect(app.stdout.toString()).toBe("");
const postNum = await Developer.countDocuments({});
expect(prevNum).toBe(postNum);
});
});
});
describe("With duplicated email", () => {
it("Explains the problem and doesn't add the developer", async () => {
const preDeveloper = await Developer.findOne({
email: developers[0].email,
});
const app = spawnSync("node", [
appFilePath,
command,
"-n",
name,
"-e",
developers[0].email,
"-p",
phone,
"-c",
category,
]);
expect(app.status).toBe(0);
expect(app.stdout.toString()).toBe("");
expect(app.stderr.toString()).toContain("duplicate key");
const postDeveloper = await Developer.findOne({
email: developers[0].email,
});
expect(preDeveloper.toObject()).toEqual(postDeveloper.toObject());
});
});
});