-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.test.ts
More file actions
63 lines (56 loc) · 2.24 KB
/
index.test.ts
File metadata and controls
63 lines (56 loc) · 2.24 KB
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
import { simpleCalculator, Action } from './index';
describe('simpleCalculator tests', () => {
test('should add two numbers', () => {
expect(simpleCalculator({ a: 7, b: 6, action: Action.Add })).toBe(13);
expect(simpleCalculator({ a: -7, b: 6, action: Action.Add })).toBe(-1);
expect(simpleCalculator({ a: 2, b: 2, action: Action.Add })).toBe(4);
});
test('should subtract two numbers', () => {
expect(simpleCalculator({ a: 7, b: 6, action: Action.Subtract })).toBe(1);
expect(simpleCalculator({ a: -7, b: 6, action: Action.Subtract })).toBe(
-13,
);
expect(simpleCalculator({ a: 2, b: 2, action: Action.Subtract })).toBe(0);
});
test('should multiply two numbers', () => {
expect(simpleCalculator({ a: 7, b: 6, action: Action.Multiply })).toBe(42);
expect(simpleCalculator({ a: -7, b: 6, action: Action.Multiply })).toBe(
-42,
);
expect(simpleCalculator({ a: 0, b: 2, action: Action.Multiply })).toBe(0);
});
test('should divide two numbers', () => {
expect(simpleCalculator({ a: 15, b: 3, action: Action.Divide })).toBe(5);
expect(simpleCalculator({ a: -15, b: 3, action: Action.Divide })).toBe(-5);
expect(simpleCalculator({ a: 0, b: 2, action: Action.Divide })).toBe(0);
expect(simpleCalculator({ a: 2, b: 0, action: Action.Divide })).toBe(
Infinity,
);
});
test('should exponentiate two numbers', () => {
expect(simpleCalculator({ a: 2, b: 3, action: Action.Exponentiate })).toBe(
8,
);
expect(simpleCalculator({ a: -2, b: 3, action: Action.Exponentiate })).toBe(
-8,
);
expect(simpleCalculator({ a: 2, b: -3, action: Action.Exponentiate })).toBe(
0.125,
);
expect(simpleCalculator({ a: 0, b: 3, action: Action.Exponentiate })).toBe(
0,
);
});
test('should return null for invalid action', () => {
expect(simpleCalculator({ a: 7, b: 6, action: '%' })).toBeNull();
});
test('should return null for invalid arguments', () => {
expect(simpleCalculator({ a: '7', b: 6, action: Action.Add })).toBeNull();
expect(
simpleCalculator({ a: 7, b: '6', action: Action.Subtract }),
).toBeNull();
expect(
simpleCalculator({ a: null, b: 6, action: Action.Multiply }),
).toBeNull();
});
});