Skip to content

Commit

Permalink
test: add tests (#2566)
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin authored May 31, 2023
1 parent 7d3030c commit 536359e
Showing 1 changed file with 20 additions and 8 deletions.
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

0 comments on commit 536359e

Please sign in to comment.