1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/components/app/cancel-speedup-popover/cancel-speedup-popover.test.js

172 lines
5.5 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { act, screen } from '@testing-library/react';
import BigNumber from 'bignumber.js';
import {
EditGasModes,
GasEstimateTypes,
} from '../../../../shared/constants/gas';
import { renderWithProvider } from '../../../../test/lib/render-helpers';
import mockEstimates from '../../../../test/data/mock-estimates.json';
import mockState from '../../../../test/data/mock-state.json';
import { GasFeeContextProvider } from '../../../contexts/gasFee';
import configureStore from '../../../store/store';
import InfoTooltip from '../../ui/info-tooltip';
import {
decGWEIToHexWEI,
hexWEIToDecETH,
} from '../../../../shared/modules/conversion.utils';
import CancelSpeedupPopover from './cancel-speedup-popover';
const MAXFEEPERGAS_ABOVE_MOCK_MEDIUM_HEX = '0x174876e800';
const MAXGASCOST_ABOVE_MOCK_MEDIUM_BN = new BigNumber(
MAXFEEPERGAS_ABOVE_MOCK_MEDIUM_HEX,
16,
).times(21000, 10);
2022-07-31 20:26:40 +02:00
const MAXGASCOST_ABOVE_MOCK_MEDIUM_BN_PLUS_TEN_PCT_HEX =
MAXGASCOST_ABOVE_MOCK_MEDIUM_BN.times(1.1, 10).toString(16);
const EXPECTED_ETH_FEE_1 = hexWEIToDecETH(
MAXGASCOST_ABOVE_MOCK_MEDIUM_BN_PLUS_TEN_PCT_HEX,
);
const MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_DEC_GWEI =
mockEstimates[GasEstimateTypes.feeMarket].gasFeeEstimates.medium
.suggestedMaxFeePerGas;
const MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_BN_WEI = new BigNumber(
decGWEIToHexWEI(MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_DEC_GWEI),
16,
);
2022-07-31 20:26:40 +02:00
const MAXFEEPERGAS_BELOW_MOCK_MEDIUM_HEX =
MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_BN_WEI.div(10, 10).toString(16);
const EXPECTED_ETH_FEE_2 = hexWEIToDecETH(
MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_BN_WEI.times(21000, 10).toString(16),
);
2022-07-31 20:26:40 +02:00
const MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_HEX_WEI =
MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_BN_WEI.toString(16);
jest.mock('../../../store/actions', () => ({
disconnectGasFeeEstimatePoller: jest.fn(),
getGasFeeTimeEstimate: jest.fn().mockImplementation(() => Promise.resolve()),
getGasFeeEstimatesAndStartPolling: jest
.fn()
.mockImplementation(() => Promise.resolve()),
addPollingTokenToAppState: jest.fn(),
removePollingTokenFromAppState: jest.fn(),
update gas fees (#13646) * Draft methods to brak updateTransaction into smaller more targeted methods. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * This is a combination of 76 commits. normalize and validate tx params. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Method to normalize tx and check if it's unapproved. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move the methods to controllers/transactions/index.js Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Flesh out the methods to update transaction with custom notes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> enforce that only the properties for the specific methid can be updated via the method. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Test update gas fees Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update swap approval transaction Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> use lodash to remove undefined properties update swap transaction tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Updates transaction user settings. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Add more parameters to updateSwapTransaction approvalTxId estimatedBaseFee Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Add Update Transaction Metrics Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update transaction gas fees actions.js Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update EIP 1559 Params. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint Fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Documentations. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Remove metrics from this PR Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes: Removed unused variables Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Add more params to updateTransactionGasFees. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update eip1559 method to editableParams. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix Mocha tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> add gasPrice to updateEditableParams Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Remove duplicated Params in notes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> A few more tests to cover if transaction status is not unapproved transaction is passed more parameters than it requires. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update Transaction Gas Fees. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update gas fees in edit-gas-popover. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Remove metrics. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update gas settings and user settings. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix unit tests. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Draft methods to brak updateTransaction into smaller more targeted methods. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> normalize and validate tx params. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Method to normalize tx and check if it's unapproved. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move the methods to controllers/transactions/index.js Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Flesh out the methods to update transaction with custom notes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Test update gas fees Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update swap approval transaction Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> use lodash to remove undefined properties update swap transaction tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Updates transaction user settings. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Add Update Transaction Metrics Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update transaction gas fees actions.js Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update EIP 1559 Params. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint Fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Documentations. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Remove metrics from this PR Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes: Removed unused variables Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Add more params to updateTransactionGasFees. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update eip1559 method to editableParams. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix Mocha tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> add gasPrice to updateEditableParams Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Remove duplicated Params in notes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> A few more tests to cover if transaction status is not unapproved transaction is passed more parameters than it requires. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update Transaction Gas Fees. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Remove metrics. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Update gas settings and user settings. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix unit tests. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Remove dup;icated method from rebase. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> unrelated change Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Force re-run workflow Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fix Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Do not hideLoading since we're not showing it. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> UpdateTransaction should be renamed to updateGasFees Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> updateGasFees in gas-modal-page-container. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> fix: update previous gas params update method add types to the jsdoc comments. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> updateTransactionGasFees should have been updatePreviousGasParams Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Previous gas fees can be updated for confirmed transactions. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> add updatePreviousGasParams to mocked functions. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * we need to await the first dispatch before we call the second Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * update values to make tests pass Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * More changes to make e2e pass Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Need to wait a bit after save for changes to take effect. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove merge comments. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Await one dispatch before calling another Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * We don't need goHome anymore. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Tests must use async...await syntax too since we have await in the useTranasctionFunction Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Add delay after button click for values to update Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Wait a moment after clicking save for values to update Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Wait after clicking save... Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Merge update transaction gas fees and transaction user settings Show loading indicator on edit gas popover Fix tests. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Fix JSDoc Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * updatePreviousGasParams should also return updated transaction meta. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-03-25 18:11:04 +01:00
updateTransactionGasFees: () => ({ type: 'UPDATE_TRANSACTION_PARAMS' }),
updatePreviousGasParams: () => ({ type: 'UPDATE_TRANSACTION_PARAMS' }),
createTransactionEventFragment: jest.fn(),
}));
jest.mock('../../../contexts/transaction-modal', () => ({
useTransactionModalContext: () => ({
closeModal: () => undefined,
currentModal: 'cancelSpeedUpTransaction',
}),
}));
jest.mock('../../ui/info-tooltip', () => jest.fn(() => null));
const render = (
props,
maxFeePerGas = MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_HEX_WEI,
) => {
const store = configureStore({
metamask: {
...mockState.metamask,
accounts: {
[mockState.metamask.selectedAddress]: {
address: mockState.metamask.selectedAddress,
balance: '0x1F4',
},
},
featureFlags: { advancedInlineGas: true },
gasFeeEstimates:
mockEstimates[GasEstimateTypes.feeMarket].gasFeeEstimates,
},
});
return renderWithProvider(
<GasFeeContextProvider
transaction={{
2022-01-06 23:40:31 +01:00
userFeeLevel: 'tenPercentIncreased',
txParams: {
gas: '0x5208',
maxFeePerGas,
maxPriorityFeePerGas: '0x59682f00',
},
}}
editGasMode={EditGasModes.cancel}
{...props}
>
<CancelSpeedupPopover />
</GasFeeContextProvider>,
store,
);
};
describe('CancelSpeedupPopover', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should have ❌Cancel in header if editGasMode is cancel', async () => {
await act(async () => render());
expect(screen.queryByText('❌Cancel')).toBeInTheDocument();
});
it('should have 🚀Speed up in header if editGasMode is speedup', async () => {
await act(async () => render({ editGasMode: EditGasModes.speedUp }));
expect(screen.queryByText('🚀Speed up')).toBeInTheDocument();
});
it('information tooltip should contain the correct text if editGasMode is cancel', async () => {
await act(async () => render());
expect(
InfoTooltip.mock.calls[0][0].contentText.props.children[0],
).toStrictEqual(
'To Cancel a transaction the gas fee must be increased by at least 10% for it to be recognized by the network.',
);
});
it('information tooltip should contain the correct text if editGasMode is speedup', async () => {
await act(async () => render({ editGasMode: EditGasModes.speedUp }));
expect(
InfoTooltip.mock.calls[0][0].contentText.props.children[0],
).toStrictEqual(
'To Speed up a transaction the gas fee must be increased by at least 10% for it to be recognized by the network.',
);
});
it('should show correct gas values, increased by 10%, when initial initial gas value is above estimated medium', async () => {
await act(async () =>
render(
{
editGasMode: EditGasModes.speedUp,
},
MAXFEEPERGAS_ABOVE_MOCK_MEDIUM_HEX,
),
);
expect(
screen.queryAllByTitle(`${EXPECTED_ETH_FEE_1} ETH`).length,
).toBeGreaterThan(0);
});
it('should show correct gas values, set to the estimated medium, when initial initial gas value is below estimated medium', async () => {
await act(async () =>
render(
{
editGasMode: EditGasModes.speedUp,
},
`0x${MAXFEEPERGAS_BELOW_MOCK_MEDIUM_HEX}`,
),
);
expect(
screen.queryAllByTitle(`${EXPECTED_ETH_FEE_2} ETH`).length,
).toBeGreaterThan(0);
});
});