2018-02-16 19:01:39 +00:00
|
|
|
|
import { call, put, select } from 'redux-saga/effects';
|
|
|
|
|
import { cloneableGenerator } from 'redux-saga/utils';
|
2018-02-24 18:00:00 +00:00
|
|
|
|
|
2018-06-18 01:53:00 +00:00
|
|
|
|
import { gasPriceDefaults, gasEstimateCacheTime } from 'config';
|
|
|
|
|
import { fetchGasEstimates, GasEstimates } from 'api/gas';
|
|
|
|
|
import * as configMetaSelectors from 'features/config/meta/selectors';
|
|
|
|
|
import * as configNetworksStaticReducer from 'features/config/networks/static/reducer';
|
|
|
|
|
import * as configSelectors from 'features/config/selectors';
|
|
|
|
|
import * as actions from './actions';
|
|
|
|
|
import * as selectors from './selectors';
|
|
|
|
|
import * as sagas from './sagas';
|
|
|
|
|
|
|
|
|
|
const networkState = configNetworksStaticReducer.staticNetworksReducer(undefined, {} as any);
|
2018-02-24 18:00:00 +00:00
|
|
|
|
const network = networkState.ETH;
|
|
|
|
|
const nonEstimateNetwork = networkState.ETC;
|
2018-02-16 19:01:39 +00:00
|
|
|
|
|
|
|
|
|
describe('fetchEstimates*', () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
|
const gen = cloneableGenerator(sagas.fetchEstimates)();
|
2018-02-16 19:01:39 +00:00
|
|
|
|
const offline = false;
|
|
|
|
|
const oldEstimates: GasEstimates = {
|
|
|
|
|
safeLow: 1,
|
|
|
|
|
standard: 1,
|
|
|
|
|
fast: 4,
|
|
|
|
|
fastest: 20,
|
|
|
|
|
time: Date.now() - gasEstimateCacheTime - 1000,
|
2018-02-24 18:00:00 +00:00
|
|
|
|
chainId: network.chainId,
|
2018-02-16 19:01:39 +00:00
|
|
|
|
isDefault: false
|
|
|
|
|
};
|
2018-02-24 18:00:00 +00:00
|
|
|
|
const newTimeEstimates: GasEstimates = {
|
2018-02-16 19:01:39 +00:00
|
|
|
|
safeLow: 2,
|
|
|
|
|
standard: 2,
|
|
|
|
|
fast: 8,
|
|
|
|
|
fastest: 80,
|
|
|
|
|
time: Date.now(),
|
2018-02-24 18:00:00 +00:00
|
|
|
|
chainId: network.chainId,
|
2018-02-16 19:01:39 +00:00
|
|
|
|
isDefault: false
|
|
|
|
|
};
|
2018-02-24 18:00:00 +00:00
|
|
|
|
const newChainIdEstimates: GasEstimates = {
|
|
|
|
|
...oldEstimates,
|
|
|
|
|
chainId: network.chainId + 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it('Should select getNetworkConfig', () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
|
expect(gen.next().value).toEqual(select(configSelectors.getNetworkConfig));
|
2018-02-24 18:00:00 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should use network default gas price settings if network shouldn’t estimate', () => {
|
|
|
|
|
const noEstimateGen = gen.clone();
|
|
|
|
|
expect(noEstimateGen.next(nonEstimateNetwork).value).toEqual(
|
2018-06-18 01:53:00 +00:00
|
|
|
|
call(sagas.setDefaultEstimates, nonEstimateNetwork)
|
2018-02-24 18:00:00 +00:00
|
|
|
|
);
|
|
|
|
|
expect(noEstimateGen.next().done).toBeTruthy();
|
|
|
|
|
});
|
2018-02-16 19:01:39 +00:00
|
|
|
|
|
|
|
|
|
it('Should select getOffline', () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
|
expect(gen.next(network).value).toEqual(select(configMetaSelectors.getOffline));
|
2018-02-16 19:01:39 +00:00
|
|
|
|
});
|
|
|
|
|
|
2018-02-24 18:00:00 +00:00
|
|
|
|
it('Should use network default gas price settings if offline', () => {
|
2018-02-16 19:01:39 +00:00
|
|
|
|
const offlineGen = gen.clone();
|
2018-06-18 01:53:00 +00:00
|
|
|
|
expect(offlineGen.next(true).value).toEqual(call(sagas.setDefaultEstimates, network));
|
2018-02-16 19:01:39 +00:00
|
|
|
|
expect(offlineGen.next().done).toBeTruthy();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should select getEstimates', () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
|
expect(gen.next(offline).value).toEqual(select(selectors.getEstimates));
|
2018-02-16 19:01:39 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should use cached estimates if they’re recent', () => {
|
|
|
|
|
const cachedGen = gen.clone();
|
|
|
|
|
const cacheEstimate = {
|
|
|
|
|
...oldEstimates,
|
|
|
|
|
time: Date.now() - gasEstimateCacheTime + 1000
|
|
|
|
|
};
|
2018-06-18 01:53:00 +00:00
|
|
|
|
expect(cachedGen.next(cacheEstimate).value).toEqual(
|
|
|
|
|
put(actions.setGasEstimates(cacheEstimate))
|
|
|
|
|
);
|
2018-02-16 19:01:39 +00:00
|
|
|
|
expect(cachedGen.next().done).toBeTruthy();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should fetch new estimates', () => {
|
|
|
|
|
expect(gen.next(oldEstimates).value).toEqual(call(fetchGasEstimates));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should use default estimates if request fails', () => {
|
|
|
|
|
const failedReqGen = gen.clone();
|
|
|
|
|
// Not sure why, but typescript seems to think throw might be missing.
|
|
|
|
|
if (failedReqGen.throw) {
|
2018-06-18 01:53:00 +00:00
|
|
|
|
expect(failedReqGen.throw('test').value).toEqual(call(sagas.setDefaultEstimates, network));
|
2018-02-16 19:01:39 +00:00
|
|
|
|
expect(failedReqGen.next().done).toBeTruthy();
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('SagaIterator didn’t have throw');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-24 18:00:00 +00:00
|
|
|
|
it('Should use new estimates if chainId changed, even if time is similar', () => {
|
|
|
|
|
const newChainGen = gen.clone();
|
|
|
|
|
expect(newChainGen.next(newChainIdEstimates).value).toEqual(
|
2018-06-18 01:53:00 +00:00
|
|
|
|
put(actions.setGasEstimates(newChainIdEstimates))
|
2018-02-24 18:00:00 +00:00
|
|
|
|
);
|
|
|
|
|
expect(newChainGen.next().done).toBeTruthy();
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-16 19:01:39 +00:00
|
|
|
|
it('Should use fetched estimates', () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
|
expect(gen.next(newTimeEstimates).value).toEqual(
|
|
|
|
|
put(actions.setGasEstimates(newTimeEstimates))
|
|
|
|
|
);
|
2018-02-16 19:01:39 +00:00
|
|
|
|
expect(gen.next().done).toBeTruthy();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('setDefaultEstimates*', () => {
|
2018-02-24 18:00:00 +00:00
|
|
|
|
const time = Date.now();
|
2018-02-16 19:01:39 +00:00
|
|
|
|
|
|
|
|
|
it('Should put setGasEstimates with config defaults', () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
|
const gen = sagas.setDefaultEstimates(network);
|
2018-02-24 18:00:00 +00:00
|
|
|
|
gen.next();
|
|
|
|
|
expect(gen.next(time).value).toEqual(
|
|
|
|
|
put(
|
2018-06-18 01:53:00 +00:00
|
|
|
|
actions.setGasEstimates({
|
2018-02-24 18:00:00 +00:00
|
|
|
|
safeLow: network.gasPriceSettings.min,
|
|
|
|
|
standard: network.gasPriceSettings.initial,
|
|
|
|
|
fast: network.gasPriceSettings.initial,
|
|
|
|
|
fastest: network.gasPriceSettings.max,
|
|
|
|
|
chainId: network.chainId,
|
|
|
|
|
isDefault: true,
|
|
|
|
|
time
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should use config defaults if network has no defaults', () => {
|
|
|
|
|
const customNetwork = {
|
|
|
|
|
isCustom: true as true,
|
2018-05-29 14:51:42 +00:00
|
|
|
|
id: '123',
|
|
|
|
|
name: 'Custom',
|
2018-02-24 18:00:00 +00:00
|
|
|
|
unit: 'CST',
|
|
|
|
|
chainId: 123,
|
|
|
|
|
dPathFormats: null
|
|
|
|
|
};
|
2018-06-18 01:53:00 +00:00
|
|
|
|
const gen = sagas.setDefaultEstimates(customNetwork);
|
2018-02-24 18:00:00 +00:00
|
|
|
|
|
2018-02-16 19:01:39 +00:00
|
|
|
|
gen.next();
|
|
|
|
|
expect(gen.next(time).value).toEqual(
|
|
|
|
|
put(
|
2018-06-18 01:53:00 +00:00
|
|
|
|
actions.setGasEstimates({
|
2018-02-24 18:00:00 +00:00
|
|
|
|
safeLow: gasPriceDefaults.min,
|
|
|
|
|
standard: gasPriceDefaults.initial,
|
|
|
|
|
fast: gasPriceDefaults.initial,
|
|
|
|
|
fastest: gasPriceDefaults.max,
|
|
|
|
|
chainId: customNetwork.chainId,
|
2018-02-16 19:01:39 +00:00
|
|
|
|
isDefault: true,
|
|
|
|
|
time
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|