1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 04:46:10 +01:00
metamask-extension/ui/ducks/gas/gas-duck.test.js

237 lines
6.8 KiB
JavaScript
Raw Normal View History

import nock from 'nock';
import sinon from 'sinon';
import BN from 'bn.js';
2018-09-09 21:25:54 +02:00
import GasReducer, {
setBasicEstimateStatus,
2018-09-09 21:25:54 +02:00
setBasicGasEstimateData,
setCustomGasPrice,
setCustomGasLimit,
fetchBasicGasEstimates,
} from './gas.duck';
2018-09-09 21:25:54 +02:00
jest.mock('../../helpers/utils/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;
global.Date.now = () => 2000000;
});
afterEach(() => {
sinon.restore();
global.Date.now = tempDateNow;
});
2018-09-09 21:25:54 +02:00
const mockState = {
mockProp: 123,
};
2018-09-09 21:25:54 +02:00
const initState = {
customData: {
price: null,
limit: null,
2018-09-09 21:25:54 +02:00
},
basicEstimates: {
average: null,
fast: null,
safeLow: null,
},
basicEstimateStatus: 'LOADING',
estimateSource: '',
};
const providerState = {
chainId: '0x1',
nickname: '',
rpcPrefs: {},
rpcUrl: '',
ticker: 'ETH',
type: 'mainnet',
};
const BASIC_GAS_ESTIMATE_STATUS = 'metamask/gas/BASIC_GAS_ESTIMATE_STATUS';
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';
const SET_ESTIMATE_SOURCE = 'metamask/gas/SET_ESTIMATE_SOURCE';
2018-09-09 21:25:54 +02:00
describe('GasReducer()', () => {
it('should initialize state', () => {
expect(GasReducer(undefined, {})).toStrictEqual(initState);
});
2018-09-09 21:25:54 +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',
}),
).toStrictEqual(mockState);
});
2018-09-09 21:25:54 +02:00
it('should set basicEstimateStatus to LOADING when receiving a BASIC_GAS_ESTIMATE_STATUS action with value LOADING', () => {
expect(
GasReducer(mockState, {
type: BASIC_GAS_ESTIMATE_STATUS,
value: 'LOADING',
}),
).toStrictEqual({ basicEstimateStatus: 'LOADING', ...mockState });
});
2018-09-09 21:25:54 +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' },
}),
).toStrictEqual({
basicEstimates: { someProp: 'someData123' },
...mockState,
});
});
2018-09-09 21:25:54 +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,
}),
).toStrictEqual({ customData: { price: 4321 }, ...mockState });
});
2018-09-09 21:25:54 +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,
}),
).toStrictEqual({ customData: { limit: 9876 }, ...mockState });
});
});
2018-09-09 21:25:54 +02:00
it('should set estimateSource to Metaswaps when receiving a SET_ESTIMATE_SOURCE action with value Metaswaps', () => {
expect(
GasReducer(mockState, { type: SET_ESTIMATE_SOURCE, value: 'Metaswaps' }),
).toStrictEqual({ estimateSource: 'Metaswaps', ...mockState });
});
2018-09-09 21:25:54 +02:00
describe('basicEstimateStatus', () => {
it('should create the correct action', () => {
expect(setBasicEstimateStatus('LOADING')).toStrictEqual({
type: BASIC_GAS_ESTIMATE_STATUS,
value: 'LOADING',
});
});
});
2018-09-09 21:25:54 +02:00
describe('fetchBasicGasEstimates', () => {
it('should call fetch with the expected params', async () => {
const mockDistpatch = sinon.spy();
const windowFetchSpy = sinon.spy(window, 'fetch');
nock('https://api.metaswap.codefi.network')
.get('/gasPrices')
.reply(200, mockGasPriceApiResponse);
await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState },
metamask: { provider: { ...providerState } },
}));
expect(mockDistpatch.getCall(0).args).toStrictEqual([
{ type: 'metamask/gas/BASIC_GAS_ESTIMATE_STATUS', value: 'LOADING' },
]);
expect(
windowFetchSpy
2020-11-03 00:41:28 +01:00
.getCall(0)
.args[0].startsWith('https://api.metaswap.codefi.network/gasPrices'),
).toStrictEqual(true);
expect(mockDistpatch.getCall(2).args).toStrictEqual([
{ type: 'metamask/gas/SET_ESTIMATE_SOURCE', value: 'MetaSwaps' },
]);
expect(mockDistpatch.getCall(4).args).toStrictEqual([
{ type: 'metamask/gas/BASIC_GAS_ESTIMATE_STATUS', value: 'READY' },
]);
});
it('should call fetch with the expected params for test network', async () => {
global.eth = { gasPrice: sinon.fake.returns(new BN(48199313, 10)) };
const mockDistpatch = sinon.spy();
const providerStateForTestNetwork = {
chainId: '0x5',
nickname: '',
rpcPrefs: {},
rpcUrl: '',
ticker: 'ETH',
type: 'goerli',
};
2020-11-03 00:41:28 +01:00
await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 },
metamask: { provider: { ...providerStateForTestNetwork } },
}));
expect(mockDistpatch.getCall(0).args).toStrictEqual([
{ type: 'metamask/gas/BASIC_GAS_ESTIMATE_STATUS', value: 'LOADING' },
]);
expect(mockDistpatch.getCall(1).args).toStrictEqual([
{ type: 'metamask/gas/SET_ESTIMATE_SOURCE', value: 'eth_gasprice' },
]);
expect(mockDistpatch.getCall(2).args).toStrictEqual([
2020-11-03 00:41:28 +01:00
{
type: SET_BASIC_GAS_ESTIMATE_DATA,
value: {
average: 0.0482,
},
2020-11-03 00:41:28 +01:00
},
]);
expect(mockDistpatch.getCall(3).args).toStrictEqual([
{ type: 'metamask/gas/BASIC_GAS_ESTIMATE_STATUS', value: 'READY' },
]);
});
});
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',
});
});
});
2018-09-09 21:25:54 +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',
});
});
});
2018-09-09 21:25:54 +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',
});
});
});
});