2021-02-04 19:15:23 +01:00
|
|
|
import nock from 'nock';
|
|
|
|
import sinon from 'sinon';
|
2021-02-19 03:48:23 +01:00
|
|
|
import BN from 'bn.js';
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
import GasReducer, {
|
2018-09-09 21:25:54 +02:00
|
|
|
basicGasEstimatesLoadingStarted,
|
|
|
|
basicGasEstimatesLoadingFinished,
|
|
|
|
setBasicGasEstimateData,
|
|
|
|
setCustomGasPrice,
|
|
|
|
setCustomGasLimit,
|
2018-12-10 22:51:00 +01:00
|
|
|
fetchBasicGasEstimates,
|
2021-03-09 20:49:27 +01:00
|
|
|
} from './gas.duck';
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
jest.mock('../../../lib/storage-helpers.js', () => ({
|
|
|
|
getStorageItem: jest.fn(),
|
|
|
|
setStorageItem: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('Gas Duck', () => {
|
|
|
|
let tempDateNow;
|
|
|
|
const mockGasPriceApiResponse = {
|
|
|
|
SafeGasPrice: 10,
|
|
|
|
ProposeGasPrice: 20,
|
|
|
|
FastGasPrice: 30,
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
tempDateNow = global.Date.now;
|
2019-07-04 21:18:12 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
global.Date.now = () => 2000000;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sinon.restore();
|
|
|
|
|
|
|
|
global.Date.now = tempDateNow;
|
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
|
|
|
const mockState = {
|
2020-02-06 17:38:14 +01:00
|
|
|
mockProp: 123,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-09-09 21:25:54 +02:00
|
|
|
const initState = {
|
|
|
|
customData: {
|
2018-09-13 10:47:05 +02:00
|
|
|
price: null,
|
2018-12-12 15:54:03 +01:00
|
|
|
limit: null,
|
2018-09-09 21:25:54 +02:00
|
|
|
},
|
|
|
|
basicEstimates: {
|
|
|
|
average: null,
|
|
|
|
fast: null,
|
|
|
|
safeLow: null,
|
|
|
|
},
|
|
|
|
basicEstimateIsLoading: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-02-19 03:48:23 +01:00
|
|
|
|
|
|
|
const providerState = {
|
|
|
|
chainId: '0x1',
|
|
|
|
nickname: '',
|
|
|
|
rpcPrefs: {},
|
|
|
|
rpcUrl: '',
|
|
|
|
ticker: 'ETH',
|
|
|
|
type: 'mainnet',
|
|
|
|
};
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const BASIC_GAS_ESTIMATE_LOADING_FINISHED =
|
2021-02-04 19:15:23 +01:00
|
|
|
'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED';
|
2020-11-03 00:41:28 +01:00
|
|
|
const BASIC_GAS_ESTIMATE_LOADING_STARTED =
|
2021-02-04 19:15:23 +01:00
|
|
|
'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_STARTED';
|
|
|
|
const SET_BASIC_GAS_ESTIMATE_DATA =
|
|
|
|
'metamask/gas/SET_BASIC_GAS_ESTIMATE_DATA';
|
|
|
|
const SET_CUSTOM_GAS_LIMIT = 'metamask/gas/SET_CUSTOM_GAS_LIMIT';
|
|
|
|
const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE';
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('GasReducer()', () => {
|
|
|
|
it('should initialize state', () => {
|
|
|
|
expect(GasReducer(undefined, {})).toStrictEqual(initState);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return state unchanged if it does not match a dispatched actions type', () => {
|
|
|
|
expect(
|
2018-09-09 21:25:54 +02:00
|
|
|
GasReducer(mockState, {
|
|
|
|
type: 'someOtherAction',
|
|
|
|
value: 'someValue',
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(mockState);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should set basicEstimateIsLoading to true when receiving a BASIC_GAS_ESTIMATE_LOADING_STARTED action', () => {
|
|
|
|
expect(
|
2020-02-06 17:38:14 +01:00
|
|
|
GasReducer(mockState, { type: BASIC_GAS_ESTIMATE_LOADING_STARTED }),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({ basicEstimateIsLoading: true, ...mockState });
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should set basicEstimateIsLoading to false when receiving a BASIC_GAS_ESTIMATE_LOADING_FINISHED action', () => {
|
|
|
|
expect(
|
2020-02-06 17:38:14 +01:00
|
|
|
GasReducer(mockState, { type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({ basicEstimateIsLoading: false, ...mockState });
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should set basicEstimates when receiving a SET_BASIC_GAS_ESTIMATE_DATA action', () => {
|
|
|
|
expect(
|
2018-09-09 21:25:54 +02:00
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: { someProp: 'someData123' },
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({
|
|
|
|
basicEstimates: { someProp: 'someData123' },
|
|
|
|
...mockState,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should set customData.price when receiving a SET_CUSTOM_GAS_PRICE action', () => {
|
|
|
|
expect(
|
2018-09-09 21:25:54 +02:00
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_CUSTOM_GAS_PRICE,
|
|
|
|
value: 4321,
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({ customData: { price: 4321 }, ...mockState });
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should set customData.limit when receiving a SET_CUSTOM_GAS_LIMIT action', () => {
|
|
|
|
expect(
|
2018-09-09 21:25:54 +02:00
|
|
|
GasReducer(mockState, {
|
|
|
|
type: SET_CUSTOM_GAS_LIMIT,
|
|
|
|
value: 9876,
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({ customData: { limit: 9876 }, ...mockState });
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('basicGasEstimatesLoadingStarted', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
expect(basicGasEstimatesLoadingStarted()).toStrictEqual({
|
2020-11-03 00:41:28 +01:00
|
|
|
type: BASIC_GAS_ESTIMATE_LOADING_STARTED,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('basicGasEstimatesLoadingFinished', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
expect(basicGasEstimatesLoadingFinished()).toStrictEqual({
|
2020-11-03 00:41:28 +01:00
|
|
|
type: BASIC_GAS_ESTIMATE_LOADING_FINISHED,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('fetchBasicGasEstimates', () => {
|
|
|
|
it('should call fetch with the expected params', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mockDistpatch = sinon.spy();
|
|
|
|
const windowFetchSpy = sinon.spy(window, 'fetch');
|
2021-02-02 22:14:03 +01:00
|
|
|
|
|
|
|
nock('https://api.metaswap.codefi.network')
|
|
|
|
.get('/gasPrices')
|
2021-02-04 19:15:23 +01:00
|
|
|
.reply(200, mockGasPriceApiResponse);
|
2021-02-02 22:14:03 +01:00
|
|
|
|
2020-02-06 17:38:14 +01:00
|
|
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
2021-03-09 20:49:27 +01:00
|
|
|
gas: { ...initState },
|
2021-02-19 03:48:23 +01:00
|
|
|
metamask: { provider: { ...providerState } },
|
2021-02-04 19:15:23 +01:00
|
|
|
}));
|
2021-04-15 20:01:46 +02:00
|
|
|
|
|
|
|
expect(mockDistpatch.getCall(0).args).toStrictEqual([
|
2020-11-03 00:41:28 +01:00
|
|
|
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2021-03-09 20:49:27 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(
|
2021-02-02 22:14:03 +01:00
|
|
|
windowFetchSpy
|
2020-11-03 00:41:28 +01:00
|
|
|
.getCall(0)
|
2020-12-03 00:25:19 +01:00
|
|
|
.args[0].startsWith('https://api.metaswap.codefi.network/gasPrices'),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(true);
|
2021-03-09 20:49:27 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(mockDistpatch.getCall(2).args).toStrictEqual([
|
2020-11-03 00:41:28 +01:00
|
|
|
{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED },
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
|
|
|
});
|
2019-07-04 21:18:12 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should call fetch with the expected params for test network', async () => {
|
2021-02-19 03:48:23 +01:00
|
|
|
global.eth = { gasPrice: sinon.fake.returns(new BN(48199313, 10)) };
|
|
|
|
|
|
|
|
const mockDistpatch = sinon.spy();
|
2021-03-09 20:49:27 +01:00
|
|
|
const providerStateForTestNetwork = {
|
2021-02-19 03:48:23 +01:00
|
|
|
chainId: '0x5',
|
|
|
|
nickname: '',
|
|
|
|
rpcPrefs: {},
|
|
|
|
rpcUrl: '',
|
|
|
|
ticker: 'ETH',
|
|
|
|
type: 'goerli',
|
|
|
|
};
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
2021-04-15 20:01:46 +02:00
|
|
|
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 },
|
2021-03-09 20:49:27 +01:00
|
|
|
metamask: { provider: { ...providerStateForTestNetwork } },
|
2021-02-04 19:15:23 +01:00
|
|
|
}));
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(mockDistpatch.getCall(0).args).toStrictEqual([
|
2020-11-03 00:41:28 +01:00
|
|
|
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(mockDistpatch.getCall(1).args).toStrictEqual([
|
2020-11-03 00:41:28 +01:00
|
|
|
{
|
2019-07-04 21:18:12 +02:00
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: {
|
2021-03-09 20:49:27 +01:00
|
|
|
average: 0.0482,
|
2019-07-04 21:18:12 +02:00
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(mockDistpatch.getCall(2).args).toStrictEqual([
|
2020-11-03 00:41:28 +01:00
|
|
|
{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED },
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 21:18:12 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('setBasicGasEstimateData', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
expect(setBasicGasEstimateData('mockBasicEstimatData')).toStrictEqual({
|
2020-11-03 00:41:28 +01:00
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: 'mockBasicEstimatData',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('setCustomGasPrice', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
expect(setCustomGasPrice('mockCustomGasPrice')).toStrictEqual({
|
2020-11-03 00:41:28 +01:00
|
|
|
type: SET_CUSTOM_GAS_PRICE,
|
|
|
|
value: 'mockCustomGasPrice',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-09 21:25:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('setCustomGasLimit', () => {
|
|
|
|
it('should create the correct action', () => {
|
|
|
|
expect(setCustomGasLimit('mockCustomGasLimit')).toStrictEqual({
|
2020-11-03 00:41:28 +01:00
|
|
|
type: SET_CUSTOM_GAS_LIMIT,
|
|
|
|
value: 'mockCustomGasLimit',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|