Skip to content

Commit

Permalink
add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
namgold committed Sep 12, 2023
1 parent 912d59d commit 7a1394a
Show file tree
Hide file tree
Showing 6 changed files with 4,231 additions and 88 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"start-dev": "vite --mode dev --host",
"start-stg": "vite --mode stg --host",
"start-prod": "vite --mode production --host",
"test": "vitest --mode production",
"test-e2e": "synpress run -cf cypress.config.ts",
"test-schedule": "synpress run -cf cypress.config.ts"
},
Expand Down Expand Up @@ -155,7 +156,6 @@
"@types/big.js": "^6.0.0",
"@types/crypto-js": "4.1.1",
"@types/d3": "^7.1.0",
"@types/jest": "^25.2.1",
"@types/mixpanel-browser": "^2.38.0",
"@types/multicodec": "^1.0.0",
"@types/node": "^13.13.52",
Expand Down Expand Up @@ -189,14 +189,16 @@
"eslint-plugin-react": "^7.31.10",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-unused-imports": "^2.0.0",
"jsdom": "^22.1.0",
"prettier": "^2.7.1",
"prom-client": "^14.2.0",
"ts-node": "^10.9.1",
"typescript": "4.8.4",
"vite": "^4.3.9",
"vite-plugin-checker": "^0.5.6",
"vite-plugin-svgr": "^2.4.0",
"vite-tsconfig-paths": "^4.0.8"
"vite-tsconfig-paths": "^4.0.8",
"vitest": "^0.34.4"
},
"resolutions": {
"@kyberswap/ks-sdk-core": "1.0.11",
Expand Down
7 changes: 0 additions & 7 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { TransactionFlowState } from 'types/TransactionFlowState'
import { CAMPAIGN_BASE_URL as CAMPAIGN_BASE_DOMAIN } from './env'
import * as ENV from './env'
import { EVM_NETWORK, NETWORKS_INFO, SUPPORTED_NETWORKS, isEVM } from './networks'
import { ENV_TYPE } from './type'

export const EMPTY_OBJECT: any = {}
export const EMPTY_ARRAY: any[] = []
Expand Down Expand Up @@ -263,12 +262,6 @@ export const EIP712Domain = [
{ name: 'verifyingContract', type: 'address' },
]

if (ENV.ENV_LEVEL < ENV_TYPE.PROD) {
console.groupCollapsed('ENV')
console.log(JSON.stringify(ENV, null, 4))
console.groupEnd()
}

export const INPUT_DEBOUNCE_TIME = 300

export const ENABLE_CLICK_TO_REFRESH_GET_ROUTE = false
Expand Down
23 changes: 10 additions & 13 deletions src/utils/numbers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CurrencyAmount, Fraction, Percent, Price } from '@kyberswap/ks-sdk-core'
import JSBI from 'jsbi'

import { BIG_INT_MINUS_ONE, BIG_INT_ONE, BIG_INT_ZERO, RESERVE_USD_DECIMALS } from 'constants/index'
import { BIG_INT_ONE, BIG_INT_ZERO, RESERVE_USD_DECIMALS } from 'constants/index'

// todo: deprecated, use formatDisplayNumber instead
export const formatDollarAmount = (num: number | undefined, digits = 2) => {
Expand Down Expand Up @@ -48,7 +48,7 @@ export function toFixed(x: number): string {
const e = parseInt(x.toString().split('e-')[1])
if (e) {
x *= Math.pow(10, e - 1)
return '0.' + '0'.repeat(e - 1) + x.toString().substring(2)
return x.toString().split('.')[0] + '.' + '0'.repeat(e - 1) + x.toString().split('.')[1]
}
} else {
let e = parseInt(x.toString().split('+')[1])
Expand Down Expand Up @@ -160,17 +160,12 @@ export const formatDisplayNumber = (
if (!allowNegative && parsedFraction.lessThan(BIG_INT_ZERO)) return fallbackResult

const shownFraction = style === 'percent' ? parsedFraction.multiply(100) : parsedFraction
const absShownFraction = shownFraction.lessThan(0) ? shownFraction.multiply(-1) : shownFraction

if (
shownFraction.greaterThan(BIG_INT_MINUS_ONE) &&
shownFraction.lessThan(BIG_INT_ONE) &&
!shownFraction.equalTo(BIG_INT_ZERO)
) {
const decimal = shownFraction.toSignificant(30).split('.')[1]
if (absShownFraction.lessThan(BIG_INT_ONE) && !shownFraction.equalTo(BIG_INT_ZERO)) {
const decimal = shownFraction.toSignificant(Math.max(30, significantDigits || 0, fractionDigits || 0)).split('.')[1]
const negative = shownFraction.lessThan(BIG_INT_ZERO) ? '-' : ''
const numberOfLeadingZeros = -Math.floor(
log10(shownFraction.lessThan(0) ? shownFraction.invert() : shownFraction) + 1,
)
const numberOfLeadingZeros = -Math.floor(log10(absShownFraction) + 1)
const slicedDecimal = decimal
.replace(/^0+/, '')
.slice(0, fractionDigits)
Expand All @@ -192,7 +187,7 @@ export const formatDisplayNumber = (
}

const formatter = Intl.NumberFormat('en-US', {
notation: shownFraction.greaterThan(10 ** (significantDigits || fractionDigits || 4)) ? 'compact' : 'standard',
notation: absShownFraction.greaterThan(10 ** (significantDigits || fractionDigits || 4)) ? 'compact' : 'standard',
style,
currency: 'USD',
minimumFractionDigits: fractionDigits ? 0 : undefined,
Expand All @@ -201,7 +196,9 @@ export const formatDisplayNumber = (
maximumSignificantDigits: significantDigits,
})

const result = formatter.format(Number(parsedFraction.toSignificant(30)))
const result = formatter.format(
Number(parsedFraction.toSignificant(Math.max(30, significantDigits || 0, fractionDigits || 0))),
)

// Intl.NumberFormat does not handle maximumFractionDigits well when used along with maximumSignificantDigits
// It might return number with longer fraction digits than maximumFractionDigits
Expand Down
Loading

0 comments on commit 7a1394a

Please sign in to comment.