-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.test.js
68 lines (57 loc) · 1.88 KB
/
calculator.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
const Calculator = require("./calculator");
describe("Calculator", () => {
let calc;
beforeEach(() => {
calc = new Calculator();
});
test("adds two numbers correctly", () => {
expect(calc.add(2, 3)).toBe(5);
expect(calc.add(-1, 1)).toBe(0);
});
test("subtracts two numbers correctly", () => {
expect(calc.subtract(5, 3)).toBe(2);
expect(calc.subtract(1, 1)).toBe(0);
});
test("multiplies two numbers correctly", () => {
expect(calc.multiply(2, 3)).toBe(6);
expect(calc.multiply(-2, 3)).toBe(-6);
expect(calc.multiply(5, 5)).toBe(25);
expect(calc.multiply(10, 10)).toBe(100);
expect(calc.multiply(0, 5)).toBe(0);
expect(calc.multiply(5, 1)).toBe(5);
expect(calc.multiply(5, 3)).toBe(15);
expect(calc.multiply(5, -3)).toBe(-15);
});
test("divides two numbers correctly", () => {
expect(calc.divide(6, 2)).toBe(3);
expect(calc.divide(5, 2)).toBe(2.5);
});
test("raises to power correctly", () => {
expect(calc.power(2, 3)).toBe(8);
expect(calc.power(3, 2)).toBe(9);
expect(calc.power(5, 0)).toBe(1);
});
test("calculates square root correctly", () => {
expect(calc.sqrt(9)).toBe(3);
expect(calc.sqrt(2)).toBeCloseTo(1.4142, 4);
expect(() => calc.sqrt(-1)).toThrow(
"Cannot calculate square root of negative number",
);
});
test("calculates absolute value correctly", () => {
expect(calc.abs(-5)).toBe(5);
expect(calc.abs(5)).toBe(5);
expect(calc.abs(0)).toBe(0);
});
test("calculates factorial correctly", () => {
expect(calc.factorial(0)).toBe(1);
expect(calc.factorial(1)).toBe(1);
expect(calc.factorial(5)).toBe(120);
expect(() => calc.factorial(-1)).toThrow(
"Cannot calculate factorial of negative number",
);
});
test("throws error when dividing by zero", () => {
expect(() => calc.divide(5, 0)).toThrow("Division by zero");
});
});