-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.spec.js
106 lines (88 loc) · 2.89 KB
/
calculator.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
describe('calculator.js', function(){
describe('Calculator', function(){
let calculator;
let calculator2;
beforeEach(function(){
// Anything inside this block executes before
// each spec (it) inside this describe.
calculator = new Calculator();
calculator2 = new Calculator();
})
afterEach(function(){
// Anything inside this block executes after
// each spec (it) inside this describe.
})
it('should initialize the total', function(){
expect(calculator.total).toBe(0);
expect(calculator.total).toBeFalsy();
})
it('can be instantiated', function(){
jasmine.addMatchers(customMatchers);
expect(calculator).toBeCalculator(); // custom matcher !
expect(calculator).toBeTruthy();
expect(calculator2).toBeTruthy();
expect(calculator).toEqual(calculator2);
expect(calculator.constructor.name/* Calculator */).toContain("Calc");
})
it('instantiates unique object', function(){
expect(calculator).not.toBe(calculator2);
})
it('has common operations', function(){
expect(calculator.add).toBeDefined() //or not.toBeUndefined();
expect(calculator.substract).toBeDefined();
expect(calculator.multiply).toBeDefined();
expect(calculator.divide).toBeDefined();
})
it('can overwrite total', function(){
calculator.total = null;
expect(calculator.total).toBeNull();
})
describe('add()',function(){
it('should add numbers to total', function(){
//create a new calculator
calculator.add(5);
// expect total to be 5
expect(calculator.total).toBe(5);
});
it ('returns total', function(){
calculator.total = 50;
expect(calculator.add(20)).toBe(70);
expect(calculator.total).toMatch(/-?\d+/);
expect(typeof calculator.total).toMatch('number');
expect(calculator.total).toBeNumber(); //from third party matchers jasmine-matchers.js
expect(calculator.total).toEqual(jasmine.anything());
});
})
describe('substract()', function(){
it('should substract numbers to total', function(){
calculator.total = 30;
calculator.substract(5);
expect(calculator.total).toBe(25);
});
})
describe('multiply()', function(){
it('should multiply total by number', function(){
calculator.total = 100;
calculator.multiply(2);
expect(calculator.total).toBe(200);
});
it ('does not hadle NaN', function(){
calculator.total = 20;
calculator.multiply('a');
expect (calculator.total).toBeNaN();
})
})
describe('divide()', function(){
it('should divide total by number', function(){
calculator.total = 200;
calculator.divide(2);
expect(calculator.total).toBe(100);
});
it ('handles divide by zero', function(){
expect(function(){ calculator.divide(0) }).toThrow();
expect(function(){ calculator.divide(0) }).toThrowError(Error);
expect(function(){ calculator.divide(0) }).toThrowError(Error, 'Cannot divide by zero');
})
})
})
});