forked from beverlyAH/hofPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.js
347 lines (313 loc) · 11.7 KB
/
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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
(function() {
'use strict';
describe('Underscore Practice', function() {
describe('_.each', function() {
describe('fruits', function() {
var testFruits;
beforeEach(function() {
testFruits = cloneObject(fruits);
});
describe('moreFruits', function() {
underscoreMethods('each', true, function() {
var data = testFruits.slice();
moreFruits(data);
});
nativeMethods('forEach', false, function() {
var data = testFruits.slice();
moreFruits(data);
});
noForLoops(moreFruits);
it('create a copy of the given array', function() {
var data = moreFruits(testFruits);
expect(data).to.eql(testFruits);
});
it('a new array is returned', function() {
var data = moreFruits(testFruits);
expect(data).to.not.equal(testFruits);
});
});
});
describe('numbers', function() {
var testNumbers;
beforeEach(function() {
testNumbers = cloneObject(numbers);
});
describe('multiplesOfFive', function() {
underscoreMethods('each', true, function() {
multiplesOfFive(testNumbers);
});
nativeMethods('forEach', false, function() {
multiplesOfFive(testNumbers);
});
noForLoops(multiplesOfFive);
it('should return a number', function() {
expect(typeof multiplesOfFive(testNumbers)).to.equal('number');
});
it('should return the count of numbers that are multiples of five', function() {
var otherNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
expect(multiplesOfFive(testNumbers)).to.equal(5);
expect(multiplesOfFive(otherNumbers)).to.equal(2);
});
});
});
});
describe('_.filter', function() {
describe('fruits', function() {
var testFruits;
beforeEach(function() {
testFruits = cloneObject(fruits);
});
describe('onlyOneFruit', function() {
underscoreMethods('filter', true, function() {
onlyOneFruit(testFruits, 'cherry');
});
nativeMethods('filter', false, function() {
onlyOneFruit(testFruits, 'blueberry');
});
noForLoops(onlyOneFruit);
noNewArrays(onlyOneFruit);
it('should return an array with only the specified fruit string', function() {
var lemons = onlyOneFruit(testFruits, 'lemon');
var strawberries = onlyOneFruit(testFruits, 'strawberry');
expect(lemons.length).to.eql(1);
expect(strawberries.length).to.eql(1);
expect(lemons).to.eql(['lemon']);
expect(strawberries).to.eql(['strawberry']);
});
});
describe('startsWith', function() {
underscoreMethods('filter', true, function() {
startsWith(testFruits, 'a');
});
nativeMethods('filter', false, function() {
startsWith(testFruits, 'b');
});
noForLoops(startsWith);
noNewArrays(startsWith);
it('should return an array', function() {
expect(Array.isArray(startsWith(testFruits, 'd'))).to.equal(true);
});
it('should return an array containing strings starting with the given letter', function() {
var startsWithP = startsWith(testFruits, 'p');
var startsWithA = startsWith(testFruits, 's');
var startsWithG = startsWith(testFruits, 'g');
expect(startsWithP.length).to.equal(4);
expect(startsWithA.length).to.equal(2);
expect(startsWithG.length).to.equal(1);
});
});
});
describe('desserts', function() {
var testDesserts;
beforeEach(function() {
testDesserts = cloneObject(desserts);
});
describe('cookiesOnly', function() {
underscoreMethods('filter', true, function() {
cookiesOnly(testDesserts);
});
nativeMethods('filter', false, function() {
cookiesOnly(testDesserts);
});
noForLoops(cookiesOnly);
noNewArrays(cookiesOnly);
it('should return a new array containing only cookie object', function() {
var cookies = cookiesOnly(testDesserts);
var onlyCookies = _.every(cookies, function(dessert) {
return dessert.type === 'cookie';
});
expect(onlyCookies).to.equal(true);
});
});
});
});
describe('_.reduce', function() {
describe('groceries', function() {
var testGrocery;
beforeEach(function() {
testGrocery = cloneObject(groceries);
});
describe('sumTotal', function() {
underscoreMethods('reduce', true, function() {
sumTotal(testGrocery);
});
nativeMethods('reduce', false, function() {
sumTotal(testGrocery);
});
noForLoops(sumTotal);
it('should return a number', function() {
expect(typeof sumTotal(groceries)).to.equal('number');
});
it('should return total sum of all prices', function() {
var total = sumTotal(testGrocery);
console.log(total);
expect(total).to.equal(173.98);
});
});
});
describe('desserts', function() {
var testDesserts;
beforeEach(function() {
testDesserts = cloneObject(desserts);
});
describe('dessertCategories', function() {
underscoreMethods('reduce', true, function() {
dessertCategories(testDesserts);
});
nativeMethods('reduce', false, function() {
dessertCategories(testDesserts);
});
it('should not use a for loop', function() {
var stringified = dessertCategories.toString();
expect(stringified.includes('for')).to.equal(false);
});
it('should return an object', function() {
expect(dessertCategories(testDesserts)).to.be.an('object');
});
it('should return object with correct values', function() {
expect(dessertCategories(testDesserts).pie).to.equal(3);
expect(dessertCategories(testDesserts).cake).to.equal(2);
expect(dessertCategories(testDesserts).cookie).to.equal(2);
expect(dessertCategories(testDesserts).drink).to.equal(1);
});
});
});
describe('movies', function() {
var testMovies;
beforeEach(function() {
testMovies = cloneObject(movies);
});
describe('ninetiesKid', function() {
underscoreMethods('reduce', true, function() {
ninetiesKid(testMovies);
});
nativeMethods('reduce', false, function() {
ninetiesKid(testMovies);
});
noForLoops(ninetiesKid);
it('should return a new array', function() {
var data = ninetiesKid(testMovies);
expect(data).to.not.equal(movies);
expect(Array.isArray(data)).to.equal(true);
});
it('should return an array containing movies released in the 90s', function() {
var data = ninetiesKid(testMovies);
expect(data).to.eql(['Titanic', 'Pulp Fiction', 'Toy Story']);
});
});
describe('movieNight', function() {
underscoreMethods('reduce', true, function() {
movieNight(movies, 60);
});
nativeMethods('reduce', false, function() {
movieNight(movies, 60);
});
noForLoops(movieNight);
noNewArrays(movieNight);
it('should return a boolean', function() {
var data = movieNight(movies, 60);
expect(typeof data).to.equal('boolean');
});
it('should confirm given time is enough for at least one movie', function() {
var firstNight = movieNight(movies, 60);
var secondNight = movieNight(movies, 120);
expect(firstNight).to.equal(false);
expect(secondNight).to.equal(true);
});
});
});
});
describe('_.map', function() {
describe('fruits', function() {
var testFruits;
beforeEach(function() {
testFruits = cloneObject(fruits);
});
describe('upperCaseFruits', function() {
underscoreMethods('map', true, function() {
upperCaseFruits(testFruits);
});
nativeMethods('map', false, function() {
upperCaseFruits(testFruits);
});
noForLoops(upperCaseFruits);
noNewArrays(upperCaseFruits);
it('should not return the original array', function() {
var data = upperCaseFruits(testFruits);
expect(data).to.not.eql(testFruits);
});
it('should return an array with all strings converted to uppercase', function() {
var data = upperCaseFruits(testFruits);
var allUpperCase = true;
_.each(data, function(fruit) {
fruit.split('').forEach(function(letter) {
if (letter !== letter.toUpperCase()) {
allUpperCase = false;
}
});
});
expect(allUpperCase).to.equal(true);
});
});
});
describe('desserts', function() {
var testDesserts;
beforeEach(function() {
testDesserts = cloneObject(desserts);
});
describe('glutenFree', function() {
underscoreMethods('map', true, function() {
glutenFree(testDesserts);
});
nativeMethods('map', false, function() {
glutenFree(testDesserts);
});
noForLoops(glutenFree);
noNewArrays(glutenFree);
it('should not return the original array', function() {
var data = glutenFree(testDesserts);
expect(desserts[0].glutenFree).to.equal(undefined);
});
it('should return a new array with all items possessing a glutenFree status', function() {
var data = glutenFree(testDesserts);
var allHaveGlutenFreeProperty = true;
data.forEach(function(dessert) {
if (!dessert.hasOwnProperty('glutenFree')) {
allHaveGlutenFreeProperty = false;
}
});
expect(allHaveGlutenFreeProperty).to.equal(true);
});
});
});
describe('groceries', function() {
describe('applyCoupon', function() {
var testGrocery;
beforeEach(function() {
testGrocery = cloneObject(groceries);
});
underscoreMethods('map', true, function() {
applyCoupon(testGrocery, 0.20);
});
nativeMethods('map', false, function() {
applyCoupon(testGrocery, 0.20);
});
noForLoops(applyCoupon);
noNewArrays(applyCoupon);
it('should not return the original array', function() {
var data = applyCoupon(testGrocery, 0.20);
expect(data).to.not.eql(groceries);
});
it('should return array of items with sale prices', function() {
var data = applyCoupon(testGrocery, 0.20);
expect(data[10].salePrice).to.equal('$4.47');
});
it('items in array should have added salePrice property', function() {
var onSale = applyCoupon(testGrocery, 0.20);
expect(onSale[12]).to.have.property('salePrice');
});
});
});
});
});
}());