1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-04 15:14:29 +01:00
metamask-extension/ui/pages/confirm-transaction-base/transaction-alerts/transaction-alerts.test.js

173 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-11-23 19:18:44 +01:00
import React from 'react';
import { fireEvent, screen } from '@testing-library/react';
2021-11-23 19:18:44 +01:00
import { GAS_ESTIMATE_TYPES } from '../../../../shared/constants/gas';
import {
TRANSACTION_ENVELOPE_TYPES,
TRANSACTION_STATUSES,
} from '../../../../shared/constants/transaction';
2021-11-23 19:18:44 +01:00
import { renderWithProvider } from '../../../../test/lib/render-helpers';
import mockEstimates from '../../../../test/data/mock-estimates.json';
import mockState from '../../../../test/data/mock-state.json';
2021-11-23 19:18:44 +01:00
import { GasFeeContextProvider } from '../../../contexts/gasFee';
import configureStore from '../../../store/store';
import TransactionAlerts from './transaction-alerts';
jest.mock('../../../store/actions', () => ({
disconnectGasFeeEstimatePoller: jest.fn(),
getGasFeeEstimatesAndStartPolling: jest
.fn()
.mockImplementation(() => Promise.resolve()),
addPollingTokenToAppState: jest.fn(),
}));
const render = ({ componentProps, transactionProps, state }) => {
2021-11-23 19:18:44 +01:00
const store = configureStore({
metamask: {
...mockState.metamask,
2021-11-23 19:18:44 +01:00
accounts: {
[mockState.metamask.selectedAddress]: {
address: mockState.metamask.selectedAddress,
2021-11-23 19:18:44 +01:00
balance: '0x1F4',
},
},
gasFeeEstimates: mockEstimates[GAS_ESTIMATE_TYPES.FEE_MARKET],
2021-11-23 19:18:44 +01:00
...state,
},
});
return renderWithProvider(
<GasFeeContextProvider
transaction={{
txParams: {
type: TRANSACTION_ENVELOPE_TYPES.FEE_MARKET,
},
...transactionProps,
}}
>
<TransactionAlerts {...componentProps} />
2021-11-23 19:18:44 +01:00
</GasFeeContextProvider>,
store,
);
};
describe('TransactionAlerts', () => {
beforeEach(() => {
process.env.EIP_1559_V2 = true;
});
afterEach(() => {
process.env.EIP_1559_V2 = false;
});
2021-11-23 19:18:44 +01:00
it('should returning warning message for low gas estimate', () => {
render({ transactionProps: { userFeeLevel: 'low' } });
2021-11-23 19:18:44 +01:00
expect(
document.getElementsByClassName('actionable-message--warning'),
).toHaveLength(1);
});
it('should return null for gas estimate other than low', () => {
render({ transactionProps: { userFeeLevel: 'high' } });
2021-11-23 19:18:44 +01:00
expect(
document.getElementsByClassName('actionable-message--warning'),
).toHaveLength(0);
});
it('should not show insufficient balance message if transaction value is less than balance', () => {
render({
transactionProps: {
userFeeLevel: 'high',
txParams: { value: '0x64' },
2021-11-23 19:18:44 +01:00
},
});
expect(screen.queryByText('Insufficient funds.')).not.toBeInTheDocument();
});
it('should show insufficient balance message if transaction value is more than balance', () => {
render({
transactionProps: {
userFeeLevel: 'high',
txParams: { value: '0x5208' },
2021-11-23 19:18:44 +01:00
},
});
expect(screen.queryByText('Insufficient funds.')).toBeInTheDocument();
});
it('should show pending transaction message if there are >= 1 pending transactions', () => {
render({
state: {
currentNetworkTxList: [
{
id: 0,
time: 0,
txParams: {
from: mockState.metamask.selectedAddress,
2021-11-23 19:18:44 +01:00
to: '0xRecipient',
},
status: TRANSACTION_STATUSES.SUBMITTED,
},
],
},
});
expect(
screen.queryByText('You have (1) pending transaction.'),
2021-11-23 19:18:44 +01:00
).toBeInTheDocument();
});
describe('SimulationError Message', () => {
it('should show simulation error message along with option to proceed anyway if transaction.simulationFails is true', () => {
render({ transactionProps: { simulationFails: true } });
expect(
screen.queryByText(
'We were not able to estimate gas. There might be an error in the contract and this transaction may fail.',
),
).toBeInTheDocument();
expect(
screen.queryByText('I want to proceed anyway'),
).toBeInTheDocument();
});
it('should not show options to acknowledge gas-missing warning if component prop userAcknowledgedGasMissing is already true', () => {
render({
componentProps: {
userAcknowledgedGasMissing: true,
},
transactionProps: { simulationFails: true },
});
expect(
screen.queryByText(
'We were not able to estimate gas. There might be an error in the contract and this transaction may fail.',
),
).toBeInTheDocument();
expect(
screen.queryByText('I want to proceed anyway'),
).not.toBeInTheDocument();
});
it('should call prop setUserAcknowledgedGasMissing if option to acknowledge gas-missing warning is clicked', () => {
const setUserAcknowledgedGasMissing = jest.fn();
render({
componentProps: {
setUserAcknowledgedGasMissing,
},
transactionProps: { simulationFails: true },
});
fireEvent.click(screen.queryByText('I want to proceed anyway'));
expect(setUserAcknowledgedGasMissing).toHaveBeenCalledTimes(1);
});
it('should return null for legacy transactions', () => {
const { container } = render({
transactionProps: {
txParams: {
type: TRANSACTION_ENVELOPE_TYPES.LEGACY,
},
},
});
expect(container.firstChild).toBeNull();
});
});
2021-11-23 19:18:44 +01:00
});