1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 23:58:06 +01:00
metamask-extension/ui/hooks/gasFeeInput/useGasFeeInputs.test.js

177 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-07-09 20:26:54 +02:00
import { act, renderHook } from '@testing-library/react-hooks';
import { useSelector } from 'react-redux';
import { TransactionEnvelopeType } from '../../../shared/constants/transaction';
import {
GasRecommendations,
EditGasModes,
} from '../../../shared/constants/gas';
import { ETH, PRIMARY } from '../../helpers/constants/common';
import { useUserPreferencedCurrency } from '../useUserPreferencedCurrency';
import { useGasFeeEstimates } from '../useGasFeeEstimates';
2021-07-09 20:26:54 +02:00
import { useGasFeeInputs } from './useGasFeeInputs';
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(),
}));
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(),
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(() => {
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', () => {
useSelector.mockImplementation(
generateUseSelectorRouter({
checkNetworkAndAccountSupports1559Response: false,
}),
);
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');
});
});
describe('when transaction is type-0', () => {
beforeEach(() => {
configureEIP1559();
});
it('returns gasPrice appropriately, and "0" for EIP1559 fields', () => {
const { result } = renderHook(() =>
useGasFeeInputs(GasRecommendations.medium, {
txParams: {
value: '3782DACE9D90000',
gasLimit: '0x5028',
gasPrice: '0x5028',
type: TransactionEnvelopeType.legacy,
},
}),
);
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(() => {
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,
);
});
});
describe('when balance is sufficient for minimum transaction cost', () => {
beforeEach(() => {
configureEIP1559();
});
it('should return false', () => {
const { result } = renderHook(() => useGasFeeInputs());
expect(result.current.balanceError).toBe(false);
});
});
describe('when balance is insufficient for minimum transaction cost', () => {
beforeEach(() => {
configureEIP1559();
useGasFeeEstimates.mockImplementation(
() => HIGH_FEE_MARKET_ESTIMATE_RETURN_VALUE,
);
});
it('should return true', () => {
const { result } = renderHook(() =>
useGasFeeInputs(null, {
userFeeLevel: GasRecommendations.medium,
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(() =>
useGasFeeInputs(undefined, undefined, undefined, EditGasModes.swaps),
2021-12-12 00:26:28 +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
});