-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
296 lines (269 loc) · 6.99 KB
/
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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
var test = require("tape");
var logic = require("./logic");
test("Example test", function (t) {
t.pass();
t.end();
});
//test5
test('test that function appends newTodo to cloned todo array', function (t) {
let todos = [{
id: 0,
description: 'comet',
done: false
}];
let newTodo = {
description: 'asteroid'
};
let result = [{
id: 0,
description: 'comet',
done: false
},
{
id: 1,
description: 'asteroid',
done: false
}
];
var actual = logic.addTodo(todos, newTodo);
var expected = result;
t.deepEquals(
actual,
expected,
"newTodo should be appended to cloned todos array"
);
t.end();
});
//test 1
test('test that array length has increased by 1', function (t) {
var todos = [{
id: 0,
description: 'comet',
done: false
}];
let k = todos.length;
console.log("todos length:" + todos.length);
var newTodo = {};
var result = logic.addTodo(todos, newTodo);
var actual = result.length;
var expected = k + 1;
console.log("todos length:" + todos.length);
console.log("result:" + result);
console.log("actual:" + actual);
console.log("expected:" + expected);
t.equals(actual, expected, "length of returned array should increase by 1");
t.end();
});
//test 2
test('check that appended element is an object', function (t) {
var todos = [{
id: 0,
description: 'comet',
done: false
}];
var newTodo = {};
var result = logic.addTodo(todos, newTodo);
var actual = typeof result[result.length - 1];
var expected = 'object';
console.log("result:" + result)
console.log("actual:" + actual)
console.log("expected:" + expected)
t.equals(actual, expected, 'last element of returned array should be an object');
t.end();
});
//test 3
test('test original todos remains unchanged by checking length', function (t) {
var todos = [{
id: 0,
description: 'comet',
done: false
}];
var newTodo = {};
var origLength = todos.length;
logic.addTodo(todos, newTodo);
var newLength = todos.length;
var actual = newLength;
var expected = origLength;
console.log("actual:" + actual);
console.log("expected:" + expected);
t.equals(actual, expected, "original todos length remains unchanged");
t.end();
});
//test4
test('check newTodo has an id prop after appending', function (t) {
var todos = [{
id: 0,
description: 'comet',
done: false
}];
var newTodo = {};
let result = logic.addTodo(todos, newTodo);
var expected = true;
var actual = result[result.length - 1].hasOwnProperty('id'); // grab id of last object in returned array
t.equals(actual, expected, 'should be true that last object has id prop');
t.end();
});
//test 6
test('test original todos remains unchanged by checking deep equality', function (t) {
var todos = [{
id: 0,
description: 'comet',
done: false
}];
var newTodo = {};
let origTodos = todos.map(x => JSON.parse(JSON.stringify(x))); // arrow function deep copy of todos
logic.addTodo(todos, newTodo);
var actual = todos;
var expected = origTodos;
console.log("actual:" + actual);
console.log("expected:" + expected);
t.deepEquals(
actual,
expected,
"original todos should match todos after invoking function"
);
t.end();
});
//markTodo Tests
//test 1
test('test todos remain unchanged by checking deep equality', function (t) {
var todos = [{
id: 0,
description: 'comet',
done: false
}];
var idToMark = {};
let origTodos = todos.map(x => JSON.parse(JSON.stringify(x))); // arrow function deep copy of todos
logic.markTodo(todos, idToMark);
var actual = todos;
var expected = origTodos;
console.log("actual:" + actual);
console.log("expected:" + expected);
t.deepEquals(actual, expected, 'original todos should match todos after invoking function');
t.end();
});
//test2
test('test if todos have a boolean value for done key', function (t) {
var todos = [{
id: 0,
description: 'comet',
done: false
}];
var idToMark = {};
let allTodos = todos.map(x => JSON.parse(JSON.stringify(x))); // arrow function deep copy of todos
logic.markTodo(todos, idToMark);
var actual = allTodos.every(obj => typeof obj.done === 'boolean');
var expected = true;
console.log("actual:" + actual);
console.log("expected:" + expected);
t.deepEquals(actual, expected, 'Todos should have a boolean value for done key');
t.end();
});
//test3
test('done key boolean value should be reversed', function (t) {
// var idToMark = 0;
// let allTodos = todos.map(x => JSON.parse(JSON.stringify(x))); // arrow function deep copy of todos
var todos = [{
id: 0,
description: "comet",
done: false
}];
let result = logic.markTodo(todos, 0);
var actual = result[0].done;
var expected = true;
console.log("actual:" + actual);
console.log("expected:" + expected);
t.deepEquals(actual, expected, 'done key boolean value should be reversed');
t.end();
});
// **deleteTodo tests**
// test 1
test("test original todos remains unchanged after invoking deleteTodo", function (t) {
var todos = [{
id: 0,
description: "comet",
done: false
},
{
id: 1,
description: "asteroid",
done: false
}
];
let origTodos = todos.map(x => JSON.parse(JSON.stringify(x))); // get copy of todos
logic.deleteTodo(todos, 1);
var actual = todos;
var expected = origTodos;
t.deepEquals(
actual,
expected,
"original todos should match todos after invoking function"
);
t.end();
});
//test 2
test("test that todo with given id has been removed", function (t) {
var todos = [{
id: 0,
description: "comet",
done: false
},
{
id: 1,
description: "asteroid",
done: false
}
];
let result = logic.deleteTodo(todos, 1);
let actual = result.filter(x => x.id === 1);
let expected = [];
t.deepEquals(actual, expected, "should be true that todo with id 1 is removed");
t.end();
});
// **sortTodo test**
// test 1
test("test original todos remains unchanged after invoking sortTodos", function (t) {
var todos = [{
id: 0,
description: "comet",
done: false
},
{
id: 1,
description: "asteroid",
done: false
},
{
id: 2,
description: "jupiter",
done: true
}
];
let origTodos = logic.cloneArrayOfObjects(todos); // get copy of todos
logic.sortTodos(todos, (x, y) => y.id - x.id);
var actual = todos;
var expected = origTodos;
t.deepEquals(actual, expected, "original todos should match todos after invoking function");
t.end();
});
// test 2
test('test that todos are sorted in reverse by id given appropriate function', function (t) {
var todos = [{
id: 0,
description: 'comet',
done: false
}, {
id: 1,
description: 'asteroid',
done: false
}, {
id: 2,
description: "jupiter",
done: true
}];
let result = logic.sortTodos(todos, (x, y) => y.id - x.id);
var actual = result[0].id;
var expected = 2;
t.deepEquals(actual, expected, 'first todo should have id 2');
t.end();
});