2021-07-09 20:26:54 +02:00
|
|
|
import { act, renderHook } from '@testing-library/react-hooks';
|
|
|
|
import { useSelector } from 'react-redux';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { TransactionEnvelopeType } from '../../../shared/constants/transaction';
|
2021-11-06 01:59:23 +01:00
|
|
|
import {
|
2023-01-27 19:28:03 +01:00
|
|
|
GasRecommendations,
|
|
|
|
EditGasModes,
|
2021-11-06 01:59:23 +01:00
|
|
|
} from '../../../shared/constants/gas';
|
2021-08-03 00:52:18 +02:00
|
|
|
|
2021-10-22 19:14:28 +02:00
|
|
|
import { ETH, PRIMARY } from '../../helpers/constants/common';
|
2021-08-03 00:52:18 +02:00
|
|
|
|
2021-10-22 19:14:28 +02:00
|
|
|
import { useUserPreferencedCurrency } from '../useUserPreferencedCurrency';
|
|
|
|
import { useGasFeeEstimates } from '../useGasFeeEstimates';
|
2021-07-09 20:26:54 +02:00
|
|
|
import { useGasFeeInputs } from './useGasFeeInputs';
|
|
|
|
|
2021-10-22 19:14:28 +02:00
|
|
|
import {
|
|
|
|
LEGACY_GAS_ESTIMATE_RETURN_VALUE,
|
|
|
|
FEE_MARKET_ESTIMATE_RETURN_VALUE,
|
|
|
|
HIGH_FEE_MARKET_ESTIMATE_RETURN_VALUE,
|
|
|
|
configureEIP1559,
|
|
|
|
configureLegacy,
|
|
|
|
generateUseSelectorRouter,
|
|
|
|
} from './test-utils';
|
|
|
|
|
|
|
|
jest.mock('../useUserPreferencedCurrency', () => ({
|
2021-07-09 20:26:54 +02:00
|
|
|
useUserPreferencedCurrency: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2021-10-22 19:14:28 +02:00
|
|
|
jest.mock('../useGasFeeEstimates', () => ({
|
2021-07-09 20:26:54 +02:00
|
|
|
useGasFeeEstimates: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('react-redux', () => {
|
|
|
|
const actual = jest.requireActual('react-redux');
|
|
|
|
|
|
|
|
return {
|
|
|
|
...actual,
|
|
|
|
useSelector: jest.fn(),
|
2021-11-18 18:54:58 +01:00
|
|
|
useDispatch: () => jest.fn(),
|
2021-07-09 20:26:54 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('useGasFeeInputs', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
useUserPreferencedCurrency.mockImplementation((type) => {
|
|
|
|
if (type === PRIMARY) {
|
|
|
|
return { currency: ETH, numberOfDecimals: 6 };
|
|
|
|
}
|
|
|
|
return { currency: 'USD', numberOfDecimals: 2 };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when using gasPrice API for estimation', () => {
|
|
|
|
beforeEach(() => {
|
2021-10-22 19:14:28 +02:00
|
|
|
configureLegacy();
|
2021-07-09 20:26:54 +02:00
|
|
|
});
|
|
|
|
it('passes through the raw estimate values from useGasFeeEstimates', () => {
|
|
|
|
const { result } = renderHook(() => useGasFeeInputs());
|
|
|
|
expect(result.current.gasFeeEstimates).toMatchObject(
|
|
|
|
LEGACY_GAS_ESTIMATE_RETURN_VALUE.gasFeeEstimates,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns gasPrice appropriately, and "0" for EIP1559 fields', () => {
|
|
|
|
const { result } = renderHook(() => useGasFeeInputs());
|
|
|
|
expect(result.current.gasPrice).toBe(
|
|
|
|
LEGACY_GAS_ESTIMATE_RETURN_VALUE.gasFeeEstimates.medium,
|
|
|
|
);
|
|
|
|
expect(result.current.maxFeePerGas).toBe('0');
|
|
|
|
expect(result.current.maxPriorityFeePerGas).toBe('0');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates values when user modifies gasPrice', () => {
|
2021-07-31 02:45:18 +02:00
|
|
|
useSelector.mockImplementation(
|
2021-08-03 00:52:18 +02:00
|
|
|
generateUseSelectorRouter({
|
|
|
|
checkNetworkAndAccountSupports1559Response: false,
|
|
|
|
}),
|
2021-07-31 02:45:18 +02:00
|
|
|
);
|
2021-07-09 20:26:54 +02:00
|
|
|
const { result } = renderHook(() => useGasFeeInputs());
|
|
|
|
expect(result.current.gasPrice).toBe(
|
|
|
|
LEGACY_GAS_ESTIMATE_RETURN_VALUE.gasFeeEstimates.medium,
|
|
|
|
);
|
|
|
|
act(() => {
|
|
|
|
result.current.setGasPrice('30');
|
|
|
|
});
|
|
|
|
expect(result.current.gasPrice).toBe('30');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-06 20:29:57 +02:00
|
|
|
describe('when transaction is type-0', () => {
|
|
|
|
beforeEach(() => {
|
2021-10-22 19:14:28 +02:00
|
|
|
configureEIP1559();
|
2021-10-06 20:29:57 +02:00
|
|
|
});
|
2021-10-22 19:14:28 +02:00
|
|
|
|
2021-10-06 20:29:57 +02:00
|
|
|
it('returns gasPrice appropriately, and "0" for EIP1559 fields', () => {
|
|
|
|
const { result } = renderHook(() =>
|
2023-01-27 19:28:03 +01:00
|
|
|
useGasFeeInputs(GasRecommendations.medium, {
|
2021-10-06 20:29:57 +02:00
|
|
|
txParams: {
|
|
|
|
value: '3782DACE9D90000',
|
|
|
|
gasLimit: '0x5028',
|
|
|
|
gasPrice: '0x5028',
|
2023-01-18 15:47:29 +01:00
|
|
|
type: TransactionEnvelopeType.legacy,
|
2021-10-06 20:29:57 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(result.current.gasPrice).toBe(0.00002052);
|
|
|
|
expect(result.current.maxFeePerGas).toBe('0');
|
|
|
|
expect(result.current.maxPriorityFeePerGas).toBe('0');
|
|
|
|
expect(result.current.hasBlockingGasErrors).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
describe('when using EIP 1559 API for estimation', () => {
|
|
|
|
beforeEach(() => {
|
2021-10-22 19:14:28 +02:00
|
|
|
configureEIP1559();
|
2021-07-09 20:26:54 +02:00
|
|
|
});
|
|
|
|
it('passes through the raw estimate values from useGasFeeEstimates', () => {
|
|
|
|
const { result } = renderHook(() => useGasFeeInputs());
|
|
|
|
expect(result.current.gasFeeEstimates).toMatchObject(
|
|
|
|
FEE_MARKET_ESTIMATE_RETURN_VALUE.gasFeeEstimates,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns EIP-1559 fields appropriately, and "0" for gasPrice fields', () => {
|
|
|
|
const { result } = renderHook(() => useGasFeeInputs());
|
|
|
|
expect(result.current.gasPrice).toBe('0');
|
|
|
|
expect(result.current.maxFeePerGas).toBe(
|
|
|
|
FEE_MARKET_ESTIMATE_RETURN_VALUE.gasFeeEstimates.medium
|
|
|
|
.suggestedMaxFeePerGas,
|
|
|
|
);
|
|
|
|
expect(result.current.maxPriorityFeePerGas).toBe(
|
|
|
|
FEE_MARKET_ESTIMATE_RETURN_VALUE.gasFeeEstimates.medium
|
|
|
|
.suggestedMaxPriorityFeePerGas,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-07-31 00:46:31 +02:00
|
|
|
|
|
|
|
describe('when balance is sufficient for minimum transaction cost', () => {
|
|
|
|
beforeEach(() => {
|
2021-10-22 19:14:28 +02:00
|
|
|
configureEIP1559();
|
2021-07-31 00:46:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false', () => {
|
|
|
|
const { result } = renderHook(() => useGasFeeInputs());
|
|
|
|
expect(result.current.balanceError).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when balance is insufficient for minimum transaction cost', () => {
|
|
|
|
beforeEach(() => {
|
2021-10-22 19:14:28 +02:00
|
|
|
configureEIP1559();
|
2021-07-31 00:46:31 +02:00
|
|
|
useGasFeeEstimates.mockImplementation(
|
|
|
|
() => HIGH_FEE_MARKET_ESTIMATE_RETURN_VALUE,
|
|
|
|
);
|
2021-10-22 19:14:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true', () => {
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useGasFeeInputs(null, {
|
2023-01-27 19:28:03 +01:00
|
|
|
userFeeLevel: GasRecommendations.medium,
|
2021-10-22 19:14:28 +02:00
|
|
|
txParams: { gas: '0x5208' },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(result.current.balanceError).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
describe('editGasMode', () => {
|
|
|
|
it('should return editGasMode passed', () => {
|
|
|
|
const { result } = renderHook(() =>
|
2023-01-27 19:28:03 +01:00
|
|
|
useGasFeeInputs(undefined, undefined, undefined, EditGasModes.swaps),
|
2021-12-12 00:26:28 +01:00
|
|
|
);
|
2023-01-27 19:28:03 +01:00
|
|
|
expect(result.current.editGasMode).toBe(EditGasModes.swaps);
|
2021-12-12 00:26:28 +01:00
|
|
|
});
|
|
|
|
});
|
2021-07-09 20:26:54 +02:00
|
|
|
});
|