WA-230 Adding get balance selector using rr match props. Included tests

This commit is contained in:
apanizo 2018-04-17 11:37:01 +02:00
parent 2fa204a693
commit 01977f91cd
4 changed files with 82 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import { type GlobalState } from '~/store/index'
import { SAFE_PARAM_ADDRESS } from '~/routes/routes' import { SAFE_PARAM_ADDRESS } from '~/routes/routes'
import { type Safe } from '~/routes/safe/store/model/safe' import { type Safe } from '~/routes/safe/store/model/safe'
import { safesMapSelector } from '~/routes/safeList/store/selectors' import { safesMapSelector } from '~/routes/safeList/store/selectors'
import { BALANCE_REDUCER_ID } from '~/routes/safe/store/reducer/balances'
type RouterProps = { type RouterProps = {
match: Match, match: Match,
@ -13,6 +14,8 @@ type RouterProps = {
const safeAddessSelector = (state: GlobalState, props: RouterProps) => props.match.params[SAFE_PARAM_ADDRESS] || '' const safeAddessSelector = (state: GlobalState, props: RouterProps) => props.match.params[SAFE_PARAM_ADDRESS] || ''
const balancesSelector = (state: GlobalState) => state[BALANCE_REDUCER_ID]
export type SafeSelectorProps = Safe | typeof undefined export type SafeSelectorProps = Safe | typeof undefined
export const safeSelector: Selector<GlobalState, RouterProps, SafeSelectorProps> = createSelector( export const safeSelector: Selector<GlobalState, RouterProps, SafeSelectorProps> = createSelector(
@ -27,6 +30,18 @@ export const safeSelector: Selector<GlobalState, RouterProps, SafeSelectorProps>
}, },
) )
export const balanceSelector: Selector<GlobalState, RouterProps, string> = createSelector(
balancesSelector,
safeAddessSelector,
(balances: Map<string, string>, address: string) => {
if (!address) {
return '0'
}
return balances.get(address) || '0'
},
)
export default createStructuredSelector({ export default createStructuredSelector({
safe: safeSelector, safe: safeSelector,
}) })

View File

@ -0,0 +1,61 @@
// @flow
import { type Match } from 'react-router-dom'
import addBalance from '~/routes/safe/store/actions/addBalance'
import { aNewStore } from '~/store'
import { balanceSelector } from '../selectors'
const buildMathPropsFrom = (address): Match => ({
params: {
address,
},
isExact: true,
path: '',
url: '',
})
const balanceSelectorTests = () => {
describe('Safe Selector[balanceSelector]', () => {
it('should return 0 when safe address is not found', () => {
// GIVEN
const safeAddress = 'foo'
const match = buildMathPropsFrom(safeAddress)
const store = aNewStore()
// WHEN
const balance = balanceSelector(store.getState(), { match })
// THEN
expect(balance).toBe('0')
})
it('should return 0 when safe has no funds', async () => {
// GIVEN
const safeAddress = 'foo'
const match = buildMathPropsFrom(safeAddress)
const store = aNewStore()
// WHEN
await store.dispatch(addBalance('bar', '1'))
const balance = balanceSelector(store.getState(), { match })
// THEN
expect(balance).toBe('0')
})
it('should return safe funds', async () => {
// GIVEN
const safeAddress = 'foo'
const match = buildMathPropsFrom(safeAddress)
const store = aNewStore()
// WHEN
await store.dispatch(addBalance(safeAddress, '1'))
const balance = balanceSelector(store.getState(), { match })
// THEN
expect(balance).toBe('1')
})
})
}
export default balanceSelectorTests

View File

@ -1,6 +1,7 @@
// @flow // @flow
import balanceReducerTests from './balance.reducer' import balanceReducerTests from './balance.reducer'
import safeReducerTests from './safe.reducer' import safeReducerTests from './safe.reducer'
import balanceSelectorTests from './balance.selector'
import safeSelectorTests from './safe.selector' import safeSelectorTests from './safe.selector'
describe('Safe Test suite', () => { describe('Safe Test suite', () => {
@ -10,4 +11,7 @@ describe('Safe Test suite', () => {
// SAFE SELECTOR // SAFE SELECTOR
safeSelectorTests() safeSelectorTests()
// BALANCE SELECTOR
balanceSelectorTests()
}) })

View File

@ -33,4 +33,5 @@ const initialState = { [SAFE_REDUCER_ID]: calculateInitialState() }
export const store: Store<GlobalState> = createStore(reducers, initialState, finalCreateStore) export const store: Store<GlobalState> = createStore(reducers, initialState, finalCreateStore)
export const aNewStore = (): Store<GlobalState> => createStore(reducers, initialState, finalCreateStore) export const aNewStore = (localState?: Object): Store<GlobalState> =>
createStore(reducers, localState, finalCreateStore)