Add tests for date and number formatting functions

This commit is contained in:
Koen Vlaswinkel
2022-09-16 13:17:35 +02:00
parent 55b060af97
commit cbc2650f30
4 changed files with 31 additions and 5 deletions

View File

@@ -2,7 +2,7 @@
* Contains an assortment of helper constants and functions for working with numbers. * Contains an assortment of helper constants and functions for working with numbers.
*/ */
const numberFormatter = new Intl.NumberFormat('en'); const numberFormatter = new Intl.NumberFormat('en-US');
export function formatDecimal(value: number): string { export function formatDecimal(value: number): string {
return numberFormatter.format(value); return numberFormatter.format(value);

View File

@@ -0,0 +1,11 @@
import { expect } from 'chai';
import 'mocha';
import { formatDate } from '../../src/pure/date';
describe('Date', () => {
it('should return a formatted date', () => {
expect(formatDate(new Date(1663326904000))).to.eq('Sep 16, 1:15 PM');
expect(formatDate(new Date(1631783704000))).to.eq('Sep 16, 2021, 11:15 AM');
});
});

View File

@@ -0,0 +1,12 @@
import { expect } from 'chai';
import 'mocha';
import { formatDecimal } from '../../src/pure/number';
describe('Number', () => {
it('should return a formatted decimal', () => {
expect(formatDecimal(9)).to.eq('9');
expect(formatDecimal(10_000)).to.eq('10,000');
expect(formatDecimal(100_000_000_000)).to.eq('100,000,000,000');
});
});

View File

@@ -5,10 +5,13 @@ import { humanizeRelativeTime, humanizeUnit } from '../../src/pure/time';
describe('Time', () => { describe('Time', () => {
it('should return a humanized unit', () => { it('should return a humanized unit', () => {
expect(humanizeUnit(undefined)).to.eq('Less than a minute'); expect(humanizeUnit(undefined)).to.eq('Less than a second');
expect(humanizeUnit(0)).to.eq('Less than a minute'); expect(humanizeUnit(0)).to.eq('Less than a second');
expect(humanizeUnit(-1)).to.eq('Less than a minute'); expect(humanizeUnit(-1)).to.eq('Less than a second');
expect(humanizeUnit(1000 * 60 - 1)).to.eq('Less than a minute'); expect(humanizeUnit(1000 - 1)).to.eq('Less than a second');
expect(humanizeUnit(1000)).to.eq('1 second');
expect(humanizeUnit(1000 * 2)).to.eq('2 seconds');
expect(humanizeUnit(1000 * 60 - 1)).to.eq('59 seconds');
expect(humanizeUnit(1000 * 60)).to.eq('1 minute'); expect(humanizeUnit(1000 * 60)).to.eq('1 minute');
expect(humanizeUnit(1000 * 60 * 2 - 1)).to.eq('1 minute'); expect(humanizeUnit(1000 * 60 * 2 - 1)).to.eq('1 minute');
expect(humanizeUnit(1000 * 60 * 2)).to.eq('2 minutes'); expect(humanizeUnit(1000 * 60 * 2)).to.eq('2 minutes');