Skip to content
Merged
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
13 changes: 13 additions & 0 deletions ui/hooks/useFiatFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ describe('useFiatFormatter', () => {
expect(getCurrentCurrency).toHaveBeenCalledTimes(1);
});

it('uses overrideCurrency instead of Redux currency when provided', () => {
mockGetIntlLocale.mockReturnValue('en-US');
mockGetCurrentCurrency.mockReturnValue('EUR');

const { result } = renderHook(() =>
useFiatFormatter({ overrideCurrency: 'usd' }),
);
const formatFiat = result.current;

expect(formatFiat(1.23)).toBe('$1.23');
expect(formatFiat(0)).toBe('$0.00');
});

it('should gracefully handle unknown currencies by returning amount followed by currency code', () => {
mockGetCurrentCurrency.mockReturnValue('storj');
mockGetIntlLocale.mockReturnValue('en-US');
Expand Down
11 changes: 9 additions & 2 deletions ui/hooks/useFiatFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ type FiatFormatter = (
options?: FiatFormatterOptions,
) => string;

export const useFiatFormatter = (): FiatFormatter => {
type UseFiatFormatterOptions = {
overrideCurrency?: string;
};

export const useFiatFormatter = (
hookOptions?: UseFiatFormatterOptions,
): FiatFormatter => {
const locale = useSelector(getIntlLocale);
const fiatCurrency = useSelector(getCurrentCurrency);
const reduxCurrency = useSelector(getCurrentCurrency);
const fiatCurrency = hookOptions?.overrideCurrency ?? reduxCurrency;

return (fiatAmount: number, options: FiatFormatterOptions = {}) => {
const { shorten, truncatedCharLimit, truncatedStartChars } = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ describe('MusdConversionInfo', () => {
);
});

it('passes usd as currency to CustomAmountInfo so the hero symbol is always $', () => {
render();

expect(
useTransactionCustomAmountModule.useTransactionCustomAmount,
).toHaveBeenCalledWith(expect.objectContaining({ currency: 'usd' }));
});

it('renders the custom amount input', () => {
const { getByTestId } = render();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const MusdConversionInfo = () => {

return (
<CustomAmountInfo
currency="usd"
disableAutomaticToken={true}
preferredToken={preferredToken}
hasMax={true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ describe('BridgeFeeRow', () => {
expect(queryByTestId('bridge-fee-row-tooltip')).not.toBeInTheDocument();
});

it('always renders fee in USD even when user currency is EUR', () => {
const state = getMockPersonalSignConfirmState({
metamask: { currentCurrency: 'eur' },
});
const { getByTestId } = renderWithConfirmContextProvider(
<BridgeFeeRow />,
mockStore(state),
);

expect(getByTestId('transaction-fee-value')).toHaveTextContent('$1.23');
});

it('renders fee value with ConfirmInfoRowText for Default variant', () => {
const { getByTestId } = render();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function BridgeFeeRow({
tooltipDescription,
}: BridgeFeeRowProps) {
const t = useI18nContext();
const formatFiat = useFiatFormatter();
const formatFiat = useFiatFormatter({ overrideCurrency: 'usd' });
const isLoading = useIsTransactionPayLoading();
const quotes = useTransactionPayQuotes();
const totals = useTransactionPayTotals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ describe('PayWithRow', () => {
expect(screen.getByTestId('pay-with-row')).toBeInTheDocument();
});

it('formats balance in USD regardless of user currency', () => {
const store = mockStore(getMockState());
renderWithProvider(
<PayWithRow variant={ConfirmInfoRowSize.Small} />,
store,
);

expect(useFiatFormatterMock).toHaveBeenCalledWith({
overrideCurrency: 'usd',
});
});

it('renders balance display', () => {
const store = mockStore(getMockState());
renderWithProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function PayWithRow({
const [isModalOpen, setIsModalOpen] = useState(false);
const { payToken } = useTransactionPayToken();
const requiredTokens = useTransactionPayRequiredTokens();
const fiatFormatter = useFiatFormatter();
const fiatFormatter = useFiatFormatter({ overrideCurrency: 'usd' });

const { currentConfirmation } = useConfirmContext<TransactionMeta>();
const from = currentConfirmation?.txParams?.from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ describe('TotalRow', () => {
expect(queryByText(messages.total.message)).not.toBeInTheDocument();
});

it('always renders total in USD even when user currency is EUR', () => {
const state = getMockPersonalSignConfirmState({
metamask: { currentCurrency: 'eur' },
});
const { getByTestId } = renderWithConfirmContextProvider(
<TotalRow />,
mockStore(state),
);

expect(getByTestId('total-value')).toHaveTextContent('$123.46');
});

it('renders total value with ConfirmInfoRowText for Default variant', () => {
const { getByTestId } = render();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function TotalRow({
variant = ConfirmInfoRowSize.Default,
}: TotalRowProps) {
const t = useI18nContext();
const formatFiat = useFiatFormatter();
const formatFiat = useFiatFormatter({ overrideCurrency: 'usd' });
const isLoading = useIsTransactionPayLoading();
const totals = useTransactionPayTotals();

Expand Down
Loading