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
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class TransactionConfirmation extends Confirmation {

async closeGasFeeToastMessage() {
// the toast message automatically disappears after some seconds, so we need to use clickElementSafe to prevent race conditions
await this.driver.clickElementSafe(this.gasFeeCloseToastMessage, 5000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an intentional increase of timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we had to increase it for this test to pass on firefox, as tests are slower for that build

await this.driver.clickElementSafe(this.gasFeeCloseToastMessage, 10000);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import React from 'react';
import { Hex } from '@metamask/utils';
import { AvatarAccountSize } from '@metamask/design-system-react';
import { TransactionMeta } from '@metamask/transaction-controller';
import { Hex } from '@metamask/utils';
import React from 'react';
import { useSelector } from 'react-redux';

import { NATIVE_TOKEN_ADDRESS } from '../../../../../../../../shared/constants/transaction';
import { useConfirmContext } from '../../../../../context/confirm';
import { selectNetworkConfigurationByChainId } from '../../../../../../../selectors';
import Identicon from '../../../../../../../components/ui/identicon';
import { CHAIN_ID_TOKEN_IMAGE_MAP } from '../../../../../../../../shared/constants/network';
import { NATIVE_TOKEN_ADDRESS } from '../../../../../../../../shared/constants/transaction';
import { PreferredAvatar } from '../../../../../../../components/app/preferred-avatar';
import {
AvatarToken,
AvatarTokenSize,
Box,
} from '../../../../../../../components/component-library';
import Identicon from '../../../../../../../components/ui/identicon';
import { BackgroundColor } from '../../../../../../../helpers/constants/design-system';
import {
selectERC20TokensByChain,
selectNetworkConfigurationByChainId,
} from '../../../../../../../selectors';
import { useConfirmContext } from '../../../../../context/confirm';

export enum GasFeeTokenIconSize {
Sm = 'sm',
Expand All @@ -36,13 +40,30 @@ export function GasFeeTokenIcon({
selectNetworkConfigurationByChainId(state, chainId),
);

const erc20TokensByChain = useSelector(selectERC20TokensByChain);
const variation = chainId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor - unnecessary constant?

const { iconUrl: image } =
erc20TokensByChain?.[variation]?.data?.[tokenAddress] ?? {};

if (tokenAddress !== NATIVE_TOKEN_ADDRESS) {
return (
<Box data-testid="token-icon">
<Identicon
address={tokenAddress}
diameter={size === GasFeeTokenIconSize.Md ? 32 : 12}
/>
{image ? (
<Identicon
address={tokenAddress}
diameter={size === GasFeeTokenIconSize.Md ? 32 : 12}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor - const isMdSize = size === GasFeeTokenIconSize.Md;

image={image}
/>
) : (
<PreferredAvatar
address={tokenAddress}
size={
size === GasFeeTokenIconSize.Md
? AvatarAccountSize.Md
: AvatarAccountSize.Xs
}
/>
)}
</Box>
);
}
Expand Down
146 changes: 130 additions & 16 deletions ui/pages/confirmations/hooks/useAutomaticGasFeeTokenSelect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NATIVE_TOKEN_ADDRESS } from '../../../../shared/constants/transaction';
import { genUnapprovedContractInteractionConfirmation } from '../../../../test/data/confirmations/contract-interaction';
import { getMockConfirmStateForTransaction } from '../../../../test/data/confirmations/helper';
import { renderHookWithConfirmContextProvider } from '../../../../test/lib/confirmations/render-helpers';
import { flushPromises } from '../../../../test/lib/timer-helpers';
import { updateSelectedGasFeeToken } from '../../../store/controller-actions/transaction-controller';
import { Alert } from '../../../ducks/confirm-alerts/confirm-alerts';
import { forceUpdateMetamaskState } from '../../../store/actions';
Expand Down Expand Up @@ -46,6 +47,12 @@ function runHook({
return { ...result, state };
}

async function flushAsyncUpdates() {
await act(async () => {
await flushPromises();
});
}

describe('useAutomaticGasFeeTokenSelect', () => {
const updateSelectedGasFeeTokenMock = jest.mocked(updateSelectedGasFeeToken);
const forceUpdateMetamaskStateMock = jest.mocked(forceUpdateMetamaskState);
Expand All @@ -67,31 +74,56 @@ describe('useAutomaticGasFeeTokenSelect', () => {
});
});

it('selects first gas fee token', () => {
runHook();
it('selects first gas fee token', async () => {
const { store } = runHook();

await flushAsyncUpdates();

if (!store) {
throw new Error('Expected store to be defined');
}

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(1);
expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledWith(
expect.any(String),
GAS_FEE_TOKEN_MOCK.tokenAddress,
);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(1);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledWith(store.dispatch);
});

it('does not select first gas fee token if gas fee token already selected', () => {
it('does not select first gas fee token if gas fee token already selected', async () => {
runHook({ selectedGasFeeToken: GAS_FEE_TOKEN_MOCK.tokenAddress });

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);
});

it('does not select first gas fee token if no gas fee tokens', () => {
it('does not select first gas fee token if no gas fee tokens', async () => {
runHook({ gasFeeTokens: [] });

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);
});

it('does not select first gas fee token if not first load', () => {
const { rerender, state } = runHook({
it('selects first gas fee token on rerender when selection becomes eligible', async () => {
const { rerender, state, store } = runHook({
selectedGasFeeToken: GAS_FEE_TOKEN_MOCK.tokenAddress,
});

if (!store) {
throw new Error('Expected store to be defined');
}

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);

const transactionMeta = state.metamask
.transactions[0] as unknown as TransactionMeta;

Expand All @@ -101,41 +133,100 @@ describe('useAutomaticGasFeeTokenSelect', () => {

rerender();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(1);
expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledWith(
expect.any(String),
GAS_FEE_TOKEN_MOCK.tokenAddress,
);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(1);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledWith(store.dispatch);
});

it('does not select first gas fee token if gasless not supported', () => {
it('does not select first gas fee token if gasless not supported', async () => {
useIsGaslessSupportedMock.mockReturnValue({
isSupported: false,
isSmartTransaction: false,
});

runHook();

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);
});

it('does not select first gas fee token if sufficient balance', () => {
it('does not select first gas fee token if sufficient balance', async () => {
useInsufficientBalanceAlertsMock.mockReturnValue([]);

runHook();

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);
});

it('does not select first gas fee token after firstCheck is set to false', () => {
const { rerender, state } = runHook();
it('selects first gas fee token when insufficient balance appears after first render', async () => {
let alerts: Alert[] = [];
useInsufficientBalanceAlertsMock.mockImplementation(() => alerts);

const { rerender, store } = runHook({
selectedGasFeeToken: undefined,
});

if (!store) {
throw new Error('Expected store to be defined');
}

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);

alerts = [{} as Alert];

rerender();

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(1);
expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledWith(
expect.any(String),
GAS_FEE_TOKEN_MOCK.tokenAddress,
);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(1);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledWith(store.dispatch);
});

it('does not select first gas fee token after firstCheck is set to false', async () => {
const { rerender, state, store } = runHook();

if (!store) {
throw new Error('Expected store to be defined');
}

await flushAsyncUpdates();

// Simulate a rerender with new state that would otherwise trigger selection
act(() => {
(
state.metamask.transactions[0] as unknown as TransactionMeta
).selectedGasFeeToken = undefined;
});

rerender();

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(1); // Only first run
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(1);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledWith(store.dispatch);
});

it('does not select if transactionId is falsy', () => {
it('does not select if transactionId is falsy', async () => {
const state = getMockConfirmStateForTransaction(
genUnapprovedContractInteractionConfirmation({
gasFeeTokens: [GAS_FEE_TOKEN_MOCK],
Expand All @@ -145,15 +236,23 @@ describe('useAutomaticGasFeeTokenSelect', () => {
// Remove transactionId
state.metamask.transactions = [];
renderHookWithConfirmContextProvider(useAutomaticGasFeeTokenSelect, state);

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);
});

it('does not select if gasFeeTokens is falsy', () => {
it('does not select if gasFeeTokens is falsy', async () => {
runHook({ gasFeeTokens: [] });

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);
});

it('does not select first gas fee token if 7702 and future native token', () => {
it('does not select first gas fee token if 7702 and future native token', async () => {
useIsGaslessSupportedMock.mockReturnValue({
isSupported: true,
isSmartTransaction: false,
Expand All @@ -168,16 +267,19 @@ describe('useAutomaticGasFeeTokenSelect', () => {
],
});

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(0);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(0);
});

it('selects second gas fee token if 7702 and future native token', () => {
it('selects second gas fee token if 7702 and future native token', async () => {
useIsGaslessSupportedMock.mockReturnValue({
isSupported: true,
isSmartTransaction: false,
});

runHook({
const { store } = runHook({
gasFeeTokens: [
{
...GAS_FEE_TOKEN_MOCK,
Expand All @@ -187,6 +289,18 @@ describe('useAutomaticGasFeeTokenSelect', () => {
],
});

if (!store) {
throw new Error('Expected store to be defined');
}

await flushAsyncUpdates();

expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledTimes(1);
expect(updateSelectedGasFeeTokenMock).toHaveBeenCalledWith(
expect.any(String),
GAS_FEE_TOKEN_MOCK.tokenAddress,
);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledTimes(1);
expect(forceUpdateMetamaskStateMock).toHaveBeenCalledWith(store.dispatch);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ export function useAutomaticGasFeeTokenSelect() {
return;
}

setFirstCheck(false);

if (shouldSelect) {
await selectFirstToken();
setFirstCheck(false);
}
}, [shouldSelect, selectFirstToken, firstCheck, gasFeeTokens, transactionId]);
}