WA-230 Adding get balance selector using rr match props. Included tests
This commit is contained in:
parent
2fa204a693
commit
01977f91cd
|
@ -6,6 +6,7 @@ import { type GlobalState } from '~/store/index'
|
|||
import { SAFE_PARAM_ADDRESS } from '~/routes/routes'
|
||||
import { type Safe } from '~/routes/safe/store/model/safe'
|
||||
import { safesMapSelector } from '~/routes/safeList/store/selectors'
|
||||
import { BALANCE_REDUCER_ID } from '~/routes/safe/store/reducer/balances'
|
||||
|
||||
type RouterProps = {
|
||||
match: Match,
|
||||
|
@ -13,6 +14,8 @@ type RouterProps = {
|
|||
|
||||
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 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({
|
||||
safe: safeSelector,
|
||||
})
|
||||
|
|
|
@ -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
|
|
@ -1,6 +1,7 @@
|
|||
// @flow
|
||||
import balanceReducerTests from './balance.reducer'
|
||||
import safeReducerTests from './safe.reducer'
|
||||
import balanceSelectorTests from './balance.selector'
|
||||
import safeSelectorTests from './safe.selector'
|
||||
|
||||
describe('Safe Test suite', () => {
|
||||
|
@ -10,4 +11,7 @@ describe('Safe Test suite', () => {
|
|||
|
||||
// SAFE SELECTOR
|
||||
safeSelectorTests()
|
||||
|
||||
// BALANCE SELECTOR
|
||||
balanceSelectorTests()
|
||||
})
|
||||
|
|
|
@ -33,4 +33,5 @@ const initialState = { [SAFE_REDUCER_ID]: calculateInitialState() }
|
|||
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue