Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions js/utils/__tests__/mathutils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// A basic test file in jest framework for the mathutils.js
const MathUtility = require('../mathutils');

// Mock SOLFEGENAMES for testing doRandom function
const SOLFEGENAMES = ['do', 're', 'mi', 'fa', 'sol', 'la', 'ti'];
global.SOLFEGENAMES = SOLFEGENAMES;

describe('MathUtility', () => {
describe('doRandom', () => {
test('returns random number between two numbers', () => {
const result = MathUtility.doRandom(1, 10);
expect(result).toBeGreaterThanOrEqual(1);
expect(result).toBeLessThanOrEqual(10);
});

test('returns random solfege array between two solfege names', () => {
const result = MathUtility.doRandom('do', 'sol', 4);
expect(result).toHaveLength(2);
expect(SOLFEGENAMES).toContain(result[0]);
expect(Number(result[1])).toBeGreaterThanOrEqual(4);
});

test('throws error for invalid inputs', () => {
expect(() => MathUtility.doRandom('invalid', 10)).toThrow('NanError');
});
});

describe('doOneOf', () => {
test('returns either a or b', () => {
const result = MathUtility.doOneOf('a', 'b');
expect(['a', 'b']).toContain(result);
});
});

describe('doMod', () => {
test('calculates modulus correctly', () => {
expect(MathUtility.doMod(10, 3)).toBe(1);
});

test('throws error for invalid inputs', () => {
expect(() => MathUtility.doMod('a', 3)).toThrow('NanError');
});
});

describe('doSqrt', () => {
test('calculates square root', () => {
expect(MathUtility.doSqrt(9)).toBe(3);
});

test('throws error for negative input', () => {
expect(() => MathUtility.doSqrt(-1)).toThrow('NoSqrtError');
});
});

describe('doPlus', () => {
test('adds two numbers', () => {
expect(MathUtility.doPlus(2, 3)).toBe(5);
});

test('concatenates strings', () => {
expect(MathUtility.doPlus('a', 'b')).toBe('ab');
});
});

describe('doMinus', () => {
test('subtracts numbers', () => {
expect(MathUtility.doMinus(5, 3)).toBe(2);
});

test('throws error for string inputs', () => {
expect(() => MathUtility.doMinus('a', 3)).toThrow('NanError');
});
});

describe('doMultiply', () => {
test('multiplies numbers', () => {
expect(MathUtility.doMultiply(2, 3)).toBe(6);
});

test('throws error for string inputs', () => {
expect(() => MathUtility.doMultiply('a', 3)).toThrow('NanError');
});
});

describe('doDivide', () => {
test('divides numbers', () => {
expect(MathUtility.doDivide(6, 2)).toBe(3);
});

test('throws error for division by zero', () => {
expect(() => MathUtility.doDivide(6, 0)).toThrow('DivByZeroError');
});
});

describe('doCalculateDistance', () => {
test('calculates Euclidean distance', () => {
expect(MathUtility.doCalculateDistance(0, 0, 3, 4)).toBe(5);
});
});

describe('doPower', () => {
test('calculates power', () => {
expect(MathUtility.doPower(2, 3)).toBe(8);
});
});

describe('doAbs', () => {
test('calculates absolute value', () => {
expect(MathUtility.doAbs(-5)).toBe(5);
});
});

describe('doNegate', () => {
test('negates number', () => {
expect(MathUtility.doNegate(5)).toBe(-5);
});

test('reverses string', () => {
expect(MathUtility.doNegate('abc')).toBe('cba');
});
});

describe('doInt', () => {
test('rounds to nearest integer', () => {
expect(MathUtility.doInt(4.6)).toBe(5);
});
});
});

2 changes: 2 additions & 0 deletions js/utils/mathutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,5 @@ class MathUtility {
}
}
}
// Ensure mathutils.js exports the MathUtility class
module.exports = MathUtility;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"scripts": {
"lint": "eslint js/**/*.js",
"serve": "python3 -c \"import os, sys; os.system('python3 -m SimpleHTTPServer 3000 --bind 127.0.0.1') if sys.version_info.major==2 else os.system('python3 -m http.server 3000 --bind 127.0.0.1');\"",
"winserve": "py -c \"import os, sys; os.system('py -m SimpleHTTPServer 3000') if sys.version_info.major==2 else os.system('py -m http.server 3000 --bind 127.0.0.1');\""
"winserve": "py -c \"import os, sys; os.system('py -m SimpleHTTPServer 3000') if sys.version_info.major==2 else os.system('py -m http.server 3000 --bind 127.0.0.1');\"",
"test": "jest"
},
"devDependencies": {
"@babel/core": "^7.11.1",
Expand Down
Loading