-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.test.js
219 lines (172 loc) · 6.25 KB
/
todo.test.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
const fs = require("fs");
const { execSync } = require("child_process");
let deleteFile = (path) => {
try {
fs.unlinkSync(path);
} catch (err) {}
};
beforeEach(() => {
deleteFile(`${__dirname}/todo.txt`);
deleteFile(`${__dirname}/done.txt`);
});
let todoTxtCli = (...args) => [`${__dirname}/todo`, ...args].join(" ");
let usage = `Usage :-
$ ./todo add "todo item" # Add a new todo
$ ./todo ls # Show remaining todos
$ ./todo del NUMBER # Delete a todo
$ ./todo done NUMBER # Complete a todo
$ ./todo help # Show usage
$ ./todo report # Statistics`;
test("prints help when no additional args are provided", () => {
let received = execSync(todoTxtCli()).toString("utf8");
expect(received).toEqual(expect.stringContaining(usage));
});
test("prints help", () => {
let received = execSync(todoTxtCli("help")).toString("utf8");
expect(received).toEqual(expect.stringContaining(usage));
});
test("add a single todo", () => {
let expected = 'Added todo: "the thing i need to do"';
let received = execSync(
todoTxtCli("add", '"the thing i need to do"')
).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("show error message when add is not followed by a todo", () => {
let expected = "Error: Missing todo string. Nothing added!";
let received = execSync(todoTxtCli("add")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("add multiple todos", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo, i) => {
let expected = `Added todo: "${todo}"`;
let received = execSync(todoTxtCli("add", `"${todo}"`)).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
});
test("list todos in reverse order (added latest first)", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = `[3] find needle in the haystack
[2] water the plants
[1] the thing i need to do
`;
let received = execSync(todoTxtCli("ls")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("list when there are no remaining todos", () => {
let expected = `There are no pending todos!`;
let received = execSync(todoTxtCli("ls")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("delete a todo", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = "Deleted todo #2";
let received = execSync(todoTxtCli("del", "2")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("delete todos numbered 3, 2 & 1", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
[3, 2, 1].forEach((n) => {
let expected = `Deleted todo #${n}`;
let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
});
test("delete first todo item 3 times", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
[1, 1, 1].forEach((n) => {
let expected = `Deleted todo #${n}`;
let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
});
test("delete non-existent todos", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
[0, 4, 5].forEach((n) => {
let expected = `Error: todo #${n} does not exist. Nothing deleted.`;
let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
});
test("delete does not have enough arguments", () => {
let expected = "Error: Missing NUMBER for deleting todo.";
let received = execSync(todoTxtCli("del")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("mark a todo as done", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = "Marked todo #2 as done.";
let received = execSync(todoTxtCli("done", "2")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("mark as done a todo which does not exist", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = "Error: todo #0 does not exist.";
let received = execSync(todoTxtCli("done", "0")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("mark as done without providing a todo number", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = "Error: Missing NUMBER for marking todo as done.";
let received = execSync(todoTxtCli("done")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});
test("report pending & completed todos", () => {
let todos = [
"the thing i need to do",
"water the plants",
"find needle in the haystack",
];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
execSync(todoTxtCli("done", "1"));
execSync(todoTxtCli("done", "2"));
let date = new Date();
let expected = `${date.toISOString().slice(0, 10)} Pending : 1 Completed : 2`;
let received = execSync(todoTxtCli("report")).toString("utf8");
expect(received).toEqual(expect.stringContaining(expected));
});