83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { NumberFormatter } from './NumberFormatter';
|
|
|
|
describe('NumberFormatter', () => {
|
|
describe('format', () => {
|
|
it('should format number with thousands separators', () => {
|
|
expect(NumberFormatter.format(1234567)).toBe('1,234,567');
|
|
expect(NumberFormatter.format(1000000)).toBe('1,000,000');
|
|
});
|
|
|
|
it('should handle numbers without thousands separators', () => {
|
|
expect(NumberFormatter.format(123)).toBe('123');
|
|
expect(NumberFormatter.format(999)).toBe('999');
|
|
});
|
|
|
|
it('should handle decimal numbers', () => {
|
|
expect(NumberFormatter.format(1234.56)).toBe('1,234.56');
|
|
expect(NumberFormatter.format(1234567.89)).toBe('1,234,567.89');
|
|
});
|
|
|
|
it('should handle zero', () => {
|
|
expect(NumberFormatter.format(0)).toBe('0');
|
|
});
|
|
|
|
it('should handle negative numbers', () => {
|
|
expect(NumberFormatter.format(-1234567)).toBe('-1,234,567');
|
|
expect(NumberFormatter.format(-1234.56)).toBe('-1,234.56');
|
|
});
|
|
|
|
it('should handle large numbers', () => {
|
|
expect(NumberFormatter.format(1234567890)).toBe('1,234,567,890');
|
|
});
|
|
});
|
|
|
|
describe('formatCompact', () => {
|
|
it('should format numbers under 1000 as is', () => {
|
|
expect(NumberFormatter.formatCompact(123)).toBe('123');
|
|
expect(NumberFormatter.formatCompact(999)).toBe('999');
|
|
});
|
|
|
|
it('should format numbers 1000-999999 with k suffix', () => {
|
|
expect(NumberFormatter.formatCompact(1000)).toBe('1.0k');
|
|
expect(NumberFormatter.formatCompact(1234)).toBe('1.2k');
|
|
expect(NumberFormatter.formatCompact(999999)).toBe('1000.0k');
|
|
});
|
|
|
|
it('should format numbers 1000000 and above with M suffix', () => {
|
|
expect(NumberFormatter.formatCompact(1000000)).toBe('1.0M');
|
|
expect(NumberFormatter.formatCompact(1234567)).toBe('1.2M');
|
|
expect(NumberFormatter.formatCompact(999999999)).toBe('1000.0M');
|
|
});
|
|
|
|
it('should handle zero', () => {
|
|
expect(NumberFormatter.formatCompact(0)).toBe('0');
|
|
});
|
|
|
|
it('should handle negative numbers', () => {
|
|
expect(NumberFormatter.formatCompact(-1234)).toBe('-1.2k');
|
|
expect(NumberFormatter.formatCompact(-1234567)).toBe('-1.2M');
|
|
});
|
|
});
|
|
|
|
describe('formatCurrency', () => {
|
|
it('should format number with currency symbol', () => {
|
|
expect(NumberFormatter.formatCurrency(1234567, 'USD')).toBe('USD 1,234,567');
|
|
expect(NumberFormatter.formatCurrency(1234.56, 'EUR')).toBe('EUR 1,234.56');
|
|
});
|
|
|
|
it('should handle zero', () => {
|
|
expect(NumberFormatter.formatCurrency(0, 'USD')).toBe('USD 0');
|
|
});
|
|
|
|
it('should handle negative numbers', () => {
|
|
expect(NumberFormatter.formatCurrency(-1234567, 'USD')).toBe('USD -1,234,567');
|
|
});
|
|
|
|
it('should handle different currencies', () => {
|
|
expect(NumberFormatter.formatCurrency(1234567, 'GBP')).toBe('GBP 1,234,567');
|
|
expect(NumberFormatter.formatCurrency(1234567, 'JPY')).toBe('JPY 1,234,567');
|
|
});
|
|
});
|
|
});
|