Skip to content
Open
Show file tree
Hide file tree
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
45 changes: 45 additions & 0 deletions apps/web/src/components/Charts/PriceChart/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { FiatCurrency } from 'uniswap/src/features/fiatCurrency/constants'
import type { useLocalizationContext } from 'uniswap/src/features/language/LocalizationContext'
import { NumberType } from 'utilities/src/format/types'
import { describe, expect, it, vi } from 'vitest'
import { formatPriceAxisLabel } from '~/components/Charts/PriceChart/utils'

function createFormat() {
return {
convertFiatAmount: vi.fn((amount: number) => ({ amount, currency: FiatCurrency.UnitedStatesDollar })),
convertFiatAmountFormatted: vi.fn((amount: number, type: NumberType) => `${type}:${amount}`),
}
}

describe('formatPriceAxisLabel', () => {
it('formats a valid fixed precision as currency', () => {
const format = createFormat()

expect(
formatPriceAxisLabel({
scaledPrice: 1.234,
scaleFactor: 1,
decimals: 2,
format: format as unknown as ReturnType<typeof useLocalizationContext>,
locale: 'en-US',
}),
).toBe('$1.23')
expect(format.convertFiatAmountFormatted).not.toHaveBeenCalled()
})

it('falls back when precision exceeds Intl.NumberFormat limits', () => {
const format = createFormat()

expect(
formatPriceAxisLabel({
scaledPrice: 1.234,
scaleFactor: 1,
decimals: 101,
format: format as unknown as ReturnType<typeof useLocalizationContext>,
locale: 'en-US',
}),
).toBe(`fiat-token-price:1.234`)
expect(format.convertFiatAmount).not.toHaveBeenCalled()
expect(format.convertFiatAmountFormatted).toHaveBeenCalledWith(1.234, NumberType.FiatTokenPrice)
})
})
8 changes: 7 additions & 1 deletion apps/web/src/components/Charts/PriceChart/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { useLocalizationContext } from 'uniswap/src/features/language/Localizati
import { NumberType } from 'utilities/src/format/types'
import { PricePoint } from '~/data/util'

const MAX_INTL_FRACTION_DIGITS = 100

function isValidFractionDigits(decimals: number): boolean {
return Number.isInteger(decimals) && decimals >= 0 && decimals <= MAX_INTL_FRACTION_DIGITS
}

/**
* Returns the minimum and maximum values in the given array of PricePoints.
*/
Expand Down Expand Up @@ -80,7 +86,7 @@ export function formatPriceAxisLabel({
if (tokenFormatType) {
return format.formatNumberOrString({ value: price, type: tokenFormatType })
}
if (decimals !== undefined) {
if (decimals !== undefined && isValidFractionDigits(decimals)) {
const { amount, currency } = format.convertFiatAmount(price)
return new Intl.NumberFormat(locale, {
style: 'currency',
Expand Down