Files
gridpilot.gg/apps/website/lib/formatters/CurrencyFormatter.test.ts
Marc Mintel 3db2209d2a
Some checks failed
CI / lint-typecheck (push) Failing after 4m52s
CI / tests (push) Has been skipped
CI / contract-tests (push) Has been skipped
CI / e2e-tests (push) Has been skipped
CI / comment-pr (push) Has been skipped
CI / commit-types (push) Has been skipped
formatter tests
2026-01-25 11:17:47 +01:00

77 lines
3.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { CurrencyFormatter } from './CurrencyFormatter';
describe('CurrencyFormatter', () => {
describe('format', () => {
it('should format USD with dollar sign and commas', () => {
expect(CurrencyFormatter.format(1234.56, 'USD')).toBe('$1,234.56');
expect(CurrencyFormatter.format(1000000, 'USD')).toBe('$1,000,000.00');
});
it('should format EUR with euro sign and dots as thousands separator', () => {
expect(CurrencyFormatter.format(1234.56, 'EUR')).toBe('€1.234,56');
expect(CurrencyFormatter.format(1000000, 'EUR')).toBe('€1.000.000,00');
});
it('should format with custom currency symbol', () => {
expect(CurrencyFormatter.format(1234.56, 'GBP')).toBe('GBP 1,234.56');
expect(CurrencyFormatter.format(1234.56, 'JPY')).toBe('JPY 1,234.56');
});
it('should use USD as default currency', () => {
expect(CurrencyFormatter.format(1234.56)).toBe('$1,234.56');
});
it('should handle zero amount', () => {
expect(CurrencyFormatter.format(0, 'USD')).toBe('$0.00');
expect(CurrencyFormatter.format(0, 'EUR')).toBe('€0,00');
});
it('should handle negative amounts', () => {
expect(CurrencyFormatter.format(-1234.56, 'USD')).toBe('$-1,234.56');
expect(CurrencyFormatter.format(-1234.56, 'EUR')).toBe('€-1.234,56');
});
it('should handle amounts with many decimal places', () => {
expect(CurrencyFormatter.format(1234.5678, 'USD')).toBe('$1,234.57');
expect(CurrencyFormatter.format(1234.5678, 'EUR')).toBe('€1.234,57');
});
});
describe('formatCompact', () => {
it('should format USD with dollar sign and no decimals', () => {
expect(CurrencyFormatter.formatCompact(1234.56, 'USD')).toBe('$1,235');
expect(CurrencyFormatter.formatCompact(1000000, 'USD')).toBe('$1,000,000');
});
it('should format EUR with euro sign and dots as thousands separator', () => {
expect(CurrencyFormatter.formatCompact(1234.56, 'EUR')).toBe('€1.235');
expect(CurrencyFormatter.formatCompact(1000000, 'EUR')).toBe('€1.000.000');
});
it('should format with custom currency symbol', () => {
expect(CurrencyFormatter.formatCompact(1234.56, 'GBP')).toBe('GBP 1,235');
expect(CurrencyFormatter.formatCompact(1234.56, 'JPY')).toBe('JPY 1,235');
});
it('should use USD as default currency', () => {
expect(CurrencyFormatter.formatCompact(1234.56)).toBe('$1,235');
});
it('should handle zero amount', () => {
expect(CurrencyFormatter.formatCompact(0, 'USD')).toBe('$0');
expect(CurrencyFormatter.formatCompact(0, 'EUR')).toBe('€0');
});
it('should handle negative amounts', () => {
expect(CurrencyFormatter.formatCompact(-1234.56, 'USD')).toBe('$-1,235');
expect(CurrencyFormatter.formatCompact(-1234.56, 'EUR')).toBe('€-1.235');
});
it('should round amounts correctly', () => {
expect(CurrencyFormatter.formatCompact(1234.4, 'USD')).toBe('$1,234');
expect(CurrencyFormatter.formatCompact(1234.6, 'USD')).toBe('$1,235');
});
});
});