WA-230 Adding fetchBalance action. Included tests
This commit is contained in:
parent
68c9c86287
commit
2fa204a693
|
@ -5,7 +5,7 @@ import { type GlobalState } from '~/store/index'
|
|||
import addBalance from './addBalance'
|
||||
|
||||
export default (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||
const balance: string = await getBalanceInEtherOf(safeAddress)
|
||||
const balance = await getBalanceInEtherOf(safeAddress)
|
||||
|
||||
dispatch(addBalance(safeAddress, balance))
|
||||
return dispatch(addBalance(safeAddress, balance))
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -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()
|
||||
|
|
|
@ -32,3 +32,5 @@ const reducers: Reducer<GlobalState> = combineReducers({
|
|||
const initialState = { [SAFE_REDUCER_ID]: calculateInitialState() }
|
||||
|
||||
export const store: Store<GlobalState> = createStore(reducers, initialState, finalCreateStore)
|
||||
|
||||
export const aNewStore = (): Store<GlobalState> => createStore(reducers, initialState, finalCreateStore)
|
||||
|
|
|
@ -43,7 +43,7 @@ export const getProviderInfo: Function = async (): Promise<ProviderProps> => {
|
|||
}
|
||||
}
|
||||
|
||||
export const getBalanceInEtherOf = async (safeAddress: string): Promise<string> => {
|
||||
export const getBalanceInEtherOf = async (safeAddress: string) => {
|
||||
const funds: BigNumber = await promisify(cb => web3.eth.getBalance(safeAddress, cb))
|
||||
if (!funds) {
|
||||
return '0'
|
||||
|
|
Loading…
Reference in New Issue