|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | | -import { formatBalance } from '../formatBalance'; |
| 2 | +import { formatBalance } from '../formatBalance.js'; |
| 3 | +import { TypinkError } from '../errors.js'; |
3 | 4 |
|
4 | 5 | describe('formatBalance', () => { |
5 | | - |
6 | | - it('case 1', () => { |
| 6 | + it('should work probably', () => { |
7 | 7 | expect(formatBalance(1e12, { decimals: 12, symbol: 'AZERO' })).toEqual('1 AZERO'); |
8 | | - }); |
9 | | - |
10 | | - it('case 2', () => { |
11 | 8 | expect(formatBalance(1e12, { decimals: 13, symbol: 'AZERO' })).toEqual('0.1 AZERO'); |
12 | | - }); |
13 | | - |
14 | | - it('case 3', () => { |
15 | | - expect(formatBalance(1e12, { decimals: 13 })).toEqual('0.1'); |
16 | | - }); |
17 | | - |
18 | | - it('case 4', () => { |
19 | 9 | expect(formatBalance(12_023_172_837_123, { decimals: 12 })).toEqual('12.023172837123'); |
20 | | - }); |
21 | | - |
22 | | - it('case 5', () => { |
23 | 10 | expect(formatBalance('1', { decimals: 12 })).toEqual('0.000000000001'); |
| 11 | + expect(formatBalance(1e12, { decimals: 13 })).toEqual('0.1'); |
| 12 | + expect(formatBalance(-10000, { decimals: 4 })).toEqual('-1'); |
| 13 | + expect(formatBalance('-10200', { decimals: 4 })).toEqual('-1.02'); |
| 14 | + expect(formatBalance(-1e12, { decimals: 12, symbol: 'AZERO' })).toEqual('-1 AZERO'); |
24 | 15 | }); |
25 | 16 |
|
26 | | - // Unhandled case |
27 | | - /* |
28 | | - it('case 6', () => { |
29 | | - expect(formatBalance('1.000000000001', { decimals: 12 })).toEqual('1.000000000001'); |
30 | | - }); |
31 | | - */ |
32 | | - |
33 | | - it('case 7', () => { |
34 | | - expect(() => formatBalance('1', { decimals: 12.2, symbol: 'AZERO' })).toThrow(new Error('Invalid decimals')); |
| 17 | + it('should throw error if input has bad chars', () => { |
| 18 | + expect(() => formatBalance('1.000000000001', { decimals: 12 })).toThrow( |
| 19 | + new TypinkError('Invalid value at position 1, bigint was expected'), |
| 20 | + ); |
| 21 | + expect(() => formatBalance('1,2', { decimals: 12, symbol: 'AZERO' })).toThrow( |
| 22 | + new TypinkError('Invalid value at position 1, bigint was expected'), |
| 23 | + ); |
35 | 24 | }); |
36 | 25 |
|
37 | | - it('case 8', () => { |
38 | | - expect(() => formatBalance('1,2', { decimals: 12, symbol: 'AZERO' })).toThrow(new Error('Invalid value at position 1')); |
| 26 | + it('should throw error if decimals is invalid', () => { |
| 27 | + expect(() => formatBalance('1', { decimals: 12.2, symbol: 'AZERO' })).toThrow(new TypinkError('Invalid decimals')); |
| 28 | + expect(() => formatBalance('1', { decimals: -12, symbol: 'AZERO' })).toThrow(new TypinkError('Invalid decimals')); |
39 | 29 | }); |
40 | 30 | }); |
0 commit comments