Files
Neon-Desk/lib/format.test.ts

48 lines
1.4 KiB
TypeScript

import { describe, expect, it } from 'bun:test';
import { formatCurrencyByScale, formatScaledNumber } from './format';
describe('formatScaledNumber', () => {
it('keeps values below one thousand unscaled', () => {
expect(formatScaledNumber(950)).toBe('950');
expect(formatScaledNumber(950.44, { maximumFractionDigits: 2 })).toBe('950.44');
});
it('scales thousands with K suffix', () => {
expect(formatScaledNumber(12_345)).toBe('12.3K');
});
it('scales millions with M suffix', () => {
expect(formatScaledNumber(3_250_000)).toBe('3.3M');
});
it('scales billions with B suffix', () => {
expect(formatScaledNumber(7_500_000_000)).toBe('7.5B');
});
it('preserves negative sign when scaled', () => {
expect(formatScaledNumber(-2_750_000)).toBe('-2.8M');
});
it('promotes rounded values to the next scale when needed', () => {
expect(formatScaledNumber(999_950)).toBe('1M');
});
});
describe('formatCurrencyByScale', () => {
it('formats values in thousands', () => {
expect(formatCurrencyByScale(12_345, 'thousands')).toBe('$12.3K');
});
it('formats values in millions', () => {
expect(formatCurrencyByScale(12_345_678, 'millions')).toBe('$12.3M');
});
it('formats values in billions', () => {
expect(formatCurrencyByScale(12_345_678_901, 'billions')).toBe('$12.3B');
});
it('keeps sign for negative values', () => {
expect(formatCurrencyByScale(-2_500_000, 'millions')).toBe('-$2.5M');
});
});