Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #740 from LiskHQ/293-refactor-price-chart-api-call…
…-to-redux-action Refactor Price chart API call to redux action - Closes #293
- Loading branch information
Showing
8 changed files
with
124 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import actionTypes from '../constants/actions'; | ||
import liskServiceApi from '../../src/utils/api/liskService'; | ||
|
||
export const addDataToCurrencyGraph = data => ({ | ||
type: actionTypes.addDataToCurrencyGraph, | ||
data, | ||
}); | ||
|
||
export const addErrorToCurrencyGraph = data => ({ | ||
type: actionTypes.addErrorToCurrencyGraph, | ||
data, | ||
}); | ||
|
||
export const clearDataOfCurrencyGraph = () => ({ | ||
type: actionTypes.clearDataOfCurrencyGraph, | ||
}); | ||
|
||
export const getCurrencyGraphData = step => (dispatch) => { | ||
dispatch(clearDataOfCurrencyGraph()); | ||
liskServiceApi.getCurrencyGraphData(step).then((response) => { | ||
dispatch(addDataToCurrencyGraph({ response, step })); | ||
}).catch((error) => { | ||
dispatch(addErrorToCurrencyGraph(error)); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,58 @@ | ||
import thunk from 'redux-thunk'; | ||
import { expect } from 'chai'; | ||
import { Line as LineChart } from 'react-chartjs-2'; | ||
import sinon from 'sinon'; | ||
import React from 'react'; | ||
|
||
import { mountWithContext } from './../../../test/utils/mountHelpers'; | ||
import { prepareStore } from '../../../test/utils/applicationInit'; | ||
import liskServiceApi from '../../utils/api/liskService'; | ||
import { mountWithContext } from '../../../test/utils/mountHelpers'; | ||
|
||
import liskServiceReducer from '../../store/reducers/liskService'; | ||
import CurrencyGraph from './currencyGraph'; | ||
|
||
describe('CurrencyGraph', () => { | ||
let explorereApiMock; | ||
let liskServiceApiMock; | ||
let wrapper; | ||
let store; | ||
|
||
const prices = [ | ||
{ high: 0.003223542, date: '2018-02-01 13:00:00' }, | ||
{ high: 0.012344282, date: '2018-02-02 13:00:00' }, | ||
]; | ||
|
||
beforeEach(() => { | ||
explorereApiMock = sinon.stub(liskServiceApi, 'getCurrencyGrapData').returnsPromise(); | ||
wrapper = mountWithContext(<CurrencyGraph/>, {}); | ||
liskServiceApiMock = sinon.stub(liskServiceApi, 'getCurrencyGraphData').returnsPromise(); | ||
store = prepareStore({ | ||
liskService: liskServiceReducer, | ||
}, [thunk]); | ||
|
||
wrapper = mountWithContext(<CurrencyGraph store={store}/>, {}); | ||
}); | ||
|
||
afterEach(() => { | ||
explorereApiMock.restore(); | ||
liskServiceApiMock.restore(); | ||
}); | ||
|
||
it('shold render LineChart when explorer api resolves candle data', () => { | ||
const candles = [ | ||
{ high: 0.003223542, date: '2018-02-01 13:00:00' }, | ||
{ high: 0.012344282, date: '2018-02-01 14:00:00' }, | ||
]; | ||
|
||
it('should render LineChart when explorer api resolves candle data', () => { | ||
expect(wrapper.find('.chart-wrapper').first()).to.be.present(); | ||
expect(wrapper.find(LineChart)).not.to.be.present(); | ||
explorereApiMock.resolves({ candles }); | ||
liskServiceApiMock.resolves({ prices }); | ||
wrapper.update(); | ||
expect(wrapper.find(LineChart)).to.be.present(); | ||
}); | ||
|
||
it('shold show and error message when explorer api call fails', () => { | ||
it('should show and error message when explorer api call fails', () => { | ||
expect(wrapper.find(LineChart)).not.to.be.present(); | ||
explorereApiMock.rejects({ }); | ||
liskServiceApiMock.rejects({ }); | ||
expect(wrapper.find(LineChart)).not.to.be.present(); | ||
expect(wrapper.text()).to.contain('Price data currently not available'); | ||
}); | ||
|
||
it('should allow to change step', () => { | ||
const candles = [ | ||
{ high: 0.003223542, date: '2018-02-01 13:00:00' }, | ||
{ high: 0.012344282, date: '2018-02-02 13:00:00' }, | ||
]; | ||
|
||
wrapper.find('.step').at(1).simulate('click'); | ||
expect(wrapper.find(LineChart)).not.to.be.present(); | ||
explorereApiMock.resolves({ candles }); | ||
liskServiceApiMock.resolves({ prices }); | ||
wrapper.update(); | ||
expect(wrapper.find(LineChart)).to.be.present(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import BigNumber from 'bignumber.js'; | ||
import actionTypes from '../../constants/actions'; | ||
|
||
/** | ||
* Converts candles response into x,y coordinate positions for the graph | ||
* @param {Array} candlesResponse array of candles to show | ||
* @param {Object} target step configuration | ||
*/ | ||
const getPricesForGraph = ({ prices = [], step }) => ({ | ||
data: prices.slice(Math.max(prices.length - step.length, 1)) | ||
.map(c => ({ | ||
x: new Date(c.date), | ||
y: new BigNumber(c.high).plus(new BigNumber(c.low)).dividedBy(2), | ||
})), | ||
}); | ||
|
||
const liskService = (state = [], action) => { | ||
switch (action.type) { | ||
case actionTypes.clearDataOfCurrencyGraph: | ||
return { | ||
...state, | ||
prices: undefined, | ||
graphError: undefined, | ||
}; | ||
case actionTypes.addDataToCurrencyGraph: | ||
return { | ||
...state, | ||
prices: getPricesForGraph({ | ||
prices: action.data.response.candles, | ||
step: action.data.step, | ||
}), | ||
step: action.data.step, | ||
graphError: undefined, | ||
}; | ||
case actionTypes.addErrorToCurrencyGraph: | ||
return { | ||
...state, | ||
graphError: action.data, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default liskService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters