-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
708 lines (658 loc) · 36.2 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// Order of execution:
// npm i
// to run just the tests: npm test
// to test code coverage: npm run coverage
let mocha = require('mocha');
let chai = require('chai');
let describe = mocha.describe;
let expect = chai.expect;
let assert = require('chai').assert;
chai.should();
let Purchase = require("./purchase.js");
describe('Purchase', () => {
describe('Internet connection functionality', () => {
let purchase;
beforeEach(() => {
purchase = new Purchase(0, false, 0, []);
});
describe('Check the internet connection data type', () => {
it('should be a boolean', () => {
purchase.internetConnection(true);
assert.isBoolean(purchase.isInternetConnection);
});
it('should only accept boolean values', () => {
const invalidValues = ['true', 1, 1.1];
const errorMessage = 'isInternetConnectionChecked must be a boolean.';
for (let i = 0; i < invalidValues.length; i++) {
// console.log(invalidValues[i]);
expect(() => purchase.internetConnection(invalidValues[i])).to.throw(errorMessage);
}
});
});
describe('Check if the internet connection is included', () => {
it('should be equal with true', () => {
purchase.internetConnection(true);
purchase.isInternetConnection.should.equal(true);
});
it('should not be equal with false', () => {
purchase.internetConnection(true);
purchase.isInternetConnection.should.not.equal(false);
});
it('should total price be equal with 200', () => {
purchase.internetConnection(true);
purchase.totalPrice.should.equal(200);
});
it('should total price not be equal with 0', () => {
purchase.internetConnection(true);
purchase.totalPrice.should.not.equal(0);
});
it('should return 200', () => {
purchase.internetConnection(true).should.equal(200);
});
it('should not return 0', () => {
purchase.internetConnection(true).should.not.equal(0);
});
});
describe('Check if the internet connection is excluded', () => {
it('should be equal with false', () => {
purchase.internetConnection(false);
purchase.isInternetConnection.should.equal(false);
});
it('should not be equal with true', () => {
purchase.internetConnection(false);
purchase.isInternetConnection.should.not.equal(true);
});
it('should total price be equal with 0', () => {
purchase.internetConnection(false);
purchase.totalPrice.should.equal(0);
});
it('should total price not be equal with 200', () => {
purchase.internetConnection(false);
purchase.totalPrice.should.not.equal(200);
});
it('should return 0', () => {
purchase.internetConnection(false).should.equal(0);
});
it('should not return 200', () => {
purchase.internetConnection(false).should.not.equal(200);
});
});
});
/*
Equivalence partitioning
Valid partitions: 0
1-8
Invalid partitions: <0
>8
Non-numeric characters
Real (decimal) numbers
Boundary values
Invalid lower boundary: -1
Valid lower boundary: 0,1
Valid upper boundary: 7,8
Invalid upper boundary: 9
Middle value: 4
*/
describe('Phone lines functionality', () => {
let purchase;
beforeEach(() => {
purchase = new Purchase(0, false, 0, []);
});
describe('Should only accept integers when adding', () => {
it('should not accept floats', () => {
purchase.phoneLines = 2.3;
expect(() => purchase.addPhoneLines()).to.throw('phoneLines must be an integer between 0 and 8.');
});
it('should not accept strings', () => {
purchase.phoneLines = "myString"
expect(() => purchase.addPhoneLines()).to.throw('phoneLines must be an integer between 0 and 8.');
});
});
describe('Should only accept integers when removing', () => {
it('should not accept floats', () => {
purchase.phoneLines = 7.6;
expect(() => purchase.removePhoneLines()).to.throw('phoneLines must be an integer between 0 and 8.');
});
it('should not accept strings', () => {
purchase.phoneLines = "testString"
expect(() => purchase.removePhoneLines()).to.throw('phoneLines must be an integer between 0 and 8.');
});
});
describe('Should only accept valid partitions of integers when adding', () => {
it('should not accept negatives', () => {
purchase.phoneLines = -1;
expect(() => purchase.addPhoneLines()).to.throw('The minimum number of phone lines that can be hired is 0.');
});
it('should not accept values above 7', () => {
purchase.phoneLines = 8;
expect(() => purchase.addPhoneLines()).to.throw('The maximum number of phone lines that can be hired is 8.');
})
});
describe('Should only accept valid partitions of integers when removing', () => {
it('should not accept negatives', () => {
purchase.phoneLines = 0;
expect(() => purchase.removePhoneLines()).to.throw('The minimum number of phone lines that can be hired is 0.');
});
it('should not accept values above 8', () => {
purchase.phoneLines = 9;
expect(() => purchase.removePhoneLines()).to.throw('The maximum number of phone lines that can be hired is 8.');
})
});
describe('Should check if phone lines are included', () => {
let dataProvider = [0,1,4,7];
it('should calculate the total price after 1 addition', () => {
for (let i = 0; i < dataProvider.length; i ++) {
purchase.phoneLines = dataProvider[i];
purchase.totalPrice = dataProvider[i] * 150;
purchase.addPhoneLines();
purchase.totalPrice.should.equal((dataProvider[i] + 1) *150);
}
});
it('after 1 addition, total price should not be equal to the one before', () => {
for (let i = 0; i < dataProvider.length; i ++) {
purchase.phoneLines = dataProvider[i];
purchase.addPhoneLines();
purchase.totalPrice.should.not.equal(dataProvider[i] * 150);
}
});
it('phone lines should increase with 1', () => {
for (let i = 0; i < dataProvider.length; i ++) {
purchase.phoneLines = dataProvider[i];
purchase.addPhoneLines();
purchase.phoneLines.should.equal(dataProvider[i] + 1);
}
});
it('after 1 addition, nr of phone lines should not be equal to the ones before', () => {
for (let i = 0; i < dataProvider.length; i ++) {
purchase.phoneLines = dataProvider[i];
purchase.addPhoneLines();
purchase.phoneLines.should.not.equal(dataProvider[i]);
}
});
});
describe('Should check if phone lines are excluded', () => {
let dataProvider = [1,4,7,8];
it('should calculate the total price after 1 deletion', () => {
for (let i = 0; i < dataProvider.length; i ++) {
purchase.phoneLines = dataProvider[i];
purchase.totalPrice = dataProvider[i] * 150;
purchase.removePhoneLines();
purchase.totalPrice.should.equal((dataProvider[i] - 1) * 150);
}
});
it('after 1 deletion, total price should not be equal to the one before', () => {
for (let i = 0; i < dataProvider.length; i ++) {
purchase.phoneLines = dataProvider[i];
purchase.removePhoneLines();
purchase.totalPrice.should.not.equal(dataProvider[i] * 150);
}
});
it('phone lines should decrease with 1', () => {
for (let i = 0; i < dataProvider.length; i ++) {
purchase.phoneLines = dataProvider[i];
purchase.removePhoneLines();
purchase.phoneLines.should.equal(dataProvider[i] - 1);
}
});
it('after 1 deletion, nr of phone lines should not be equal to the one before', () => {
for (let i = 0; i < dataProvider.length; i ++) {
purchase.phoneLines = dataProvider[i];
purchase.removePhoneLines();
purchase.phoneLines.should.not.equal(dataProvider[i]);
}
});
});
});
describe('Buying functionality', () => {
let purchase;
beforeEach(() => {
purchase = new Purchase(0, false, 0, []);
});
describe('Check the buying functionality return type', () => {
it('should be a string if there is nothing selected', () => {
assert.isString(purchase.showBuyingReceipt());
});
it('should be a string if there is something selected', () => {
purchase.internetConnection(true);
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.removePhoneLines();
purchase.selectCellPhone('iPhone 99');
purchase.selectCellPhone('iPhone 99');
purchase.unselectCellPhone('iPhone 99');
purchase.selectCellPhone('Samsung Galaxy 99');
assert.isString(purchase.showBuyingReceipt());
});
});
describe('Check if there is nothing selected', () => {
it('should be equal with "Nothing is selected! Please select an item!"', () => {
purchase.showBuyingReceipt().should.equal("Nothing is selected! Please select an item!");
});
});
describe('Check if there is something selected', () => {
it('should be equal with true internet connection and 200 DKK total price', () => {
purchase.internetConnection(true);
purchase.showBuyingReceipt().should.equal('Internet Connection: ' + true + '\n' +
'Total Price: ' + 200 + ' DKK'
);
});
it('should be equal with one phone lines and 150 DKK total price', () => {
purchase.addPhoneLines();
purchase.showBuyingReceipt().should.equal('Number of Phone Lines: ' + 1 + '\n' +
'Total Price: ' + 150 + ' DKK'
);
});
it('should be equal with one iPhone 99 cell phone and 6000 DKK total price', () => {
purchase.selectCellPhone('iPhone 99');
purchase.showBuyingReceipt().should.equal('Cell Phones: ' + 'iPhone 99' + '\n' +
'Total Price: ' + 6000 + ' DKK'
);
});
it('should be equal with true internet connection, 2 phone lines and 500 DKK total price', () => {
purchase.internetConnection(true);
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.showBuyingReceipt().should.equal('Internet Connection: ' + true + '\n' +
'Number of Phone Lines: ' + 2 + '\n' +
'Total Price: ' + 500 + ' DKK'
);
});
it('should be equal with true internet connection, 1 iphone, 1 samsung and 7200 DKK total price', () => {
purchase.internetConnection(true);
purchase.selectCellPhone('iPhone 99');
purchase.selectCellPhone('Samsung Galaxy 99');
purchase.showBuyingReceipt().should.equal('Internet Connection: ' + true + '\n' +
'Cell Phones: ' + 'iPhone 99,Samsung Galaxy 99' + '\n' +
'Total Price: ' + 7200 + ' DKK'
);
});
it('should be equal with 4 phone lines, 1 motorola, 1 samsung, 1 huawei and 3300 DKK total price', () => {
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.selectCellPhone('Motorola G99');
purchase.selectCellPhone('Samsung Galaxy 99');
purchase.selectCellPhone('Huawei 99');
purchase.showBuyingReceipt().should.equal('Number of Phone Lines: ' + 4 + '\n' +
'Cell Phones: ' + 'Motorola G99,Samsung Galaxy 99,Huawei 99' + '\n' +
'Total Price: ' + 3300 + ' DKK'
);
});
it('should be equal with true internet connection, one phone lines, one iPhone 99 cell phone and 6350 DKK total price', () => {
purchase.internetConnection(true);
purchase.addPhoneLines();
purchase.selectCellPhone('iPhone 99');
purchase.showBuyingReceipt().should.equal('Internet Connection: ' + true + '\n' +
'Number of Phone Lines: ' + 1 + '\n' +
'Cell Phones: ' + 'iPhone 99' + '\n' +
'Total Price: ' + 6350 + ' DKK'
);
});
it('should be equal with true internet connection, 5 phone lines, 1 motorola, 1 iphone, 3 samsung, 1 sony, 1 huawei cell phones and 12550 DKK total price', () => {
purchase.internetConnection(true);
purchase.internetConnection(false);
purchase.internetConnection(true);
purchase.addPhoneLines();
purchase.removePhoneLines();
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.removePhoneLines();
purchase.addPhoneLines();
purchase.addPhoneLines();
purchase.selectCellPhone('Motorola G99');
purchase.selectCellPhone('iPhone 99');
purchase.selectCellPhone('Samsung Galaxy 99');
purchase.selectCellPhone('Sony Xperia 99');
purchase.selectCellPhone('Samsung Galaxy 99');
purchase.selectCellPhone('iPhone 99');
purchase.selectCellPhone('Huawei 99');
purchase.selectCellPhone('Samsung Galaxy 99');
purchase.unselectCellPhone('iPhone 99');
purchase.showBuyingReceipt().should.equal('Internet Connection: ' + true + '\n' +
'Number of Phone Lines: ' + 5 + '\n' +
'Cell Phones: ' + 'Motorola G99,Samsung Galaxy 99,Sony Xperia 99,Samsung Galaxy 99,iPhone 99,Huawei 99,Samsung Galaxy 99' + '\n' +
'Total Price: ' + 12550 + ' DKK'
);
});
});
});
describe('Select a Cell Phone functionality', () => {
let purchase;
beforeEach(() => {
purchase = new Purchase(0, false, 0, []);
});
describe('Check if the parameter (modelName) is valid', () => {
// Valid cases
it('should not fail if the parameter is a String object', function(){
expect(() => purchase.selectCellPhone(String('Text'))).to.not.throw('The parameter modelName must be a string.');
});
it('should not fail if the parameter is a String with single quotes', function(){
expect(() => purchase.selectCellPhone('Text')).to.not.throw('The parameter modelName must be a string.');
});
it('should not fail if the parameter is a String with double quotes', function(){
expect(() => purchase.selectCellPhone("Text")).to.not.throw('The parameter modelName must be a string.');
});
// Invalid cases
it('should fail if parameter is not given', function(){
// call selectCellPhone() with no parameters
expect(() => purchase.selectCellPhone()).to.throw('The parameter modelName must be a string.');
});
it('should fail if parameter is NaN', function(){
expect(() => purchase.selectCellPhone(NaN)).to.throw('The parameter modelName must be a string.');
});
it('should fail if the parameter is a literal number', function(){
expect(() => purchase.selectCellPhone(1)).to.throw('The parameter modelName must be a string.');
});
it('should fail if the parameter is a Number object', function(){
expect(() => purchase.selectCellPhone(Number(10))).to.throw('The parameter modelName must be a string.');
});
});
describe('Check if error is thrown ("The Model Name must be one of the 5 available Models!")', () => {
// Valid values
it('should not throw error if parameter is Motorola G99', function(){
expect(() => purchase.selectCellPhone(String('Motorola G99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
it('should not throw error if parameter is iPhone 99', function(){
expect(() => purchase.selectCellPhone(String('iPhone 99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
it('should not throw error if parameter is Samsung Galaxy 99', function(){
expect(() => purchase.selectCellPhone(String('Samsung Galaxy 99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
it('should not throw error if parameter is Sony Xperia 99', function(){
expect(() => purchase.selectCellPhone(String('Sony Xperia 99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
it('should not throw error if parameter is Huawei 99', function(){
expect(() => purchase.selectCellPhone(String('Huawei 99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
// Invalid values - writing just part of the phone name
it('should throw error if parameter is Motorola', function(){
expect(() => purchase.selectCellPhone(String('Motorola'))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is iPhone', function(){
expect(() => purchase.selectCellPhone(String('iPhone'))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is Samsung', function(){
expect(() => purchase.selectCellPhone(String('Samsung'))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is Sony', function(){
expect(() => purchase.selectCellPhone(String('Sony'))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is Huawei', function(){
expect(() => purchase.selectCellPhone(String('Huawei'))).to.throw('The Model Name must be one of the 5 available Models!');
});
// Invalid values
it('should throw error if parameter is an Empty String', function(){
expect(() => purchase.selectCellPhone(String(''))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is NOT A PHONE NAME', function(){
expect(() => purchase.selectCellPhone(String('NOT A PHONE NAME'))).to.throw('The Model Name must be one of the 5 available Models!');
});
});
describe('Check if a VALID selected phone name is added in the array of selectedCellPhones', () => {
// Valid cases
it('should add "Motorola G99" to the selectedCellPhones array', () => {
purchase.selectCellPhone('Motorola G99');
expect(purchase.selectedCellPhones).to.have.members(['Motorola G99']);
});
it('should add "iPhone 99" to the selectedCellPhones array', () => {
purchase.selectCellPhone('iPhone 99');
expect(purchase.selectedCellPhones).to.have.members(['iPhone 99']);
});
it('should add "Samsung Galaxy 99" to the selectedCellPhones array', () => {
purchase.selectCellPhone('Samsung Galaxy 99');
expect(purchase.selectedCellPhones).to.have.members(['Samsung Galaxy 99']);
});
it('should add "Sony Xperia 99" to the selectedCellPhones array', () => {
purchase.selectCellPhone('Sony Xperia 99');
expect(purchase.selectedCellPhones).to.have.members(['Sony Xperia 99']);
});
it('should add "Huawei 99" to the selectedCellPhones array', () => {
purchase.selectCellPhone('Huawei 99');
expect(purchase.selectedCellPhones).to.have.members(['Huawei 99']);
});
it('should add the same elemnt multiple time to the selectedCellPhones array', () => {
purchase.selectCellPhone('Motorola G99');
purchase.selectCellPhone('Motorola G99');
purchase.selectCellPhone('Huawei 99');
purchase.selectCellPhone('Motorola G99');
purchase.selectCellPhone('Huawei 99');
purchase.selectCellPhone('iPhone 99');
purchase.selectCellPhone('iPhone 99');
purchase.selectCellPhone('Sony Xperia 99');
expect(purchase.selectedCellPhones).to.have.members(["Motorola G99", "Motorola G99", 'Huawei 99', "Motorola G99", "Huawei 99", "iPhone 99", "iPhone 99", "Sony Xperia 99"]);
});
// Add more phones in the same time
it('should add 5 elements to the selectedCellPhones array - DATA PROVIDER', () => {
let dataProvider = ["Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"];
for (let i = 0; i < dataProvider.length; i ++) {
purchase.selectCellPhone(dataProvider[i]);
}
expect(purchase.selectedCellPhones).to.have.members(["Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"]);
});
// Invalid cases
it('should not have 3 elements in the selectedCellPhones array when one is added', () => {
purchase.selectCellPhone('Motorola G99');
purchase.selectCellPhone('iPhone 99');
purchase.selectCellPhone('Motorola G99');
expect(purchase.selectedCellPhones).to.not.have.members(['Motorola G99']);
});
});
describe('Check if the price increases when adding a phone name', () => {
// Valid cases
it('should have totalPrice 800 when "Motorola G99" is added', () => {
purchase.selectCellPhone('Motorola G99');
purchase.totalPrice.should.equal(800);
});
it('should have totalPrice 6000 when "iPhone 99" is added', () => {
purchase.selectCellPhone('iPhone 99');
purchase.totalPrice.should.equal(6000);
});
it('should have totalPrice 1000 when "Samsung Galaxy 99" is added', () => {
purchase.selectCellPhone('Samsung Galaxy 99');
purchase.totalPrice.should.equal(1000);
});
it('should have totalPrice 900 when "Sony Xperia 99" is added', () => {
purchase.selectCellPhone('Sony Xperia 99');
purchase.totalPrice.should.equal(900);
});
it('should have totalPrice 900 when "Huawei 99" is added', () => {
purchase.selectCellPhone('Huawei 99');
purchase.totalPrice.should.equal(900);
});
it('should have totalPrice calculated based on 5 elements - DATA PROVIDER', () => {
let dataProviderNames = ["Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"];
let dataProviderPrices = [800, 6000, 1000, 900, 900];
let expectedPrice = 0;
for (let i = 0; i < dataProviderNames.length; i ++) {
purchase.selectCellPhone(dataProviderNames[i]);
expectedPrice += dataProviderPrices[i];
}
purchase.totalPrice.should.equal(expectedPrice);
});
// Invalid cases
it('should NOT have totalPrice 0 when "Huawei 99" is added', () => {
purchase.selectCellPhone('Huawei 99');
purchase.totalPrice.should.not.equal(0);
});
});
});
describe('Unselect a Cell Phone functionality', () => {
let purchase;
beforeEach(() => {
purchase = new Purchase(0, false, 0, []);
});
describe('Check if the parameter (modelName) is valid', () => {
// Valid cases
it('should not fail if the parameter is a String object', function(){
expect(() => purchase.unselectCellPhone(String('Text'))).to.not.throw('The parameter modelName must be a string.');
});
it('should not fail if the parameter is a String with single quotes', function(){
expect(() => purchase.unselectCellPhone('Text')).to.not.throw('The parameter modelName must be a string.');
});
it('should not fail if the parameter is a String with double quotes', function(){
expect(() => purchase.unselectCellPhone("Text")).to.not.throw('The parameter modelName must be a string.');
});
// Invalid cases
it('should fail if parameter is not given', function(){
// call selectCellPhone() with no parameters
expect(() => purchase.unselectCellPhone()).to.throw('The parameter modelName must be a string.');
});
it('should fail if parameter is NaN', function(){
expect(() => purchase.unselectCellPhone(NaN)).to.throw('The parameter modelName must be a string.');
});
it('should fail if the parameter is a literal number', function(){
expect(() => purchase.unselectCellPhone(1)).to.throw('The parameter modelName must be a string.');
});
it('should fail if the parameter is a Number object', function(){
expect(() => purchase.unselectCellPhone(Number(10))).to.throw('The parameter modelName must be a string.');
});
});
describe('Check if error is thrown ("The Model Name must be one of the 5 available Models!")', () => {
// Valid values
it('should not throw error if parameter is Motorola G99', function(){
expect(() => purchase.unselectCellPhone(String('Motorola G99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
it('should not throw error if parameter is iPhone 99', function(){
expect(() => purchase.unselectCellPhone(String('iPhone 99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
it('should not throw error if parameter is Samsung Galaxy 99', function(){
expect(() => purchase.unselectCellPhone(String('Samsung Galaxy 99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
it('should not throw error if parameter is Sony Xperia 99', function(){
expect(() => purchase.unselectCellPhone(String('Sony Xperia 99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
it('should not throw error if parameter is Huawei 99', function(){
expect(() => purchase.unselectCellPhone(String('Huawei 99'))).to.not.throw('The Model Name must be one of the 5 available Models!');
});
// Invalid values - writing just part of the phone name
it('should throw error if parameter is Motorola', function(){
expect(() => purchase.unselectCellPhone(String('Motorola'))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is iPhone', function(){
expect(() => purchase.unselectCellPhone(String('iPhone'))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is Samsung', function(){
expect(() => purchase.unselectCellPhone(String('Samsung'))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is Sony', function(){
expect(() => purchase.unselectCellPhone(String('Sony'))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is Huawei', function(){
expect(() => purchase.unselectCellPhone(String('Huawei'))).to.throw('The Model Name must be one of the 5 available Models!');
});
// Invalid values
it('should throw error if parameter is an Empty String', function(){
expect(() => purchase.unselectCellPhone(String(''))).to.throw('The Model Name must be one of the 5 available Models!');
});
it('should throw error if parameter is NOT A PHONE NAME', function(){
expect(() => purchase.unselectCellPhone(String('NOT A PHONE NAME'))).to.throw('The Model Name must be one of the 5 available Models!');
});
});
describe('Check if a VALID selected phone name is removed from the array of selectedCellPhones', () => {
// Valid cases
it('should remove "Motorola G99" from the selectedCellPhones array', () => {
purchase.selectCellPhone('Motorola G99');
purchase.unselectCellPhone('Motorola G99');
expect(purchase.selectedCellPhones).to.have.members([]);
});
it('should remove "iPhone 99" from the selectedCellPhones array', () => {
purchase.selectCellPhone('iPhone 99');
purchase.unselectCellPhone('iPhone 99');
expect(purchase.selectedCellPhones).to.have.members([]);
});
it('should remove "Samsung Galaxy 99" from the selectedCellPhones array', () => {
purchase.selectCellPhone('Samsung Galaxy 99');
purchase.unselectCellPhone('Samsung Galaxy 99');
expect(purchase.selectedCellPhones).to.have.members([]);
});
it('should remove "Sony Xperia 99" from the selectedCellPhones array', () => {
purchase.selectCellPhone('Sony Xperia 99');
purchase.unselectCellPhone('Sony Xperia 99');
expect(purchase.selectedCellPhones).to.have.members([]);
});
it('should remove "Huawei 99" from the selectedCellPhones array', () => {
purchase.selectCellPhone('Huawei 99');
purchase.unselectCellPhone('Huawei 99');
expect(purchase.selectedCellPhones).to.have.members([]);
});
// Remove more phones in the same time
it('should remove 5 elements from the selectedCellPhones array - DATA PROVIDER', () => {
let dataProvider = ["Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"];
// Add phones
for (let i = 0; i < dataProvider.length; i ++) {
purchase.selectCellPhone(dataProvider[i]);
}
expect(purchase.selectedCellPhones).to.have.members(["Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"]);
// Remove phones
for (let i = 0; i < dataProvider.length; i ++) {
purchase.unselectCellPhone(dataProvider[i]);
}
expect(purchase.selectedCellPhones).to.have.members([]);
});
// Invalid cases
it('should not remove any elements if the Phone Name is not in the list already', () => {
purchase.selectCellPhone('Motorola G99');
purchase.unselectCellPhone('iPhone 99');
expect(purchase.selectedCellPhones).to.have.members(['Motorola G99']);
});
});
describe('Check if the price decreases when removing a phone name', () => {
// Valid cases
it('should have totalPrice 0 when "Motorola G99" is removed', () => {
purchase.selectCellPhone('Motorola G99');
purchase.unselectCellPhone('Motorola G99');
purchase.totalPrice.should.equal(0);
});
it('should have totalPrice 0 when "iPhone 99" is removed', () => {
purchase.selectCellPhone('iPhone 99');
purchase.unselectCellPhone('iPhone 99');
purchase.totalPrice.should.equal(0);
});
it('should have totalPrice 0 when "Samsung Galaxy 99" is removed', () => {
purchase.selectCellPhone('Samsung Galaxy 99');
purchase.unselectCellPhone('Samsung Galaxy 99');
purchase.totalPrice.should.equal(0);
});
it('should have totalPrice 0 when "Sony Xperia 99" is removed', () => {
purchase.selectCellPhone('Sony Xperia 99');
purchase.unselectCellPhone('Sony Xperia 99');
purchase.totalPrice.should.equal(0);
});
it('should have totalPrice 0 when "Huawei 99" is removed', () => {
purchase.selectCellPhone('Huawei 99');
purchase.unselectCellPhone('Huawei 99');
purchase.totalPrice.should.equal(0);
});
it('should have totalPrice calculated based on removing 5 elements - DATA PROVIDER', () => {
let dataProviderNames = ["Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"];
let dataProviderPrices = [800, 6000, 1000, 900, 900];
let expectedPrice = 0;
// Add Phones
for (let i = 0; i < dataProviderNames.length; i ++) {
purchase.selectCellPhone(dataProviderNames[i]);
expectedPrice += dataProviderPrices[i];
}
// Remove Phones
for (let i = 0; i < dataProviderNames.length; i ++) {
purchase.unselectCellPhone(dataProviderNames[i]);
expectedPrice -= dataProviderPrices[i];
}
purchase.totalPrice.should.equal(0);
});
it('should NOT decrease totalPrice if a not existing phone was removed', () => {
purchase.selectCellPhone('Huawei 99');
purchase.unselectCellPhone('iPhone 99');
purchase.totalPrice.should.not.equal(900);
});
// Invalid cases
it('should NOT have totalPrice 900 when "Huawei 99" was removed', () => {
purchase.selectCellPhone('Huawei 99');
purchase.unselectCellPhone('Huawei 99');
purchase.totalPrice.should.not.equal(900);
});
});
});
});