Skip to content

Commit

Permalink
Remove update transaction mock and correctly test gas fee increase sc…
Browse files Browse the repository at this point in the history
…enarios now that updateTransaction used in cancel-speedup is defined on the front end
  • Loading branch information
danjm committed Apr 13, 2022
1 parent b7effc5 commit 86827f7
Showing 1 changed file with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { act, screen } from '@testing-library/react';
import BigNumber from 'bignumber.js';

import {
EDIT_GAS_MODES,
Expand All @@ -10,9 +11,47 @@ 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 {
hexWEIToDecETH,
decGWEIToHexWEI,
} from '../../../helpers/utils/conversions.util';

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);
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[GAS_ESTIMATE_TYPES.FEE_MARKET].gasFeeEstimates.medium
.suggestedMaxFeePerGas;
const MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_BN_WEI = new BigNumber(
decGWEIToHexWEI(MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_DEC_GWEI),
16,
);
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),
);

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()),
Expand All @@ -21,7 +60,6 @@ jest.mock('../../../store/actions', () => ({
.mockImplementation(() => Promise.resolve()),
addPollingTokenToAppState: jest.fn(),
removePollingTokenFromAppState: jest.fn(),
updateTransaction: () => ({ type: 'UPDATE_TRANSACTION_PARAMS' }),
updateTransactionGasFees: () => ({ type: 'UPDATE_TRANSACTION_PARAMS' }),
updatePreviousGasParams: () => ({ type: 'UPDATE_TRANSACTION_PARAMS' }),
createTransactionEventFragment: jest.fn(),
Expand All @@ -34,7 +72,10 @@ jest.mock('../../../contexts/transaction-modal', () => ({
}),
}));

const render = (props) => {
const render = (
props,
maxFeePerGas = MOCK_SUGGESTED_MEDIUM_MAXFEEPERGAS_HEX_WEI,
) => {
const store = configureStore({
metamask: {
...mockState.metamask,
Expand All @@ -56,7 +97,7 @@ const render = (props) => {
userFeeLevel: 'tenPercentIncreased',
txParams: {
gas: '0x5208',
maxFeePerGas: '0x59682f10',
maxFeePerGas,
maxPriorityFeePerGas: '0x59682f00',
},
}}
Expand All @@ -80,12 +121,32 @@ describe('CancelSpeedupPopover', () => {
expect(screen.queryByText('🚀Speed Up')).toBeInTheDocument();
});

it('should show correct gas values', async () => {
it('should show correct gas values, increased by 10%, when initial initial gas value is above estimated medium', async () => {
await act(async () =>
render({
editGasMode: EDIT_GAS_MODES.SPEED_UP,
}),
render(
{
editGasMode: EDIT_GAS_MODES.SPEED_UP,
},
MAXFEEPERGAS_ABOVE_MOCK_MEDIUM_HEX,
),
);
expect(screen.queryAllByTitle('0.0000315 ETH').length).toBeGreaterThan(0);
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: EDIT_GAS_MODES.SPEED_UP,
},
`0x${MAXFEEPERGAS_BELOW_MOCK_MEDIUM_HEX}`,
),
);

expect(
screen.queryAllByTitle(`${EXPECTED_ETH_FEE_2} ETH`).length,
).toBeGreaterThan(0);
});
});

0 comments on commit 86827f7

Please sign in to comment.