mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 04:47:45 +01:00
- Add @~/.claude/CLAUDE.md import for global engineering standards - Update Last Updated date to 2026-01-23 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
100 lines
3.4 KiB
TypeScript
100 lines
3.4 KiB
TypeScript
import { formatCurrency, formatNumber, formatDate, formatPercent, toISOString } from '@/lib/format';
|
|
|
|
describe('Format Utilities', () => {
|
|
describe('formatCurrency', () => {
|
|
it('formats numbers as CHF currency by default (Swiss context)', () => {
|
|
// Swiss German locale uses RIGHT SINGLE QUOTATION MARK (U+2019) as thousands separator
|
|
expect(formatCurrency(1000)).toMatch(/CHF\s*1[''\u2019]000/);
|
|
expect(formatCurrency(1234567)).toMatch(/CHF\s*1[''\u2019]234[''\u2019]567/);
|
|
expect(formatCurrency(0)).toMatch(/CHF\s*0/);
|
|
});
|
|
|
|
it('supports USD currency option', () => {
|
|
expect(formatCurrency(1000, { currency: 'USD' })).toBe('$1,000');
|
|
expect(formatCurrency(1234567, { currency: 'USD' })).toBe('$1,234,567');
|
|
});
|
|
|
|
it('supports compact notation', () => {
|
|
const result = formatCurrency(1234567, { compact: true });
|
|
// Swiss German locale uses "Mio." for million
|
|
expect(result).toMatch(/1[\.,]?2?\s*M|Mio/i);
|
|
});
|
|
|
|
it('supports decimal places', () => {
|
|
const result = formatCurrency(1234.56, { decimals: 2 });
|
|
// Swiss German locale uses RIGHT SINGLE QUOTATION MARK as thousands separator
|
|
expect(result).toMatch(/CHF\s*1[''\u2019]234[\.,]56/);
|
|
});
|
|
});
|
|
|
|
describe('formatNumber', () => {
|
|
it('formats numbers with locale separators', () => {
|
|
expect(formatNumber(1000)).toBe('1,000');
|
|
expect(formatNumber(1234567)).toBe('1,234,567');
|
|
});
|
|
|
|
it('supports compact notation', () => {
|
|
const result = formatNumber(1234567, { compact: true });
|
|
expect(result).toMatch(/1\.2M|1M/);
|
|
});
|
|
|
|
it('supports decimal places', () => {
|
|
expect(formatNumber(1234.567, { decimals: 2 })).toBe('1,234.57');
|
|
});
|
|
});
|
|
|
|
describe('formatDate', () => {
|
|
it('formats dates with default long style', () => {
|
|
const result = formatDate('2024-01-15');
|
|
expect(result).toContain('January');
|
|
expect(result).toContain('15');
|
|
expect(result).toContain('2024');
|
|
});
|
|
|
|
it('supports short style', () => {
|
|
const result = formatDate('2024-01-15', { style: 'short' });
|
|
expect(result).toMatch(/1\/15\/24|15\/1\/24/); // US or other locale
|
|
});
|
|
|
|
it('accepts Date objects', () => {
|
|
const date = new Date('2024-06-20');
|
|
const result = formatDate(date);
|
|
expect(result).toContain('June');
|
|
expect(result).toContain('20');
|
|
});
|
|
|
|
it('accepts timestamps', () => {
|
|
const timestamp = new Date('2024-03-10').getTime();
|
|
const result = formatDate(timestamp);
|
|
expect(result).toContain('March');
|
|
expect(result).toContain('10');
|
|
});
|
|
});
|
|
|
|
describe('formatPercent', () => {
|
|
it('formats decimal values as percentages', () => {
|
|
expect(formatPercent(0.5)).toBe('50%');
|
|
expect(formatPercent(1)).toBe('100%');
|
|
expect(formatPercent(0.123)).toBe('12%');
|
|
});
|
|
|
|
it('supports decimal places', () => {
|
|
expect(formatPercent(0.1234, { decimals: 1 })).toBe('12.3%');
|
|
expect(formatPercent(0.8567, { decimals: 2 })).toBe('85.67%');
|
|
});
|
|
});
|
|
|
|
describe('toISOString', () => {
|
|
it('returns ISO string for current date when no argument', () => {
|
|
const result = toISOString();
|
|
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
|
|
});
|
|
|
|
it('returns ISO string for provided date', () => {
|
|
const date = new Date('2024-01-15T10:30:00Z');
|
|
const result = toISOString(date);
|
|
expect(result).toBe('2024-01-15T10:30:00.000Z');
|
|
});
|
|
});
|
|
});
|