From 26c4d8ffda0fa7b6c4616d546119c7d69f8b9832 Mon Sep 17 00:00:00 2001 From: apanizo Date: Tue, 22 May 2018 10:22:57 +0200 Subject: [PATCH] WA-238 added grantedSelector and its test for displaying safe if user is owner --- src/routes/safe/container/index.jsx | 12 ++-- src/routes/safe/container/selector.js | 32 +++++++++- src/routes/safe/store/selectors/index.js | 2 +- .../safe/store/test/granted.selector.js | 61 +++++++++++++++++++ src/routes/safe/store/test/safe.spec.js | 4 ++ 5 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 src/routes/safe/store/test/granted.selector.js diff --git a/src/routes/safe/container/index.jsx b/src/routes/safe/container/index.jsx index 62edd438..7aebf94c 100644 --- a/src/routes/safe/container/index.jsx +++ b/src/routes/safe/container/index.jsx @@ -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 { componentDidMount() { @@ -31,15 +33,17 @@ class SafeView extends React.PureComponent { intervalId: IntervalID render() { - const { safe, provider, balance } = this.props + const { + safe, provider, balance, granted, + } = this.props return ( - + /> } ) } diff --git a/src/routes/safe/container/selector.js b/src/routes/safe/container/selector.js index 3ac175f5..63300566 100644 --- a/src/routes/safe/container/selector.js +++ b/src/routes/safe/container/selector.js @@ -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 = createSelector( + userAccountSelector, + safeSelector, + (userAccount: string, safe: Safe | typeof undefined): boolean => { + if (!safe) { + return false + } + + if (!userAccount) { + return false + } + + const owners: List = 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, }) diff --git a/src/routes/safe/store/selectors/index.js b/src/routes/safe/store/selectors/index.js index 1bcd96f2..e6bf40eb 100644 --- a/src/routes/safe/store/selectors/index.js +++ b/src/routes/safe/store/selectors/index.js @@ -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, } diff --git a/src/routes/safe/store/test/granted.selector.js b/src/routes/safe/store/test/granted.selector.js new file mode 100644 index 00000000..0f91cd8d --- /dev/null +++ b/src/routes/safe/store/test/granted.selector.js @@ -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 = 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 = 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 diff --git a/src/routes/safe/store/test/safe.spec.js b/src/routes/safe/store/test/safe.spec.js index 57521e6f..c8e45daa 100644 --- a/src/routes/safe/store/test/safe.spec.js +++ b/src/routes/safe/store/test/safe.spec.js @@ -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() })