-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create functions for formatting token and usd values
- Loading branch information
1 parent
c316522
commit fb8d992
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { formatUsdValue, formatTokenValue } from './index' | ||
|
||
describe('formatUsdValue', () => { | ||
test('formats basic USD values correctly', () => { | ||
expect(formatUsdValue('5678.90')).toBe('5,678.90') | ||
expect(formatUsdValue(1234567.89123)).toBe('1,234,567.89') | ||
expect(formatUsdValue(1234567.89)).toBe('1,234,567.89') | ||
expect(formatUsdValue(1234567)).toBe('1,234,567.00') | ||
expect(formatUsdValue(1234.5)).toBe('1,234.50') | ||
expect(formatUsdValue(1234)).toBe('1,234.00') | ||
}) | ||
|
||
test('handles zero as a special case', () => { | ||
expect(formatUsdValue(0)).toBe('0.00') | ||
expect(formatUsdValue('0')).toBe('0.00') | ||
}) | ||
|
||
test('formats negative USD values correctly', () => { | ||
expect(formatUsdValue(-1234.56)).toBe('-1,234.56') | ||
}) | ||
|
||
test('rounds to two decimal places', () => { | ||
expect(formatUsdValue(1234.567)).toBe('1,234.57') | ||
}) | ||
|
||
test('small amounts', () => { | ||
expect(formatUsdValue(0.0000000099)).toBe('<0.01') | ||
expect(formatUsdValue(0.009)).toBe('<0.01') | ||
expect(formatUsdValue(0.0100000001)).toBe('0.01') | ||
expect(formatUsdValue(0.01)).toBe('0.01') | ||
expect(formatUsdValue(0.1)).toBe('0.10') | ||
}) | ||
}) | ||
|
||
describe('formatTokenValue', () => { | ||
test('formats basic token values correctly', () => { | ||
expect(formatTokenValue(1234)).toBe('1,234') | ||
expect(formatTokenValue('5678.901234')).toBe('5,678.901234') | ||
}) | ||
|
||
test('removes unnecessary trailing zeros', () => { | ||
expect(formatTokenValue(1234.5)).toBe('1,234.5') | ||
}) | ||
|
||
test('handles zero as a special case', () => { | ||
expect(formatTokenValue(0)).toBe('0.00') | ||
expect(formatTokenValue('0')).toBe('0.00') | ||
}) | ||
|
||
test('formats very small token values correctly', () => { | ||
expect(formatTokenValue(0.0000000099)).toBe('<0.00000001') | ||
expect(formatTokenValue(0.000123)).toBe('0.000123') | ||
}) | ||
|
||
test('formats negative token values correctly', () => { | ||
expect(formatTokenValue(-1234.56789)).toBe('-1,234.56789') | ||
}) | ||
|
||
test('rounds to six decimal places where applicable', () => { | ||
expect(formatTokenValue(1234.567890123)).toBe('1,234.56789012') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters