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

View File

@ -1,7 +1,11 @@
// @flow
import { createStructuredSelector } from 'reselect'
import { balanceSelector, safeSelector, type SafeSelectorProps } from '~/routes/safe/store/selectors'
import { providerNameSelector } from '~/wallets/store/selectors/index'
import { List } from 'immutable'
import { createSelector, createStructuredSelector, type Selector } from 'reselect'
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 = {
safe: SafeSelectorProps,
@ -9,8 +13,30 @@ export type SelectorProps = {
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({
safe: safeSelector,
provider: providerNameSelector,
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 { BALANCE_REDUCER_ID } from '~/routes/safe/store/reducer/balances'
type RouterProps = {
export type RouterProps = {
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 balanceSelectorTests from './balance.selector'
import safeSelectorTests from './safe.selector'
import grantedSelectorTests from './granted.selector'
describe('Safe Test suite', () => {
// ACTIONS AND REDUCERS
@ -16,4 +17,7 @@ describe('Safe Test suite', () => {
// BALANCE SELECTOR
balanceSelectorTests()
// GRANTED SELECTOR
grantedSelectorTests()
})