|
| 1 | +import { formatTokenAmount } from './number-formatter'; |
| 2 | + |
| 3 | +describe('formatTokenAmount', () => { |
| 4 | + describe('with 18 decimals (default)', () => { |
| 5 | + it('should format zero correctly', () => { |
| 6 | + expect(formatTokenAmount('0')).toBe('0'); |
| 7 | + expect(formatTokenAmount(0)).toBe('0'); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should format very small amounts as <0.1', () => { |
| 11 | + expect(formatTokenAmount('1')).toBe('<0.1'); // 1 wei |
| 12 | + expect(formatTokenAmount('50000000000000000')).toBe('<0.1'); // 0.05 token |
| 13 | + expect(formatTokenAmount('99999999999999999')).toBe('0.1'); // 0.099... token (rounds to 0.1) |
| 14 | + }); |
| 15 | + |
| 16 | + it('should format small amounts with 1 decimal', () => { |
| 17 | + expect(formatTokenAmount('100000000000000000')).toBe('0.1'); // 0.1 token |
| 18 | + expect(formatTokenAmount('100000000000000001')).toBe('0.1'); // 0.1+ token (rounded) |
| 19 | + expect(formatTokenAmount('500000000000000000')).toBe('0.5'); // 0.5 token |
| 20 | + expect(formatTokenAmount('1000000000000000000')).toBe('1.0'); // 1 token |
| 21 | + expect(formatTokenAmount('1500000000000000000')).toBe('1.5'); // 1.5 tokens |
| 22 | + expect(formatTokenAmount('999000000000000000000')).toBe('999.0'); // 999 tokens |
| 23 | + }); |
| 24 | + |
| 25 | + it('should format thousands with K suffix', () => { |
| 26 | + expect(formatTokenAmount('1000000000000000000000')).toBe('1.0K'); // 1,000 tokens |
| 27 | + expect(formatTokenAmount('1234000000000000000000')).toBe('1.2K'); // 1,234 tokens |
| 28 | + expect(formatTokenAmount('12345000000000000000000')).toBe('12.3K'); // 12,345 tokens |
| 29 | + expect(formatTokenAmount('999999000000000000000000')).toBe('1.0M'); // 999,999 tokens (rounded up to 1M) |
| 30 | + }); |
| 31 | + |
| 32 | + it('should format millions with M suffix', () => { |
| 33 | + expect(formatTokenAmount('1000000000000000000000000')).toBe('1.0M'); // 1 million tokens |
| 34 | + expect(formatTokenAmount('1234567000000000000000000')).toBe('1.2M'); // 1.234567 million tokens |
| 35 | + expect(formatTokenAmount('999999999000000000000000000')).toBe('1.0B'); // ~1 billion tokens (rounded up to 1B) |
| 36 | + }); |
| 37 | + |
| 38 | + it('should format billions with B suffix', () => { |
| 39 | + expect(formatTokenAmount('1000000000000000000000000000')).toBe('1.0B'); // 1 billion tokens |
| 40 | + expect(formatTokenAmount('1234567890000000000000000000')).toBe('1.2B'); // 1.23... billion tokens |
| 41 | + }); |
| 42 | + |
| 43 | + it('should use exponential notation for very large numbers', () => { |
| 44 | + expect(formatTokenAmount('1000000000000000000000000000000')).toBe('1.0e+12'); // trillion (uses exponential) |
| 45 | + }); |
| 46 | + |
| 47 | + it('should promote units when rounding results in 1000+', () => { |
| 48 | + // Test promotion from K to M (999.95K rounds to 1000.0K -> 1.0M) |
| 49 | + expect(formatTokenAmount('999950000000000000000000')).toBe('1.0M'); |
| 50 | + |
| 51 | + // Test promotion from M to B (999.95M rounds to 1000.0M -> 1.0B) |
| 52 | + expect(formatTokenAmount('999950000000000000000000000')).toBe('1.0B'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('with custom decimals', () => { |
| 57 | + it('should handle 6 decimals (like USDC)', () => { |
| 58 | + expect(formatTokenAmount('1000000', 6)).toBe('1.0'); // 1 USDC |
| 59 | + expect(formatTokenAmount('1500000', 6)).toBe('1.5'); // 1.5 USDC |
| 60 | + expect(formatTokenAmount('1000000000', 6)).toBe('1.0K'); // 1,000 USDC |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle 8 decimals (like WBTC)', () => { |
| 64 | + expect(formatTokenAmount('100000000', 8)).toBe('1.0'); // 1 WBTC |
| 65 | + expect(formatTokenAmount('50000000', 8)).toBe('0.5'); // 0.5 WBTC |
| 66 | + expect(formatTokenAmount('100000000000', 8)).toBe('1.0K'); // 1,000 WBTC |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments