2021-10-22 19:14:28 +02:00
|
|
|
import { act, renderHook } from '@testing-library/react-hooks';
|
|
|
|
|
2021-11-06 01:59:23 +01:00
|
|
|
import {
|
2023-01-27 19:28:03 +01:00
|
|
|
GasRecommendations,
|
2021-11-06 01:59:23 +01:00
|
|
|
CUSTOM_GAS_ESTIMATE,
|
|
|
|
} from '../../../shared/constants/gas';
|
2021-10-22 19:14:28 +02:00
|
|
|
import {
|
|
|
|
FEE_MARKET_ESTIMATE_RETURN_VALUE,
|
|
|
|
LEGACY_GAS_ESTIMATE_RETURN_VALUE,
|
|
|
|
configure,
|
|
|
|
} from './test-utils';
|
|
|
|
import { useGasPriceInput } from './useGasPriceInput';
|
|
|
|
|
|
|
|
jest.mock('../useGasFeeEstimates', () => ({
|
|
|
|
useGasFeeEstimates: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('react-redux', () => {
|
|
|
|
const actual = jest.requireActual('react-redux');
|
|
|
|
|
|
|
|
return {
|
|
|
|
...actual,
|
|
|
|
useSelector: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('useGasPriceInput', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
configure();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns gasPrice values from transaction if transaction.userFeeLevel is custom', () => {
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useGasPriceInput({
|
|
|
|
transaction: {
|
2021-11-06 01:59:23 +01:00
|
|
|
userFeeLevel: CUSTOM_GAS_ESTIMATE,
|
2021-10-22 19:14:28 +02:00
|
|
|
txParams: { gasPrice: '0x5028' },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(result.current.gasPrice).toBe(0.00002052);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not return gasPrice values from transaction if transaction.userFeeLevel is not custom', () => {
|
|
|
|
configure();
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useGasPriceInput({
|
2023-01-27 19:28:03 +01:00
|
|
|
estimateToUse: GasRecommendations.high,
|
2021-10-22 19:14:28 +02:00
|
|
|
transaction: {
|
2023-01-27 19:28:03 +01:00
|
|
|
userFeeLevel: GasRecommendations.high,
|
2021-10-22 19:14:28 +02:00
|
|
|
txParams: { gasPrice: '0x5028' },
|
|
|
|
},
|
|
|
|
...LEGACY_GAS_ESTIMATE_RETURN_VALUE,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(result.current.gasPrice).toBe('30');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('if no gasPrice is provided returns default estimate for legacy transaction', () => {
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useGasPriceInput({
|
2023-01-27 19:28:03 +01:00
|
|
|
estimateToUse: GasRecommendations.medium,
|
2021-10-22 19:14:28 +02:00
|
|
|
...LEGACY_GAS_ESTIMATE_RETURN_VALUE,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(result.current.gasPrice).toBe('20');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('for legacy transaction if estimateToUse is high and no gasPrice is provided returns high estimate value', () => {
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useGasPriceInput({
|
2023-01-27 19:28:03 +01:00
|
|
|
estimateToUse: GasRecommendations.high,
|
2021-10-22 19:14:28 +02:00
|
|
|
...LEGACY_GAS_ESTIMATE_RETURN_VALUE,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(result.current.gasPrice).toBe('30');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 0 if gasPrice is not present in transaction and estimates are also not legacy', () => {
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useGasPriceInput({
|
2023-01-27 19:28:03 +01:00
|
|
|
estimateToUse: GasRecommendations.medium,
|
2021-10-22 19:14:28 +02:00
|
|
|
...FEE_MARKET_ESTIMATE_RETURN_VALUE,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(result.current.gasPrice).toBe('0');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns gasPrice set by user if gasPriceHasBeenManuallySet is true', () => {
|
|
|
|
const { result } = renderHook(() =>
|
2023-01-27 19:28:03 +01:00
|
|
|
useGasPriceInput({ estimateToUse: GasRecommendations.medium }),
|
2021-10-22 19:14:28 +02:00
|
|
|
);
|
|
|
|
act(() => {
|
|
|
|
result.current.setGasPriceHasBeenManuallySet(true);
|
|
|
|
result.current.setGasPrice(100);
|
|
|
|
});
|
|
|
|
expect(result.current.gasPrice).toBe(100);
|
|
|
|
});
|
|
|
|
});
|