-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddressBook_spec.js
245 lines (214 loc) · 7.86 KB
/
addressBook_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
"use strict";
/* eslint-env jasmine */
var jsonfile = require('jsonfile');
var fs = require('fs');
var child_process = require('child_process');
var file = 'data.json';
describe("addressBookCLI tests", function() {
var origData;
beforeEach(function() {
// Save and reset data before running test
if (fs.existsSync(file)) {
origData = jsonfile.readFileSync(file);
} else {
origData = [];
}
jsonfile.writeFileSync(file, []);
});
afterEach(function() {
// restore original data after running test
jsonfile.writeFileSync(file, origData);
});
describe("Displaying Contacts", function() {
it("displays all contacts in the correct format when contacts exist", function(){
jsonfile.writeFileSync(file, [
{
"name": "Moose",
"number": 123
},
{
"name": "Ricky",
"number": 456
}
]);
var stdout = runAndCleanStdout('node addressBook.js display');
expect(stdout.length).toBe(3);
var moose = stdout[1].split(/[ ]+/)
var ricky = stdout[2].split(/[ ]+/)
expect(moose[0]).toEqual('Moose')
expect(ricky[0]).toEqual('Ricky')
expect(moose[1]).toEqual('123')
expect(ricky[1]).toEqual('456')
})
it('displays the correct headers', function() {
jsonfile.writeFileSync(file, [
{
"name": "Moose",
"number": 123
},
{
"name": "Ricky",
"number": -1
}
]);
var stdout = runAndCleanStdout('node addressBook.js display');
var header = stdout[0].trim().split(/[ ]+/)
expect(header[0]).toEqual("CONTACT_NAME")
expect(header[1]).toEqual("PHONE_NUMBER")
})
it("displays '-None-' for the number field when contact does not have a phone number", function(){
jsonfile.writeFileSync(file, [
{
"name": "Moose",
"number": 123
},
{
"name": "Ricky",
"number": -1
}
]);
var stdout = runAndCleanStdout('node addressBook.js display');
var moose = stdout[1].split(/[ ]+/)
var ricky = stdout[2].split(/[ ]+/)
expect(moose[0]).toEqual('Moose')
expect(ricky[0]).toEqual('Ricky')
expect(moose[1]).toEqual('123')
expect(ricky[1]).toEqual('-None-')
})
})
describe("Adding Contacts", function() {
it("Adds contacts when a name and valid number(or no number) is provided", function() {
child_process.execSync('node addressBook.js add Pam 516');
child_process.execSync('node addressBook.js add Frankie 123');
child_process.execSync('node addressBook.js add Bob');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(3)
expect(data[0]).toEqual({"name": "Pam", "number": 516})
expect(data[1]).toEqual({"name": "Frankie", "number": 123})
expect(data[2]).toEqual({"name": "Bob", "number": -1})
});
it("Does not add contact when name or number is invalid", function() {
child_process.execSync('node addressBook.js add Pam 516');
child_process.execSync('node addressBook.js add Frankie a123');
child_process.execSync('node addressBook.js add 123 123');
child_process.execSync('node addressBook.js add abc123 123');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(1)
expect(data[0]).toEqual({"name": "Pam", "number": 516})
// expect(data[1]).toEqual({"name": "Frankie", "number": 123})
// expect(data[2]).toEqual({"name": "Bob", "number": -1})
});
it("Does not add contacts when no name is provided", function() {
child_process.execSync('node addressBook.js add 516');
child_process.execSync('node addressBook.js add');
child_process.execSync('node addressBook.js add Frankie 123');
child_process.execSync('node addressBook.js add Bob');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(2)
expect(data[0]).toEqual({"name": "Frankie", "number": 123})
expect(data[1]).toEqual({"name": "Bob", "number": -1})
});
});
describe("Updating Contacts", function() {
beforeEach(function() {
//resets data before all tests
jsonfile.writeFileSync(file, [
{
"name": "Moose",
"number": 123
},
{
"name": "Ricky",
"number": 456
},
{
"name": "Graham",
"number": 789
}
]);
});
it("Updates the contact's number when only number argument is passed", function() {
child_process.execSync('node addressBook.js update Moose 999');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(3)
expect(data[0]).toEqual({"name": "Moose", "number": 999})
});
it("Updates the contact's name when only name argument is passed", function() {
child_process.execSync('node addressBook.js update Moose Moooose');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(3)
expect(data[0]).toEqual({"name": "Moooose", "number": 123})
});
it("Does not make any changes when contact does not exist", function() {
child_process.execSync('node addressBook.js update Pam 516');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(3)
expect(data[0]).toEqual({"name": "Moose", "number": 123})
expect(data[1]).toEqual({"name": "Ricky", "number": 456})
expect(data[2]).toEqual({"name": "Graham", "number": 789})
});
it("Console logs a message when contact does not exist", function() {
var stdout = runAndCleanStdout('node addressBook.js update Pam 516');
expect(stdout.length).toBe(1);
expect(stdout[0]).toEqual("No contact found");
});
})
describe("Deleting Contacts", function() {
beforeEach(function() {
//resets data before all tests
jsonfile.writeFileSync(file, [
{
"name": "Moose",
"number": 123
},
{
"name": "Ricky",
"number": 456
},
{
"name": "Graham",
"number": 789
}
]);
});
it("Deletes the contact when it exists", function() {
child_process.execSync('node addressBook.js delete Moose');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(2)
expect(data[0]).toEqual({"name": "Ricky", "number": 456})
expect(data[1]).toEqual({"name": "Graham", "number": 789})
});
it("Does not make any changes when contact does not exist", function() {
child_process.execSync('node addressBook.js delete Pam');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(3)
expect(data[0]).toEqual({"name": "Moose", "number": 123})
expect(data[1]).toEqual({"name": "Ricky", "number": 456})
expect(data[2]).toEqual({"name": "Graham", "number": 789})
});
it("Does not make any changes when no contact was specified", function() {
child_process.execSync('node addressBook.js delete');
var data = jsonfile.readFileSync(file)
expect(data.length).toBe(3)
expect(data[0]).toEqual({"name": "Moose", "number": 123})
expect(data[1]).toEqual({"name": "Ricky", "number": 456})
expect(data[2]).toEqual({"name": "Graham", "number": 789})
});
it("Console logs a message when contact does not exist", function() {
var stdout = runAndCleanStdout('node addressBook.js delete Pam');
expect(stdout.length).toBe(1);
expect(stdout[0]).toEqual("No contact found");
});
});
});
function runAndCleanStdout(cmd){
var stdout = child_process.execSync(cmd, {encoding:'utf-8'});
stdout = stdout.split(/\r\n|\r|\n/);
stdout.splice(-1, 1);
return stdout;
}
function generateTasks(){
child_process.execSync('node addressBook.js add Pam 123');
child_process.execSync('node addressBook.js add Frankie 123');
// child_process.execSync('node addressBook.js add Call the internet guy -p 3');
}