From dd606940fa0b07a4a988a7515688ce98c75c1b7a Mon Sep 17 00:00:00 2001 From: apanizo Date: Tue, 3 Jul 2018 13:23:26 +0200 Subject: [PATCH] WA-232 Added move ERC20 Tokens tests --- src/test/safe.dom.tokens.test.js | 70 +++++++++++++++++++ src/test/utils/tokenMovements.js | 2 +- .../utils/transactions/moveTokens.helper.js | 7 +- 3 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/test/safe.dom.tokens.test.js diff --git a/src/test/safe.dom.tokens.test.js b/src/test/safe.dom.tokens.test.js new file mode 100644 index 00000000..f6a7676e --- /dev/null +++ b/src/test/safe.dom.tokens.test.js @@ -0,0 +1,70 @@ +// @flow + +import TestUtils from 'react-dom/test-utils' +import * as fetchBalancesAction from '~/routes/safe/store/actions/fetchBalances' +import { aNewStore } from '~/store' +import { aMinedSafe } from '~/test/builder/safe.redux.builder' +import { addTknTo, getTokenContract } from '~/test/utils/tokenMovements' +import { EXPAND_BALANCE_INDEX, travelToSafe } from '~/test/builder/safe.dom.utils' +import { promisify } from '~/utils/promisify' +import { getWeb3 } from '~/wallets/getWeb3' +import { sendMoveTokensForm, dispatchTknBalance } from '~/test/utils/transactions/moveTokens.helper' +import { sleep } from '~/utils/timer' + +describe('DOM > Feature > SAFE ERC20 TOKENS', () => { + let store + let safeAddress: string + let accounts + beforeEach(async () => { + store = aNewStore() + safeAddress = await aMinedSafe(store) + accounts = await promisify(cb => getWeb3().eth.getAccounts(cb)) + }) + + it('sends ERC20 tokens', async () => { + // GIVEN + const numTokens = 100 + const tokenAddress = await addTknTo(safeAddress, numTokens) + await dispatchTknBalance(store, tokenAddress, safeAddress) + // const StandardToken = await fetchBalancesAction.getStandardTokenContract() + // const myToken = await StandardToken.at(tokenAddress) + // console.log(await myToken.allowance(safeAddress, accounts[2])) + // console.log(await myToken.balanceOf(safeAddress)) + + // WHEN + const SafeDom = await travelToSafe(store, safeAddress) + await sleep(800) + // $FlowFixMe + const buttons = TestUtils.scryRenderedDOMComponentsWithTag(SafeDom, 'button') + const expandBalance = buttons[EXPAND_BALANCE_INDEX] + const receiver = accounts[2] + await sendMoveTokensForm(SafeDom, expandBalance, 20, accounts[2]) + + // THEN + const safeFunds = await fetchBalancesAction.calculateBalanceOf(tokenAddress, safeAddress) + expect(Number(safeFunds)).toBe(80) + const receiverFunds = await fetchBalancesAction.calculateBalanceOf(tokenAddress, receiver) + expect(Number(receiverFunds)).toBe(20) + }) + + it('disables send token button when balance is 0', async () => { + // GIVEN + const token = await getTokenContract(getWeb3(), accounts[0]) + await dispatchTknBalance(store, token.address, safeAddress) + + // WHEN + const SafeDom = travelToSafe(store, safeAddress) + + // $FlowFixMe + const buttons = TestUtils.scryRenderedDOMComponentsWithTag(SafeDom, 'button') + const expandBalance = buttons[EXPAND_BALANCE_INDEX] + + TestUtils.Simulate.click(expandBalance) + await sleep(800) + + // $FlowFixMe + const balanceButtons = TestUtils.scryRenderedDOMComponentsWithTag(SafeDom, 'button') + const tokenButton = balanceButtons[EXPAND_BALANCE_INDEX + 1] // expand button, and the next one is for sending + expect(tokenButton.hasAttribute('disabled')).toBe(true) + }) +}) diff --git a/src/test/utils/tokenMovements.js b/src/test/utils/tokenMovements.js index b5478187..c9f1ceb0 100644 --- a/src/test/utils/tokenMovements.js +++ b/src/test/utils/tokenMovements.js @@ -4,7 +4,7 @@ import { getProviderInfo, getBalanceInEtherOf, getWeb3 } from '~/wallets/getWeb3 import { promisify } from '~/utils/promisify' import withdraw, { DESTINATION_PARAM, VALUE_PARAM } from '~/routes/safe/component/Withdraw/withdraw' import { type Safe } from '~/routes/safe/store/model/safe' -import Token from '#/test/Token.json' +import Token from '#/test/FixedSupplyToken.json' import { ensureOnce } from '~/utils/singleton' export const addEtherTo = async (address: string, eth: string) => { diff --git a/src/test/utils/transactions/moveTokens.helper.js b/src/test/utils/transactions/moveTokens.helper.js index e8b7d28f..78c494e8 100644 --- a/src/test/utils/transactions/moveTokens.helper.js +++ b/src/test/utils/transactions/moveTokens.helper.js @@ -10,7 +10,6 @@ import addBalances from '~/routes/safe/store/actions/addBalances' export const sendMoveTokensForm = async ( SafeDom: React$Component, expandBalance: React$Component, - txName: string, value: number, destination: string, ) => { @@ -28,10 +27,8 @@ export const sendMoveTokensForm = async ( // fill the form const inputs = TestUtils.scryRenderedDOMComponentsWithTag(SafeDom, 'input') - const nameInput = inputs[0] - const destinationInput = inputs[1] - const amountofTokens = inputs[2] - TestUtils.Simulate.change(nameInput, { target: { value: txName } }) + const destinationInput = inputs[0] + const amountofTokens = inputs[1] TestUtils.Simulate.change(amountofTokens, { target: { value } }) TestUtils.Simulate.change(destinationInput, { target: { value: destination } }) // $FlowFixMe