Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for formatting issue #2566

Merged
merged 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/utils/amountFormat/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CurrencyAmount, Percent } from '@uniswap/sdk-core'

import { USDC_GNOSIS_CHAIN } from 'legacy/utils/gnosis_chain/constants'
import { DAI_GOERLI } from 'legacy/utils/goerli/constants'
import { DAI_GOERLI, USDC_GOERLI } from 'legacy/utils/goerli/constants'

import { formatAmountWithPrecision, formatFiatAmount, formatPercent, formatTokenAmount } from './index'

describe('Amounts formatting', () => {
const decimals = DAI_GOERLI.decimals
const getAmount = (value: string, decimalsShift: number) =>
CurrencyAmount.fromRawAmount(DAI_GOERLI, value + '0'.repeat(decimals + decimalsShift))
const getAmount = (value: string, decimalsShift: number, token = DAI_GOERLI) =>
CurrencyAmount.fromRawAmount(token, value + '0'.repeat(token.decimals + decimalsShift))

describe('Amounts', () => {
it('Zero amount', () => {
Expand All @@ -24,7 +24,7 @@ describe('Amounts formatting', () => {
})

it('Small amount', () => {
const result = formatTokenAmount(getAmount('1', -4))
const result = formatTokenAmount(getAmount('1', -4)) // 1e-4 DAI

expect(result).toBe('0.0001')
})
Expand All @@ -41,16 +41,28 @@ describe('Amounts formatting', () => {
expect(result).toBe('100.001')
})

it('Trim all trailing zero', () => {
it('Trim all trailing zero (for exact)', () => {
const result = formatTokenAmount(getAmount('10000000', -5)) // 100.00000

expect(result).toBe('100')
})

it('Format decimals up ()', () => {
const result = formatTokenAmount(getAmount('9999999999999999999', -decimals)) // 9.99... DAI
it('Trim all trailing zero (when rounded down to zero)', () => {
const result = formatTokenAmount(getAmount('10000001', -5)) // 100.00001

expect(result).toBe('10')
expect(result).toBe('100')
})

it('Trim all trailing zero (when rounded up to zero)', () => {
const result = formatTokenAmount(getAmount('9999999', -5)) // 99.99999

expect(result).toBe('100')
})

it('Format decimals up (when rounded up to zero, 6 decimals)', () => {
const result = formatTokenAmount(getAmount('1850999994', -6, USDC_GOERLI)) //1850.999994

expect(result).toBe('1,851')
})

it('One amount', () => {
Expand Down