WA-238 added grantedSelector and its test for displaying safe if user is owner

This commit is contained in:
apanizo 2018-05-22 10:22:57 +02:00
parent ec25ae276e
commit 26c4d8ffda
5 changed files with 103 additions and 8 deletions

View File

@ -6,7 +6,9 @@ import Layout from '~/routes/safe/component/Layout'
import selector, { type SelectorProps } from './selector' import selector, { type SelectorProps } from './selector'
import actions, { type Actions } from './actions' import actions, { type Actions } from './actions'
type Props = Actions & SelectorProps type Props = Actions & SelectorProps & {
granted: boolean,
}
class SafeView extends React.PureComponent<Props> { class SafeView extends React.PureComponent<Props> {
componentDidMount() { componentDidMount() {
@ -31,15 +33,17 @@ class SafeView extends React.PureComponent<Props> {
intervalId: IntervalID intervalId: IntervalID
render() { render() {
const { safe, provider, balance } = this.props const {
safe, provider, balance, granted,
} = this.props
return ( return (
<Page> <Page>
<Layout { granted && <Layout
balance={balance} balance={balance}
provider={provider} provider={provider}
safe={safe} safe={safe}
/> /> }
</Page> </Page>
) )
} }

View File

@ -1,7 +1,11 @@
// @flow // @flow
import { createStructuredSelector } from 'reselect' import { List } from 'immutable'
import { balanceSelector, safeSelector, type SafeSelectorProps } from '~/routes/safe/store/selectors' import { createSelector, createStructuredSelector, type Selector } from 'reselect'
import { providerNameSelector } from '~/wallets/store/selectors/index' import { balanceSelector, safeSelector, type RouterProps, type SafeSelectorProps } from '~/routes/safe/store/selectors'
import { providerNameSelector, userAccountSelector } from '~/wallets/store/selectors/index'
import { type Safe } from '~/routes/safe/store/model/safe'
import { type Owner } from '~/routes/safe/store/model/owner'
import { type GlobalState } from '~/store/index'
export type SelectorProps = { export type SelectorProps = {
safe: SafeSelectorProps, safe: SafeSelectorProps,
@ -9,8 +13,30 @@ export type SelectorProps = {
balance: string, balance: string,
} }
export const grantedSelector: Selector<GlobalState, RouterProps, boolean> = createSelector(
userAccountSelector,
safeSelector,
(userAccount: string, safe: Safe | typeof undefined): boolean => {
if (!safe) {
return false
}
if (!userAccount) {
return false
}
const owners: List<Owner> = safe.get('owners')
if (!owners) {
return false
}
return owners.find((owner: Owner) => owner.get('address') === userAccount) !== undefined
},
)
export default createStructuredSelector({ export default createStructuredSelector({
safe: safeSelector, safe: safeSelector,
provider: providerNameSelector, provider: providerNameSelector,
balance: balanceSelector, balance: balanceSelector,
granted: grantedSelector,
}) })

View File

@ -8,7 +8,7 @@ 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' import { BALANCE_REDUCER_ID } from '~/routes/safe/store/reducer/balances'
type RouterProps = { export type RouterProps = {
match: Match, match: Match,
} }

View File

@ -0,0 +1,61 @@
// @flow
import { Map } from 'immutable'
import { type Match } from 'react-router-dom'
import { SAFE_REDUCER_ID } from '~/routes/safe/store/reducer/safe'
import { type Safe } from '~/routes/safe/store/model/safe'
import { SafeFactory } from '~/routes/safe/store/test/builder/safe.builder'
import { buildMathPropsFrom } from '~/test/buildReactRouterProps'
import { getProviderInfo } from '~/wallets/getWeb3'
import { grantedSelector } from '~/routes/safe/container/selector'
import { makeProvider } from '~/wallets/store/model/provider'
const grantedSelectorTests = () => {
let provider
beforeEach(async () => {
provider = await getProviderInfo()
})
describe('Safe Selector[grantedSelector]', () => {
it('should be granted to operate when a safe when the user is owner', () => {
// GIVEN
let map: Map<string, Safe> = Map()
map = map.set('fooAddress', SafeFactory.oneOwnerSafe(provider.account))
const match: Match = buildMathPropsFrom('fooAddress')
const reduxStore = {
[SAFE_REDUCER_ID]: map,
providers: makeProvider(provider),
balances: undefined,
}
// WHEN
const granted = grantedSelector(reduxStore, { match })
// THEN
expect(granted).toBe(true)
})
it('should NOT be granted to operate with a Safe when the user is NOT owner', () => {
// GIVEN
let map: Map<string, Safe> = Map()
map = map.set('fooAddress', SafeFactory.oneOwnerSafe('inventedOwner'))
const match: Match = buildMathPropsFrom('fooAddress')
const reduxStore = {
[SAFE_REDUCER_ID]: map,
providers: makeProvider(provider),
balances: undefined,
}
// WHEN
const granted = grantedSelector(reduxStore, { match })
// THEN
expect(granted).toBe(false)
})
})
}
export default grantedSelectorTests

View File

@ -4,6 +4,7 @@ import safeReducerTests from './safe.reducer'
import dailyLimitReducerTests from './dailyLimit.reducer' import dailyLimitReducerTests from './dailyLimit.reducer'
import balanceSelectorTests from './balance.selector' import balanceSelectorTests from './balance.selector'
import safeSelectorTests from './safe.selector' import safeSelectorTests from './safe.selector'
import grantedSelectorTests from './granted.selector'
describe('Safe Test suite', () => { describe('Safe Test suite', () => {
// ACTIONS AND REDUCERS // ACTIONS AND REDUCERS
@ -16,4 +17,7 @@ describe('Safe Test suite', () => {
// BALANCE SELECTOR // BALANCE SELECTOR
balanceSelectorTests() balanceSelectorTests()
// GRANTED SELECTOR
grantedSelectorTests()
}) })