aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/test/util/format.test.ts
blob: 38bf356ec64e7c05906ff8447de8a241bd4ad0c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { BigNumber } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';

import { ETH_DECIMALS } from '../../src/constants';
import { format } from '../../src/util/format';

const BIG_NUMBER_ONE = new BigNumber(1);
const BIG_NUMBER_DECIMAL = new BigNumber(0.432414);
const BIG_NUMBER_IRRATIONAL = new BigNumber(5.3014059295032);
const ONE_ETH_IN_BASE_UNITS = Web3Wrapper.toBaseUnitAmount(BIG_NUMBER_ONE, ETH_DECIMALS);
const DECIMAL_ETH_IN_BASE_UNITS = Web3Wrapper.toBaseUnitAmount(BIG_NUMBER_DECIMAL, ETH_DECIMALS);
const IRRATIONAL_ETH_IN_BASE_UNITS = Web3Wrapper.toBaseUnitAmount(BIG_NUMBER_IRRATIONAL, ETH_DECIMALS);
const BIG_NUMBER_FAKE_ETH_USD_PRICE = new BigNumber(2.534);

describe('format', () => {
    describe('ethBaseAmount', () => {
        it('converts 1 ETH in base units to the string `1 ETH`', () => {
            expect(format.ethBaseUnitAmount(ONE_ETH_IN_BASE_UNITS)).toBe('1 ETH');
        });
        it('converts .432414 ETH in base units to the string `.4324 ETH`', () => {
            expect(format.ethBaseUnitAmount(DECIMAL_ETH_IN_BASE_UNITS)).toBe('0.4324 ETH');
        });
        it('converts 5.3014059295032 ETH in base units to the string `5.301 ETH`', () => {
            expect(format.ethBaseUnitAmount(IRRATIONAL_ETH_IN_BASE_UNITS)).toBe('5.301 ETH');
        });
        it('returns defaultText param when ethBaseAmount is not defined', () => {
            const defaultText = 'defaultText';
            expect(format.ethBaseUnitAmount(undefined, 4, defaultText)).toBe(defaultText);
        });
        it('it allows for configurable decimal places', () => {
            expect(format.ethBaseUnitAmount(DECIMAL_ETH_IN_BASE_UNITS, 2)).toBe('0.43 ETH');
        });
    });
    describe('ethUnitAmount', () => {
        it('converts BigNumber(1) to the string `1 ETH`', () => {
            expect(format.ethUnitAmount(BIG_NUMBER_ONE)).toBe('1 ETH');
        });
        it('converts BigNumer(.432414) to the string `.4324 ETH`', () => {
            expect(format.ethUnitAmount(BIG_NUMBER_DECIMAL)).toBe('0.4324 ETH');
        });
        it('converts BigNumber(5.3014059295032) to the string `5.301 ETH`', () => {
            expect(format.ethUnitAmount(BIG_NUMBER_IRRATIONAL)).toBe('5.301 ETH');
        });
        it('shows 1 significant digit when rounded amount would be 0', () => {
            expect(format.ethUnitAmount(new BigNumber(0.00003))).toBe('0.00003 ETH');
            expect(format.ethUnitAmount(new BigNumber(0.000034))).toBe('0.00003 ETH');
            expect(format.ethUnitAmount(new BigNumber(0.000035))).toBe('0.00004 ETH');
        });
        it('shows < 0.00001 when hits threshold', () => {
            expect(format.ethUnitAmount(new BigNumber(0.000011))).toBe('0.00001 ETH');
            expect(format.ethUnitAmount(new BigNumber(0.00001))).toBe('0.00001 ETH');
            expect(format.ethUnitAmount(new BigNumber(0.000009))).toBe('< 0.00001 ETH');
            expect(format.ethUnitAmount(new BigNumber(0.0000000009))).toBe('< 0.00001 ETH');
            expect(format.ethUnitAmount(new BigNumber(0))).toBe('0 ETH');
        });
        it('returns defaultText param when ethUnitAmount is not defined', () => {
            const defaultText = 'defaultText';
            expect(format.ethUnitAmount(undefined, 4, defaultText)).toBe(defaultText);
            expect(format.ethUnitAmount(BIG_NUMBER_ONE, 4, defaultText)).toBe('1 ETH');
        });
        it('it allows for configurable decimal places', () => {
            expect(format.ethUnitAmount(BIG_NUMBER_DECIMAL, 2)).toBe('0.43 ETH');
        });
    });
    describe('ethBaseAmountInUsd', () => {
        it('correctly formats 1 ETH to usd according to some price', () => {
            expect(format.ethBaseUnitAmountInUsd(ONE_ETH_IN_BASE_UNITS, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$2.53');
        });
        it('correctly formats .432414 ETH to usd according to some price', () => {
            expect(format.ethBaseUnitAmountInUsd(DECIMAL_ETH_IN_BASE_UNITS, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe(
                '$1.10',
            );
        });
        it('correctly formats 5.3014059295032 ETH to usd according to some price', () => {
            expect(format.ethBaseUnitAmountInUsd(IRRATIONAL_ETH_IN_BASE_UNITS, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe(
                '$13.43',
            );
        });
        it('returns defaultText param when ethBaseAmountInUsd or ethUsdPrice is not defined', () => {
            const defaultText = 'defaultText';
            expect(format.ethBaseUnitAmountInUsd(undefined, undefined, 2, defaultText)).toBe(defaultText);
            expect(format.ethBaseUnitAmountInUsd(BIG_NUMBER_ONE, undefined, 2, defaultText)).toBe(defaultText);
            expect(format.ethBaseUnitAmountInUsd(undefined, BIG_NUMBER_ONE, 2, defaultText)).toBe(defaultText);
        });
        it('it allows for configurable decimal places', () => {
            expect(format.ethBaseUnitAmountInUsd(DECIMAL_ETH_IN_BASE_UNITS, BIG_NUMBER_FAKE_ETH_USD_PRICE, 4)).toBe(
                '$1.0957',
            );
        });
    });
    describe('ethUnitAmountInUsd', () => {
        it('correctly formats 1 ETH to usd according to some price', () => {
            expect(format.ethUnitAmountInUsd(BIG_NUMBER_ONE, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$2.53');
        });
        it('correctly formats .432414 ETH to usd according to some price', () => {
            expect(format.ethUnitAmountInUsd(BIG_NUMBER_DECIMAL, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$1.10');
        });
        it('correctly formats 5.3014059295032 ETH to usd according to some price', () => {
            expect(format.ethUnitAmountInUsd(BIG_NUMBER_IRRATIONAL, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$13.43');
        });
        it('correctly formats amount that is less than 1 cent', () => {
            expect(format.ethUnitAmountInUsd(new BigNumber(0.000001), BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('<$0.01');
        });
        it('correctly formats exactly 1 cent', () => {
            expect(format.ethUnitAmountInUsd(new BigNumber(0.0039), BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$0.01');
        });
        it('returns defaultText param when ethUnitAmountInUsd or ethUsdPrice is not defined', () => {
            const defaultText = 'defaultText';
            expect(format.ethUnitAmountInUsd(undefined, undefined, 2, defaultText)).toBe(defaultText);
            expect(format.ethUnitAmountInUsd(BIG_NUMBER_ONE, undefined, 2, defaultText)).toBe(defaultText);
            expect(format.ethUnitAmountInUsd(undefined, BIG_NUMBER_ONE, 2, defaultText)).toBe(defaultText);
        });
        it('it allows for configurable decimal places', () => {
            expect(format.ethUnitAmountInUsd(BIG_NUMBER_DECIMAL, BIG_NUMBER_FAKE_ETH_USD_PRICE, 4)).toBe('$1.0957');
        });
    });
});