From 2fa204a6936d2a538accb90b16e03e69ed6ef1fa Mon Sep 17 00:00:00 2001 From: apanizo Date: Tue, 17 Apr 2018 10:05:08 +0200 Subject: [PATCH] WA-230 Adding fetchBalance action. Included tests --- src/routes/safe/store/actions/fetchBalance.js | 4 +- src/routes/safe/store/test/balance.reducer.js | 54 +++++++++++++++++++ src/routes/safe/store/test/safe.spec.js | 2 + src/store/index.js | 2 + src/wallets/getWeb3.js | 2 +- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/routes/safe/store/test/balance.reducer.js diff --git a/src/routes/safe/store/actions/fetchBalance.js b/src/routes/safe/store/actions/fetchBalance.js index d542b643..430682d8 100644 --- a/src/routes/safe/store/actions/fetchBalance.js +++ b/src/routes/safe/store/actions/fetchBalance.js @@ -5,7 +5,7 @@ import { type GlobalState } from '~/store/index' import addBalance from './addBalance' export default (safeAddress: string) => async (dispatch: ReduxDispatch) => { - const balance: string = await getBalanceInEtherOf(safeAddress) + const balance = await getBalanceInEtherOf(safeAddress) - dispatch(addBalance(safeAddress, balance)) + return dispatch(addBalance(safeAddress, balance)) } diff --git a/src/routes/safe/store/test/balance.reducer.js b/src/routes/safe/store/test/balance.reducer.js new file mode 100644 index 00000000..f54da801 --- /dev/null +++ b/src/routes/safe/store/test/balance.reducer.js @@ -0,0 +1,54 @@ +// @flow +import { BALANCE_REDUCER_ID } from '~/routes/safe/store/reducer/balances' +import fetchBalance from '~/routes/safe/store/actions/fetchBalance' +import { aNewStore } from '~/store' +import { getWeb3 } from '~/wallets/getWeb3' +import { promisify } from '~/utils/promisify' +import { aDeployedSafe } from './builder/deployedSafe.builder' + +const addOneEtherTo = async (address: string) => { + const web3 = getWeb3() + const accounts = await promisify(cb => web3.eth.getAccounts(cb)) + const txData = { from: accounts[0], to: address, value: web3.toWei('1', 'ether') } + return promisify(cb => web3.eth.sendTransaction(txData, cb)) +} + +const balanceReducerTests = () => { + describe('Safe Actions[fetchBalance]', () => { + let store + beforeEach(async () => { + store = aNewStore() + }) + + it('reducer should return 0 to just deployed safe', async () => { + // GIVEN + const safeTx = await aDeployedSafe(store) + const address = safeTx.contractAddress + + // WHEN + await store.dispatch(fetchBalance(address)) + + // THEN + const balances = store.getState()[BALANCE_REDUCER_ID] + expect(balances).not.toBe(undefined) + expect(balances.get(address)).toBe('0') + }) + + it('reducer should return 1 ETH as funds to safe with 1 ETH', async () => { + // GIVEN + const safeTx = await aDeployedSafe(store) + const address = safeTx.contractAddress + + // WHEN + await addOneEtherTo(address) + await store.dispatch(fetchBalance(address)) + + // THEN + const balances = store.getState()[BALANCE_REDUCER_ID] + expect(balances).not.toBe(undefined) + expect(balances.get(address)).toBe('1') + }) + }) +} + +export default balanceReducerTests diff --git a/src/routes/safe/store/test/safe.spec.js b/src/routes/safe/store/test/safe.spec.js index 80febf06..df6e0259 100644 --- a/src/routes/safe/store/test/safe.spec.js +++ b/src/routes/safe/store/test/safe.spec.js @@ -1,10 +1,12 @@ // @flow +import balanceReducerTests from './balance.reducer' import safeReducerTests from './safe.reducer' import safeSelectorTests from './safe.selector' describe('Safe Test suite', () => { // ACTIONS AND REDUCERS safeReducerTests() + balanceReducerTests() // SAFE SELECTOR safeSelectorTests() diff --git a/src/store/index.js b/src/store/index.js index 5bf6ace5..7538a26d 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -32,3 +32,5 @@ const reducers: Reducer = combineReducers({ const initialState = { [SAFE_REDUCER_ID]: calculateInitialState() } export const store: Store = createStore(reducers, initialState, finalCreateStore) + +export const aNewStore = (): Store => createStore(reducers, initialState, finalCreateStore) diff --git a/src/wallets/getWeb3.js b/src/wallets/getWeb3.js index 775f546e..aa57987b 100644 --- a/src/wallets/getWeb3.js +++ b/src/wallets/getWeb3.js @@ -43,7 +43,7 @@ export const getProviderInfo: Function = async (): Promise => { } } -export const getBalanceInEtherOf = async (safeAddress: string): Promise => { +export const getBalanceInEtherOf = async (safeAddress: string) => { const funds: BigNumber = await promisify(cb => web3.eth.getBalance(safeAddress, cb)) if (!funds) { return '0'