Skip to main content

Testing pure JavaScript helper functions

https://powrtrak.atlassian.net//browse/PM-4921 Create a file under test/javascript/helpers

Create tests:

import { fixCommaSeparators } from '../../../app/javascript/helpers/input_validations';

describe('fixCommaSeparators', () => {
it('Check fixing coma separators', () => {
expect(fixCommaSeparators('1,000')).toBe(1000);
expect(fixCommaSeparators('10,000.99')).toBe(10000.99);
expect(fixCommaSeparators('11,231,230,000.99')).toBe(11231230000.99);
expect(fixCommaSeparators('10,11')).toBe(10.11);
expect(fixCommaSeparators('100,00')).toBe(100);
expect(fixCommaSeparators('11.12 ')).toBe(11.12);
expect(fixCommaSeparators('100,01')).toBe(100.01);
expect(fixCommaSeparators('100,10')).toBe(100.1);
expect(fixCommaSeparators('100,100')).toBe(100100);
expect(fixCommaSeparators('1,00,10')).toBe(10010);
expect(fixCommaSeparators('500000')).toBe(500000);
expect(fixCommaSeparators('1')).toBe(1);
expect(fixCommaSeparators('1.1')).toBe(1.1);
expect(fixCommaSeparators('1,1')).toBe(1.1);
expect(fixCommaSeparators('21,310,000.99')).toBe(21310000.99);
expect(fixCommaSeparators('21,310,0asd00.99')).toBe(21310000.99);
expect(fixCommaSeparators('11.fun12')).toBe(11.12);
});
});

Add this line to the scripts of your package.json if you don't have one: "test-helpers": "NODE_ENV=test npx jest ./test/javascript/helpers"

Run npm run test-helpers in terminal:

This is what you will see if some test case fails: