forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.test.ts
More file actions
153 lines (125 loc) · 5.05 KB
/
Copy pathformat.test.ts
File metadata and controls
153 lines (125 loc) · 5.05 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { describe, expect, it } from 'vitest'
import {
addressOf,
formatAmount,
formatDelta,
formatMoney,
formatShare,
truncateAddress,
} from './format'
describe('formatAmount', () => {
it('converts base units without floating point', () => {
// 1234.5678 USDC at 6 decimals.
expect(formatAmount('1234567800', 6)).toBe('1,234.5678')
})
it('handles a full ether, which exceeds MAX_SAFE_INTEGER in base units', () => {
expect(formatAmount('1000000000000000000', 18)).toBe('1')
expect(formatAmount('1500000000000000000', 18)).toBe('1.5')
})
it('keeps precision on a value a float would round', () => {
// Number('9007199254740993') is 9007199254740992 — the odd unit is lost.
expect(formatAmount('9007199254740993', 0, { grouping: false })).toBe('9007199254740993')
})
it('trims trailing zeros but keeps significant ones', () => {
expect(formatAmount('1500000', 6)).toBe('1.5')
expect(formatAmount('1050000', 6)).toBe('1.05')
})
it('truncates rather than rounds up', () => {
// Rounding up would show more than the person actually receives.
expect(formatAmount('1999999999', 6, { maxFractionDigits: 2 })).toBe('1,999.99')
})
it('never renders a non-zero amount as zero', () => {
// "0" on a review page reads as nothing moving.
expect(formatAmount('1', 18)).toBe('<0.000000000000000001')
expect(formatAmount('1', 6, { maxFractionDigits: 2 })).toBe('<0.000001')
})
it('renders an actual zero as zero', () => {
expect(formatAmount('0', 18)).toBe('0')
})
it('handles zero decimals', () => {
expect(formatAmount('42', 0)).toBe('42')
})
it('rejects input that is not an integer string', () => {
expect(() => formatAmount('1.5', 18)).toThrow(/not an integer/)
expect(() => formatAmount('-1', 18)).toThrow(/not an integer/)
expect(() => formatAmount('1', -1)).toThrow(/decimals/)
})
})
describe('truncateAddress', () => {
const addr = '0xd8da6bf26964af9d7eed9e03e53415d37aa96045'
it('middle-truncates', () => {
expect(truncateAddress(addr)).toBe('0xd8da…6045')
})
it('leaves short strings alone rather than mangling them', () => {
expect(truncateAddress('0xabc')).toBe('0xabc')
})
it('preserves casing, since display may be checksummed', () => {
expect(truncateAddress('0xD8dA6BF26964aF9D7eEd9e03E53415D37aA96045')).toBe('0xD8dA…6045')
})
})
describe('addressOf', () => {
it('pulls the address out of a CAIP-10 identifier', () => {
expect(addressOf('eip155:8453:0xd8da6bf26964af9d7eed9e03e53415d37aa96045')).toBe(
'0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
)
})
it('passes a bare address through', () => {
expect(addressOf('0xabc')).toBe('0xabc')
})
})
describe('formatMoney', () => {
it('splits the cents out for the headline to dim', () => {
expect(formatMoney(12431.09)).toEqual({ whole: '$12,431', fraction: '09' })
})
it('groups thousands and always shows two decimal places', () => {
expect(formatMoney(1000000)).toEqual({ whole: '$1,000,000', fraction: '00' })
expect(formatMoney(0.5)).toEqual({ whole: '$0', fraction: '50' })
})
it('rounds once, so the carry lands in the whole part', () => {
// Rounding after the split prints $12,430.100 for this.
expect(formatMoney(12430.999)).toEqual({ whole: '$12,431', fraction: '00' })
expect(formatMoney(0.995)).toEqual({ whole: '$1', fraction: '00' })
})
it('uses a real minus sign, which is digit-width in a tabular column', () => {
expect(formatMoney(-40).whole).toBe('−$40')
expect(formatMoney(-40).whole.startsWith('-')).toBe(false)
})
it('does not print NaN at somebody', () => {
expect(formatMoney(Number.NaN)).toEqual({ whole: '$0', fraction: '00' })
expect(formatMoney(Number.POSITIVE_INFINITY)).toEqual({ whole: '$0', fraction: '00' })
})
it('knows the currencies the provider can price in', () => {
expect(formatMoney(12, 'eur').whole).toBe('€12')
expect(formatMoney(12, 'GBP').whole).toBe('£12')
})
})
describe('formatDelta', () => {
it('measures the percent against yesterday, not today', () => {
// Up $100 to $1,100 is a 10% rise, not 9.09%.
expect(formatDelta(100, 1100)?.text).toBe('+$100.00 (10.00%) today')
})
it('reads a fall as a fall', () => {
const delta = formatDelta(-100, 900)
expect(delta?.direction).toBe('down')
expect(delta?.text).toBe('−$100.00 (10.00%) today')
})
it('says nothing rather than "+$0.00 (0.00%) today"', () => {
expect(formatDelta(0, 1000)).toBeNull()
expect(formatDelta(Number.NaN, 1000)).toBeNull()
})
it('drops the percent when there was nothing to grow from', () => {
expect(formatDelta(50, 50)?.text).toBe('+$50.00 today')
})
})
describe('formatShare', () => {
it('shows a share to one decimal place', () => {
expect(formatShare(0.593)).toBe('59.3%')
})
it('marks dust as dust rather than rounding it to 0.0%', () => {
expect(formatShare(0.0002)).toBe('<0.1%')
})
it('has nothing to show for a row with no positive value', () => {
expect(formatShare(0)).toBe('—')
expect(formatShare(Number.NaN)).toBe('—')
})
})