2017-11-30 04:07:16 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
2018-01-02 18:04:50 +00:00
|
|
|
import { loadBityRatesSucceededSwap, loadShapeshiftRatesSucceededSwap } from 'actions/swap';
|
2017-11-30 04:07:16 +00:00
|
|
|
import { getAllRates } from 'api/bity';
|
|
|
|
import { delay } from 'redux-saga';
|
2018-01-02 18:04:50 +00:00
|
|
|
import { call, cancel, fork, put, race, take, takeLatest } from 'redux-saga/effects';
|
2017-11-30 04:07:16 +00:00
|
|
|
import { createMockTask } from 'redux-saga/utils';
|
2018-01-02 18:04:50 +00:00
|
|
|
import {
|
|
|
|
loadBityRates,
|
|
|
|
handleBityRates,
|
|
|
|
getBityRatesSaga,
|
|
|
|
loadShapeshiftRates,
|
|
|
|
getShapeShiftRatesSaga,
|
|
|
|
SHAPESHIFT_TIMEOUT,
|
|
|
|
handleShapeShiftRates
|
|
|
|
} from 'sagas/swap/rates';
|
|
|
|
import shapeshift from 'api/shapeshift';
|
|
|
|
import { TypeKeys } from 'actions/swap/constants';
|
2017-11-30 04:07:16 +00:00
|
|
|
|
|
|
|
describe('loadBityRates*', () => {
|
|
|
|
const gen1 = loadBityRates();
|
|
|
|
const gen2 = loadBityRates();
|
2017-12-11 17:44:53 +00:00
|
|
|
const apiResponse = {
|
|
|
|
BTCETH: {
|
|
|
|
id: 'BTCETH',
|
|
|
|
options: [{ id: 'BTC' }, { id: 'ETH' }],
|
|
|
|
rate: 23.27855114
|
|
|
|
},
|
|
|
|
ETHBTC: {
|
|
|
|
id: 'ETHBTC',
|
|
|
|
options: [{ id: 'ETH' }, { id: 'BTC' }],
|
|
|
|
rate: 0.042958
|
|
|
|
}
|
2017-11-30 04:07:16 +00:00
|
|
|
};
|
|
|
|
let random;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
random = Math.random;
|
|
|
|
Math.random = () => 0.001;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
Math.random = random;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call getAllRates', () => {
|
|
|
|
expect(gen1.next().value).toEqual(call(getAllRates));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should put loadBityRatesSucceededSwap', () => {
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(gen1.next(apiResponse).value).toEqual(put(loadBityRatesSucceededSwap(apiResponse)));
|
2017-11-30 04:07:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call delay for 5 seconds', () => {
|
|
|
|
expect(gen1.next().value).toEqual(call(delay, 30000));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle an exception', () => {
|
|
|
|
const err = { message: 'error' };
|
|
|
|
gen2.next();
|
2017-12-11 17:44:53 +00:00
|
|
|
expect((gen2 as any).throw(err).value).toEqual(put(showNotification('danger', err.message)));
|
2017-11-30 04:07:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
describe('loadShapeshiftRates*', () => {
|
|
|
|
const gen1 = loadShapeshiftRates();
|
|
|
|
const gen2 = loadShapeshiftRates();
|
|
|
|
|
|
|
|
const apiResponse = {
|
|
|
|
['1SSTANT']: {
|
|
|
|
id: '1STANT',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
id: '1ST',
|
|
|
|
status: 'available',
|
|
|
|
image: 'https://shapeshift.io/images/coins/firstblood.png',
|
|
|
|
name: 'FirstBlood'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'ANT',
|
|
|
|
status: 'available',
|
|
|
|
image: 'https://shapeshift.io/images/coins/aragon.png',
|
|
|
|
name: 'Aragon'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
rate: '0.24707537',
|
|
|
|
limit: 5908.29166225,
|
|
|
|
min: 7.86382979
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let random;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
random = Math.random;
|
|
|
|
Math.random = () => 0.001;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
Math.random = random;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should race shapeshift.getAllRates', () => {
|
|
|
|
expect(gen1.next().value).toEqual(
|
|
|
|
race({
|
|
|
|
tokens: call(shapeshift.getAllRates),
|
|
|
|
timeout: call(delay, SHAPESHIFT_TIMEOUT)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should put loadShapeshiftRatesSucceededSwap', () => {
|
|
|
|
expect(gen1.next({ tokens: apiResponse }).value).toEqual(
|
|
|
|
put(loadShapeshiftRatesSucceededSwap(apiResponse))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call delay for 30 seconds', () => {
|
|
|
|
expect(gen1.next().value).toEqual(call(delay, 30000));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle an exception', () => {
|
|
|
|
const err = 'error';
|
|
|
|
gen2.next();
|
|
|
|
expect((gen2 as any).throw(err).value).toEqual(
|
|
|
|
put(showNotification('danger', `Error loading ShapeShift tokens - ${err}`))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
describe('handleBityRates*', () => {
|
|
|
|
const gen = handleBityRates();
|
|
|
|
const mockTask = createMockTask();
|
|
|
|
|
|
|
|
it('should fork loadBityRates', () => {
|
|
|
|
expect(gen.next().value).toEqual(fork(loadBityRates));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should take SWAP_STOP_LOAD_BITY_RATES', () => {
|
2018-01-02 18:04:50 +00:00
|
|
|
expect(gen.next(mockTask).value).toEqual(take(TypeKeys.SWAP_STOP_LOAD_BITY_RATES));
|
2017-11-30 04:07:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should cancel loadBityRatesTask', () => {
|
|
|
|
expect(gen.next().value).toEqual(cancel(mockTask));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be done', () => {
|
|
|
|
expect(gen.next().done).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
describe('handleShapeshiftRates*', () => {
|
|
|
|
const gen = handleShapeShiftRates();
|
|
|
|
const mockTask = createMockTask();
|
|
|
|
|
|
|
|
it('should fork loadShapeshiftRates', () => {
|
|
|
|
expect(gen.next().value).toEqual(fork(loadShapeshiftRates));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should take SWAP_STOP_LOAD_BITY_RATES', () => {
|
|
|
|
expect(gen.next(mockTask).value).toEqual(take(TypeKeys.SWAP_STOP_LOAD_SHAPESHIFT_RATES));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should cancel loadShapeShiftRatesTask', () => {
|
|
|
|
expect(gen.next().value).toEqual(cancel(mockTask));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be done', () => {
|
|
|
|
expect(gen.next().done).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
describe('getBityRatesSaga*', () => {
|
|
|
|
const gen = getBityRatesSaga();
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
it('should takeLatest SWAP_LOAD_BITY_RATES_REQUESTED', () => {
|
|
|
|
expect(gen.next().value).toEqual(
|
|
|
|
takeLatest(TypeKeys.SWAP_LOAD_BITY_RATES_REQUESTED, handleBityRates)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getShapeshiftRatesSaga*', () => {
|
|
|
|
const gen = getShapeShiftRatesSaga();
|
|
|
|
|
|
|
|
it('should takeLatest SWAP_LOAD_BITY_RATES_REQUESTED', () => {
|
|
|
|
expect(gen.next().value).toEqual(
|
|
|
|
takeLatest(TypeKeys.SWAP_LOAD_SHAPESHIFT_RATES_REQUESTED, handleShapeShiftRates)
|
|
|
|
);
|
2017-11-30 04:07:16 +00:00
|
|
|
});
|
|
|
|
});
|