Files
job-search/web/src/lib/format.test.ts
T

70 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { expect, test } from 'vitest';
import {
formatCount,
formatDate,
formatDateTime,
formatMoney,
formatMoneyShort,
formatValue,
parseDate,
} from './format';
test.each([
{ name: 'null', value: null },
{ name: 'undefined', value: undefined },
{ name: 'empty', value: '' },
{ name: 'Go’s zero time', value: '0001-01-01T00:00:00Z' },
{ name: 'not a date', value: 'soon' },
])('parseDate reads $name as no date', ({ value }) => {
expect(parseDate(value)).toBeNull();
expect(formatDate(value)).toBe('—');
});
test('dates are shown day, short month and year', () => {
// Tests run in UK time (vite.config.ts), as dates are shown in local time.
expect(parseDate('2026-03-03T12:00:00Z')?.toISOString()).toBe('2026-03-03T12:00:00.000Z');
expect(formatDate('2026-03-03T12:00:00Z')).toBe('3 Mar 2026');
// 23:30 UTC is already the next day in British Summer Time.
expect(formatDate('2026-07-01T23:30:00Z')).toBe('2 Jul 2026');
});
test('money is shown in whole pounds, and nothing as a dash', () => {
expect(formatMoney(125000.5)).toBe('£125,001');
expect(formatMoney(0)).toBe('—');
expect(formatMoney(-5)).toBe('—');
});
test.each([
{ value: 1, want: '£1' },
{ value: 500, want: '£500' },
{ value: 3563, want: '£3.6k' },
{ value: 12500, want: '£13k' },
{ value: 583333, want: '£583k' },
{ value: 1500000, want: '£1.5m' },
{ value: 9621987, want: '£9.6m' },
{ value: 36250000000, want: '£36bn' },
{ value: 0, want: '—' },
{ value: -5, want: '—' },
])('formatMoneyShort shows $value as $want', ({ value, want }) => {
expect(formatMoneyShort(value)).toBe(want);
});
test('formatCount groups thousands', () => {
expect(formatCount(4854)).toBe('4,854');
expect(formatCount(7)).toBe('7');
});
test('formatValue shows the amount in its currency', () => {
expect(formatValue(1200.5, 'EUR')).toBe('€1,201');
expect(formatValue(250000, 'GBP')).toBe('£250,000');
expect(formatValue(250000, '')).toBe('£250,000');
expect(formatValue(1500, 'not a code')).toBe('1,500 not a code');
expect(formatValue(0, 'EUR')).toBe('—');
});
test('formatDateTime shows the UK time', () => {
expect(formatDateTime('2026-10-15T09:05:26Z')).toBe('15 Oct 2026, 10:05');
expect(formatDateTime(null)).toBe('—');
});