2021-07-09 17:23:45 +02:00
|
|
|
import { cleanup, renderHook } from '@testing-library/react-hooks';
|
|
|
|
import { useSelector } from 'react-redux';
|
2023-01-27 19:28:03 +01:00
|
|
|
import { GasEstimateTypes } from '../../shared/constants/gas';
|
2021-07-09 17:23:45 +02:00
|
|
|
import createRandomId from '../../shared/modules/random-id';
|
|
|
|
import {
|
|
|
|
getGasEstimateType,
|
|
|
|
getGasFeeEstimates,
|
2021-08-06 01:59:58 +02:00
|
|
|
getIsGasEstimatesLoading,
|
2021-07-09 17:23:45 +02:00
|
|
|
} from '../ducks/metamask/metamask';
|
2021-08-03 00:52:18 +02:00
|
|
|
import { checkNetworkAndAccountSupports1559 } from '../selectors';
|
2021-07-09 17:23:45 +02:00
|
|
|
import {
|
|
|
|
disconnectGasFeeEstimatePoller,
|
|
|
|
getGasFeeEstimatesAndStartPolling,
|
|
|
|
} from '../store/actions';
|
2021-08-06 01:59:58 +02:00
|
|
|
|
2021-07-09 17:23:45 +02:00
|
|
|
import { useGasFeeEstimates } from './useGasFeeEstimates';
|
|
|
|
|
|
|
|
jest.mock('../store/actions', () => ({
|
|
|
|
disconnectGasFeeEstimatePoller: jest.fn(),
|
|
|
|
getGasFeeEstimatesAndStartPolling: jest.fn(),
|
2021-08-04 23:53:13 +02:00
|
|
|
addPollingTokenToAppState: jest.fn(),
|
|
|
|
removePollingTokenFromAppState: jest.fn(),
|
2021-07-09 17:23:45 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('react-redux', () => {
|
|
|
|
const actual = jest.requireActual('react-redux');
|
|
|
|
|
|
|
|
return {
|
|
|
|
...actual,
|
|
|
|
useSelector: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const DEFAULT_OPTS = {
|
2021-08-03 00:52:18 +02:00
|
|
|
checkNetworkAndAccountSupports1559: false,
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.legacy,
|
2021-07-09 17:23:45 +02:00
|
|
|
gasFeeEstimates: {
|
|
|
|
low: '10',
|
|
|
|
medium: '20',
|
|
|
|
high: '30',
|
|
|
|
},
|
2021-08-06 01:59:58 +02:00
|
|
|
isGasEstimatesLoading: true,
|
2021-07-09 17:23:45 +02:00
|
|
|
};
|
|
|
|
|
2022-07-31 20:26:40 +02:00
|
|
|
const generateUseSelectorRouter =
|
|
|
|
(opts = DEFAULT_OPTS) =>
|
|
|
|
(selector) => {
|
|
|
|
if (selector === checkNetworkAndAccountSupports1559) {
|
|
|
|
return (
|
|
|
|
opts.checkNetworkAndAccountSupports1559 ??
|
|
|
|
DEFAULT_OPTS.checkNetworkAndAccountSupports1559
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (selector === getGasEstimateType) {
|
|
|
|
return opts.gasEstimateType ?? DEFAULT_OPTS.gasEstimateType;
|
|
|
|
}
|
|
|
|
if (selector === getGasFeeEstimates) {
|
|
|
|
return opts.gasFeeEstimates ?? DEFAULT_OPTS.gasFeeEstimates;
|
|
|
|
}
|
|
|
|
if (selector === getIsGasEstimatesLoading) {
|
|
|
|
return opts.isGasEstimatesLoading ?? DEFAULT_OPTS.isGasEstimatesLoading;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|
2021-07-09 17:23:45 +02:00
|
|
|
|
|
|
|
describe('useGasFeeEstimates', () => {
|
|
|
|
let tokens = [];
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
tokens = [];
|
|
|
|
getGasFeeEstimatesAndStartPolling.mockImplementation(() => {
|
|
|
|
const token = createRandomId();
|
|
|
|
tokens.push(token);
|
|
|
|
return Promise.resolve(token);
|
|
|
|
});
|
|
|
|
disconnectGasFeeEstimatePoller.mockImplementation((token) => {
|
|
|
|
tokens = tokens.filter((tkn) => tkn !== token);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('registers with the controller', () => {
|
2021-08-06 01:59:58 +02:00
|
|
|
useSelector.mockImplementation(generateUseSelectorRouter());
|
2021-07-09 17:23:45 +02:00
|
|
|
renderHook(() => useGasFeeEstimates());
|
|
|
|
expect(tokens).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clears token with the controller on unmount', async () => {
|
2021-08-06 01:59:58 +02:00
|
|
|
useSelector.mockImplementation(generateUseSelectorRouter());
|
2021-07-09 17:23:45 +02:00
|
|
|
renderHook(() => useGasFeeEstimates());
|
|
|
|
expect(tokens).toHaveLength(1);
|
|
|
|
const expectedToken = tokens[0];
|
|
|
|
await cleanup();
|
|
|
|
expect(getGasFeeEstimatesAndStartPolling).toHaveBeenCalledTimes(1);
|
|
|
|
expect(disconnectGasFeeEstimatePoller).toHaveBeenCalledWith(expectedToken);
|
|
|
|
expect(tokens).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works with LEGACY gas prices', () => {
|
2021-08-06 01:59:58 +02:00
|
|
|
useSelector.mockImplementation(
|
|
|
|
generateUseSelectorRouter({
|
|
|
|
isGasEstimatesLoading: false,
|
|
|
|
}),
|
|
|
|
);
|
2021-07-09 17:23:45 +02:00
|
|
|
const {
|
|
|
|
result: { current },
|
|
|
|
} = renderHook(() => useGasFeeEstimates());
|
|
|
|
expect(current).toMatchObject({
|
|
|
|
gasFeeEstimates: DEFAULT_OPTS.gasFeeEstimates,
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.legacy,
|
2021-07-09 17:23:45 +02:00
|
|
|
isGasEstimatesLoading: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works with ETH_GASPRICE gas prices', () => {
|
|
|
|
const gasFeeEstimates = { gasPrice: '10' };
|
|
|
|
useSelector.mockImplementation(
|
|
|
|
generateUseSelectorRouter({
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.ethGasPrice,
|
2021-07-09 17:23:45 +02:00
|
|
|
gasFeeEstimates,
|
2021-08-06 01:59:58 +02:00
|
|
|
isGasEstimatesLoading: false,
|
2021-07-09 17:23:45 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const {
|
|
|
|
result: { current },
|
|
|
|
} = renderHook(() => useGasFeeEstimates());
|
|
|
|
expect(current).toMatchObject({
|
|
|
|
gasFeeEstimates,
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.ethGasPrice,
|
2021-07-09 17:23:45 +02:00
|
|
|
isGasEstimatesLoading: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works with FEE_MARKET gas prices', () => {
|
|
|
|
const gasFeeEstimates = {
|
|
|
|
low: {
|
|
|
|
minWaitTimeEstimate: 180000,
|
|
|
|
maxWaitTimeEstimate: 300000,
|
|
|
|
suggestedMaxPriorityFeePerGas: '3',
|
|
|
|
suggestedMaxFeePerGas: '53',
|
|
|
|
},
|
|
|
|
medium: {
|
|
|
|
minWaitTimeEstimate: 15000,
|
|
|
|
maxWaitTimeEstimate: 60000,
|
|
|
|
suggestedMaxPriorityFeePerGas: '7',
|
|
|
|
suggestedMaxFeePerGas: '70',
|
|
|
|
},
|
|
|
|
high: {
|
|
|
|
minWaitTimeEstimate: 0,
|
|
|
|
maxWaitTimeEstimate: 15000,
|
|
|
|
suggestedMaxPriorityFeePerGas: '10',
|
|
|
|
suggestedMaxFeePerGas: '100',
|
|
|
|
},
|
|
|
|
estimatedBaseFee: '50',
|
|
|
|
};
|
|
|
|
useSelector.mockImplementation(
|
|
|
|
generateUseSelectorRouter({
|
2021-08-03 00:52:18 +02:00
|
|
|
checkNetworkAndAccountSupports1559: true,
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.feeMarket,
|
2021-07-09 17:23:45 +02:00
|
|
|
gasFeeEstimates,
|
2021-08-06 01:59:58 +02:00
|
|
|
isGasEstimatesLoading: false,
|
2021-07-09 17:23:45 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const {
|
|
|
|
result: { current },
|
|
|
|
} = renderHook(() => useGasFeeEstimates());
|
|
|
|
expect(current).toMatchObject({
|
|
|
|
gasFeeEstimates,
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.feeMarket,
|
2021-07-09 17:23:45 +02:00
|
|
|
isGasEstimatesLoading: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('indicates that gas estimates are loading when gasEstimateType is NONE', () => {
|
|
|
|
useSelector.mockImplementation(
|
|
|
|
generateUseSelectorRouter({
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.none,
|
2021-07-09 17:23:45 +02:00
|
|
|
gasFeeEstimates: {},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const {
|
|
|
|
result: { current },
|
|
|
|
} = renderHook(() => useGasFeeEstimates());
|
|
|
|
expect(current).toMatchObject({
|
|
|
|
gasFeeEstimates: {},
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.none,
|
2021-07-09 17:23:45 +02:00
|
|
|
isGasEstimatesLoading: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-31 02:45:18 +02:00
|
|
|
it('indicates that gas estimates are loading when gasEstimateType is not FEE_MARKET or ETH_GASPRICE, but network supports EIP-1559', () => {
|
2021-07-09 17:23:45 +02:00
|
|
|
useSelector.mockImplementation(
|
|
|
|
generateUseSelectorRouter({
|
2021-08-03 00:52:18 +02:00
|
|
|
checkNetworkAndAccountSupports1559: true,
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.legacy,
|
2021-07-09 17:23:45 +02:00
|
|
|
gasFeeEstimates: {
|
|
|
|
gasPrice: '10',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const {
|
|
|
|
result: { current },
|
|
|
|
} = renderHook(() => useGasFeeEstimates());
|
|
|
|
expect(current).toMatchObject({
|
|
|
|
gasFeeEstimates: { gasPrice: '10' },
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.legacy,
|
2021-07-09 17:23:45 +02:00
|
|
|
isGasEstimatesLoading: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('indicates that gas estimates are loading when gasEstimateType is FEE_MARKET but network does not support EIP-1559', () => {
|
|
|
|
const gasFeeEstimates = {
|
|
|
|
low: {
|
|
|
|
minWaitTimeEstimate: 180000,
|
|
|
|
maxWaitTimeEstimate: 300000,
|
|
|
|
suggestedMaxPriorityFeePerGas: '3',
|
|
|
|
suggestedMaxFeePerGas: '53',
|
|
|
|
},
|
|
|
|
medium: {
|
|
|
|
minWaitTimeEstimate: 15000,
|
|
|
|
maxWaitTimeEstimate: 60000,
|
|
|
|
suggestedMaxPriorityFeePerGas: '7',
|
|
|
|
suggestedMaxFeePerGas: '70',
|
|
|
|
},
|
|
|
|
high: {
|
|
|
|
minWaitTimeEstimate: 0,
|
|
|
|
maxWaitTimeEstimate: 15000,
|
|
|
|
suggestedMaxPriorityFeePerGas: '10',
|
|
|
|
suggestedMaxFeePerGas: '100',
|
|
|
|
},
|
|
|
|
estimatedBaseFee: '50',
|
|
|
|
};
|
|
|
|
useSelector.mockImplementation(
|
|
|
|
generateUseSelectorRouter({
|
2021-08-03 00:52:18 +02:00
|
|
|
checkNetworkAndAccountSupports1559: false,
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.feeMarket,
|
2021-07-09 17:23:45 +02:00
|
|
|
gasFeeEstimates,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const {
|
|
|
|
result: { current },
|
|
|
|
} = renderHook(() => useGasFeeEstimates());
|
|
|
|
expect(current).toMatchObject({
|
|
|
|
gasFeeEstimates,
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.feeMarket,
|
2021-07-09 17:23:45 +02:00
|
|
|
isGasEstimatesLoading: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|