Skip to content

Commit

Permalink
Merge branch 'network_origin_switch_alerts' of github.com:MetaMask/me…
Browse files Browse the repository at this point in the history
…tamask-extension into network_origin_switch_alerts
  • Loading branch information
jpuri committed Feb 27, 2025
2 parents e2fbcb3 + 46303fe commit fad470b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ui/components/ui/aggregated-balance/aggregated-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getMultichainNativeTokenBalance,
} from '../../../selectors/assets';
import { getPreferences, getSelectedInternalAccount } from '../../../selectors';
import { getMultichainNetwork } from '../../../selectors/multichain';
import { formatWithThreshold } from '../../app/assets/util/formatWithThreshold';
import { getIntlLocale } from '../../../ducks/locale/locale';
import Spinner from '../spinner';
Expand All @@ -45,6 +46,7 @@ export const AggregatedBalance = ({
const balances = useSelector(getTokenBalances);
const assets = useSelector(getAccountAssets);
const selectedAccount = useSelector(getSelectedInternalAccount);
const currentNetwork = useSelector(getMultichainNetwork);
const currentCurrency = useSelector(getCurrentCurrency);
const multichainAggregatedBalance = useSelector((state) =>
getMultichainAggregatedBalance(state, selectedAccount),
Expand All @@ -55,7 +57,7 @@ export const AggregatedBalance = ({

const formattedFiatDisplay = formatWithThreshold(
multichainAggregatedBalance,
0.01,
0.0,
locale,
{
style: 'currency',
Expand All @@ -65,13 +67,13 @@ export const AggregatedBalance = ({

const formattedTokenDisplay = formatWithThreshold(
multichainNativeTokenBalance.amount,
0.00001,
0.0,
locale,
{
minimumFractionDigits: 5,
minimumFractionDigits: 0,
maximumFractionDigits: 5,
},
).replace(/(\.0+|(?<=\.\d+)0+)$/u, ''); // strip trailing zeros
);

if (!balances || !assets[selectedAccount.id]?.length) {
return <Spinner className="loading-overlay__spinner" />;
Expand Down Expand Up @@ -104,7 +106,7 @@ export const AggregatedBalance = ({
isHidden={privacyMode}
>
{showNativeTokenAsMainBalance
? multichainNativeTokenBalance.unit
? currentNetwork.network.ticker
: currentCurrency.toUpperCase()}
</SensitiveText>

Expand Down
59 changes: 59 additions & 0 deletions ui/components/ui/aggregated-balance/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ describe('AggregatedBalance Component', () => {
expect(screen.getByText('USD')).toBeInTheDocument();
});

it('renders 0 fiat balance when showNativeTokenAsMainBalance is false, and balance is 0', () => {
renderWithProvider(
<AggregatedBalance
classPrefix="test"
balanceIsCached={false}
handleSensitiveToggle={jest.fn()}
/>,
getStore({
metamask: {
...mockMetamaskStore,
balances: {
[mockNonEvmAccount.id]: {
[MultichainNativeAssets.SOLANA]: {
amount: 0,
unit: 'SOL',
},
},
},
},
}),
);

expect(screen.getByTestId('account-value-and-suffix')).toHaveTextContent(
'$0.00',
);
expect(screen.getByText('USD')).toBeInTheDocument();
});

it('renders token balance when showNativeTokenAsMainBalance is true, up to 5 decimal places with no trailing zero', () => {
renderWithProvider(
<AggregatedBalance
Expand All @@ -150,4 +178,35 @@ describe('AggregatedBalance Component', () => {
);
expect(screen.getByText('SOL')).toBeInTheDocument();
});

it('renders 0 native balance when showNativeTokenAsMainBalance is true, and balance is 0', () => {
renderWithProvider(
<AggregatedBalance
classPrefix="test"
balanceIsCached={false}
handleSensitiveToggle={jest.fn()}
/>,
getStore({
metamask: {
...mockMetamaskStore,
preferences: {
showNativeTokenAsMainBalance: true,
},
balances: {
[mockNonEvmAccount.id]: {
[MultichainNativeAssets.SOLANA]: {
amount: 0,
unit: 'SOL',
},
},
},
},
}),
);

expect(screen.getByTestId('account-value-and-suffix')).toHaveTextContent(
'0',
);
expect(screen.getByText('SOL')).toBeInTheDocument();
});
});

0 comments on commit fad470b

Please sign in to comment.