Skip to content

Commit d2674ee

Browse files
authored
Update mathutils.test.js
1 parent 6e17d26 commit d2674ee

File tree

1 file changed

+90
-112
lines changed

1 file changed

+90
-112
lines changed
Lines changed: 90 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,129 @@
11
// A basic test file in jest framework for the mathutils.js
2+
const MathUtility = require('../mathutils');
23

3-
const MathUtility = require("../mathutils.js");
4+
// Mock SOLFEGENAMES for testing doRandom function
5+
const SOLFEGENAMES = ['do', 're', 'mi', 'fa', 'sol', 'la', 'ti'];
6+
global.SOLFEGENAMES = SOLFEGENAMES;
47

58
describe('MathUtility', () => {
6-
beforeAll(() => {
7-
global.SOLFEGENAMES = ['do', 're', 'mi', 'fa', 'sol', 'la', 'ti'];
8-
});
9-
10-
describe('doRandom', () => {
11-
it('should return a random number within the given range', () => {
12-
const min = 1, max = 10;
13-
const result = MathUtility.doRandom(min, max);
14-
expect(result).toBeGreaterThanOrEqual(min);
15-
expect(result).toBeLessThanOrEqual(max);
16-
});
17-
18-
it('should throw an error for invalid inputs', () => {
19-
expect(() => MathUtility.doRandom(null, undefined)).toThrow('NanError');
20-
});
21-
22-
it('should return a valid solfege array when inputs are solfege strings', () => {
23-
const result = MathUtility.doRandom('do', 'mi', 4);
24-
expect(result).toHaveLength(2);
25-
expect(global.SOLFEGENAMES).toContain(result[0]);
26-
expect(Number(result[1])).toBeGreaterThanOrEqual(4);
27-
});
28-
});
29-
30-
describe('doOneOf', () => {
31-
it('should return either of the inputs randomly', () => {
32-
const a = 'apple', b = 'banana';
33-
const result = MathUtility.doOneOf(a, b);
34-
expect([a, b]).toContain(result);
35-
});
36-
});
37-
38-
describe('doMod', () => {
39-
it('should return the modulus of two numbers', () => {
40-
expect(MathUtility.doMod(10, 3)).toBe(1);
41-
});
42-
43-
it('should throw an error for invalid inputs', () => {
44-
expect(() => MathUtility.doMod('a', 3)).toThrow('NanError');
45-
});
46-
});
9+
describe('doRandom', () => {
10+
test('returns random number between two numbers', () => {
11+
const result = MathUtility.doRandom(1, 10);
12+
expect(result).toBeGreaterThanOrEqual(1);
13+
expect(result).toBeLessThanOrEqual(10);
14+
});
4715

48-
describe('doSqrt', () => {
49-
it('should return the square root of a number', () => {
50-
expect(MathUtility.doSqrt(9)).toBe(3);
51-
});
16+
test('returns random solfege array between two solfege names', () => {
17+
const result = MathUtility.doRandom('do', 'sol', 4);
18+
expect(result).toHaveLength(2);
19+
expect(SOLFEGENAMES).toContain(result[0]);
20+
expect(Number(result[1])).toBeGreaterThanOrEqual(4);
21+
});
5222

53-
it('should throw an error for negative numbers', () => {
54-
expect(() => MathUtility.doSqrt(-1)).toThrow('NoSqrtError');
23+
test('throws error for invalid inputs', () => {
24+
expect(() => MathUtility.doRandom('invalid', 10)).toThrow('NanError');
25+
});
5526
});
56-
});
5727

58-
describe('doPlus', () => {
59-
it('should add two numbers', () => {
60-
expect(MathUtility.doPlus(3, 4)).toBe(7);
28+
describe('doOneOf', () => {
29+
test('returns either a or b', () => {
30+
const result = MathUtility.doOneOf('a', 'b');
31+
expect(['a', 'b']).toContain(result);
32+
});
6133
});
6234

63-
it('should concatenate strings', () => {
64-
expect(MathUtility.doPlus('3', 4)).toBe('34');
65-
});
66-
});
35+
describe('doMod', () => {
36+
test('calculates modulus correctly', () => {
37+
expect(MathUtility.doMod(10, 3)).toBe(1);
38+
});
6739

68-
describe('doMinus', () => {
69-
it('should subtract two numbers', () => {
70-
expect(MathUtility.doMinus(10, 4)).toBe(6);
40+
test('throws error for invalid inputs', () => {
41+
expect(() => MathUtility.doMod('a', 3)).toThrow('NanError');
42+
});
7143
});
7244

73-
it('should throw an error for strings', () => {
74-
expect(() => MathUtility.doMinus('10', 4)).toThrow('NanError');
75-
});
76-
});
45+
describe('doSqrt', () => {
46+
test('calculates square root', () => {
47+
expect(MathUtility.doSqrt(9)).toBe(3);
48+
});
7749

78-
describe('doMultiply', () => {
79-
it('should multiply two numbers', () => {
80-
expect(MathUtility.doMultiply(2, 5)).toBe(10);
50+
test('throws error for negative input', () => {
51+
expect(() => MathUtility.doSqrt(-1)).toThrow('NoSqrtError');
52+
});
8153
});
8254

83-
it('should throw an error for strings', () => {
84-
expect(() => MathUtility.doMultiply('2', 5)).toThrow('NanError');
85-
});
86-
});
55+
describe('doPlus', () => {
56+
test('adds two numbers', () => {
57+
expect(MathUtility.doPlus(2, 3)).toBe(5);
58+
});
8759

88-
describe('doDivide', () => {
89-
it('should divide two numbers', () => {
90-
expect(MathUtility.doDivide(10, 2)).toBe(5);
60+
test('concatenates strings', () => {
61+
expect(MathUtility.doPlus('a', 'b')).toBe('ab');
62+
});
9163
});
9264

93-
it('should throw an error for division by zero', () => {
94-
expect(() => MathUtility.doDivide(10, 0)).toThrow('DivByZeroError');
95-
});
96-
});
65+
describe('doMinus', () => {
66+
test('subtracts numbers', () => {
67+
expect(MathUtility.doMinus(5, 3)).toBe(2);
68+
});
9769

98-
describe('doCalculateDistance', () => {
99-
it('should calculate Euclidean distance', () => {
100-
expect(MathUtility.doCalculateDistance(0, 0, 3, 4)).toBe(5);
70+
test('throws error for string inputs', () => {
71+
expect(() => MathUtility.doMinus('a', 3)).toThrow('NanError');
72+
});
10173
});
10274

103-
it('should throw an error for invalid inputs', () => {
104-
expect(() => MathUtility.doCalculateDistance('a', 0, 3, 4)).toThrow('NanError');
105-
});
106-
});
75+
describe('doMultiply', () => {
76+
test('multiplies numbers', () => {
77+
expect(MathUtility.doMultiply(2, 3)).toBe(6);
78+
});
10779

108-
describe('doPower', () => {
109-
it('should calculate the power of a number', () => {
110-
expect(MathUtility.doPower(2, 3)).toBe(8);
80+
test('throws error for string inputs', () => {
81+
expect(() => MathUtility.doMultiply('a', 3)).toThrow('NanError');
82+
});
11183
});
11284

113-
it('should throw an error for invalid inputs', () => {
114-
expect(() => MathUtility.doPower('2', 3)).toThrow('NanError');
115-
});
116-
});
85+
describe('doDivide', () => {
86+
test('divides numbers', () => {
87+
expect(MathUtility.doDivide(6, 2)).toBe(3);
88+
});
11789

118-
describe('doAbs', () => {
119-
it('should return the absolute value of a number', () => {
120-
expect(MathUtility.doAbs(-5)).toBe(5);
90+
test('throws error for division by zero', () => {
91+
expect(() => MathUtility.doDivide(6, 0)).toThrow('DivByZeroError');
92+
});
12193
});
12294

123-
it('should throw an error for invalid inputs', () => {
124-
expect(() => MathUtility.doAbs('a')).toThrow('NanError');
95+
describe('doCalculateDistance', () => {
96+
test('calculates Euclidean distance', () => {
97+
expect(MathUtility.doCalculateDistance(0, 0, 3, 4)).toBe(5);
98+
});
12599
});
126-
});
127100

128-
describe('doNegate', () => {
129-
it('should negate a number', () => {
130-
expect(MathUtility.doNegate(5)).toBe(-5);
101+
describe('doPower', () => {
102+
test('calculates power', () => {
103+
expect(MathUtility.doPower(2, 3)).toBe(8);
104+
});
131105
});
132106

133-
it('should reverse a string', () => {
134-
expect(MathUtility.doNegate('hello')).toBe('olleh');
107+
describe('doAbs', () => {
108+
test('calculates absolute value', () => {
109+
expect(MathUtility.doAbs(-5)).toBe(5);
110+
});
135111
});
136112

137-
it('should throw an error for invalid inputs', () => {
138-
expect(() => MathUtility.doNegate(null)).toThrow('NoNegError');
139-
});
140-
});
113+
describe('doNegate', () => {
114+
test('negates number', () => {
115+
expect(MathUtility.doNegate(5)).toBe(-5);
116+
});
141117

142-
describe('doInt', () => {
143-
it('should round a number to the nearest integer', () => {
144-
expect(MathUtility.doInt(5.5)).toBe(6);
118+
test('reverses string', () => {
119+
expect(MathUtility.doNegate('abc')).toBe('cba');
120+
});
145121
});
146122

147-
it('should throw an error for invalid inputs', () => {
148-
expect(() => MathUtility.doInt('hello')).toThrow('NanError');
123+
describe('doInt', () => {
124+
test('rounds to nearest integer', () => {
125+
expect(MathUtility.doInt(4.6)).toBe(5);
126+
});
149127
});
150-
});
151128
});
129+

0 commit comments

Comments
 (0)