From 0917f21ec110d3064b41f4ee4c3847c3f0a2c7b0 Mon Sep 17 00:00:00 2001 From: mmv Date: Mon, 3 Jun 2019 16:49:52 +0400 Subject: [PATCH] refactor send ETH test --- build/contracts/DevDependenciesGetter.json | 2 +- build/contracts/GnosisMath.json | 2 +- build/contracts/GnosisStandardToken.json | 2 +- build/contracts/Migrations.json | 2 +- build/contracts/Proxied.json | 2 +- build/contracts/Proxy.json | 2 +- build/contracts/StandardTokenData.json | 2 +- build/contracts/Token.json | 2 +- build/contracts/TokenOMG.json | 2 +- build/contracts/TokenRDN.json | 2 +- src/store/index.js | 4 +- src/test/safe.dom.funds.test.js | 38 +++++++---- src/test/safe.dom.tokens.test.js | 73 ++++++++++++++++++++++ src/test/utils/tokenMovements.js | 8 ++- yarn.lock | 8 +-- 15 files changed, 119 insertions(+), 32 deletions(-) create mode 100644 src/test/safe.dom.tokens.test.js diff --git a/build/contracts/DevDependenciesGetter.json b/build/contracts/DevDependenciesGetter.json index 77f80b7a..f4996baa 100644 --- a/build/contracts/DevDependenciesGetter.json +++ b/build/contracts/DevDependenciesGetter.json @@ -138,7 +138,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.338Z", + "updatedAt": "2019-06-03T08:59:02.077Z", "devdoc": { "methods": {} }, diff --git a/build/contracts/GnosisMath.json b/build/contracts/GnosisMath.json index a8148fca..f02e538d 100644 --- a/build/contracts/GnosisMath.json +++ b/build/contracts/GnosisMath.json @@ -32319,7 +32319,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.349Z", + "updatedAt": "2019-06-03T08:59:02.114Z", "devdoc": { "author": "Alan Lu - Stefan George - ", "methods": { diff --git a/build/contracts/GnosisStandardToken.json b/build/contracts/GnosisStandardToken.json index da7e48c1..e5331808 100644 --- a/build/contracts/GnosisStandardToken.json +++ b/build/contracts/GnosisStandardToken.json @@ -5582,7 +5582,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.346Z", + "updatedAt": "2019-06-03T08:59:02.084Z", "devdoc": { "methods": { "allowance(address,address)": { diff --git a/build/contracts/Migrations.json b/build/contracts/Migrations.json index c6044c22..6a0e60ac 100644 --- a/build/contracts/Migrations.json +++ b/build/contracts/Migrations.json @@ -1369,7 +1369,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.344Z", + "updatedAt": "2019-06-03T08:59:02.080Z", "devdoc": { "methods": {} }, diff --git a/build/contracts/Proxied.json b/build/contracts/Proxied.json index 15e6e14e..016f4379 100644 --- a/build/contracts/Proxied.json +++ b/build/contracts/Proxied.json @@ -971,7 +971,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.362Z", + "updatedAt": "2019-06-03T08:59:02.136Z", "devdoc": { "author": "Alan Lu - ", "methods": {}, diff --git a/build/contracts/Proxy.json b/build/contracts/Proxy.json index 19d58dc3..5a25d765 100644 --- a/build/contracts/Proxy.json +++ b/build/contracts/Proxy.json @@ -987,7 +987,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.362Z", + "updatedAt": "2019-06-03T08:59:02.137Z", "devdoc": { "author": "Stefan George - ", "methods": { diff --git a/build/contracts/StandardTokenData.json b/build/contracts/StandardTokenData.json index 721a4480..64d7314a 100644 --- a/build/contracts/StandardTokenData.json +++ b/build/contracts/StandardTokenData.json @@ -5408,7 +5408,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.347Z", + "updatedAt": "2019-06-03T08:59:02.087Z", "devdoc": { "methods": {} }, diff --git a/build/contracts/Token.json b/build/contracts/Token.json index f858bea1..b6934a6b 100644 --- a/build/contracts/Token.json +++ b/build/contracts/Token.json @@ -1854,7 +1854,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.363Z", + "updatedAt": "2019-06-03T08:59:02.139Z", "devdoc": { "methods": {}, "title": "Abstract token contract - Functions to be implemented by token contracts" diff --git a/build/contracts/TokenOMG.json b/build/contracts/TokenOMG.json index cd77a240..c07180d9 100644 --- a/build/contracts/TokenOMG.json +++ b/build/contracts/TokenOMG.json @@ -953,7 +953,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.345Z", + "updatedAt": "2019-06-03T08:59:02.081Z", "devdoc": { "methods": { "allowance(address,address)": { diff --git a/build/contracts/TokenRDN.json b/build/contracts/TokenRDN.json index d729b6b8..c3fdf189 100644 --- a/build/contracts/TokenRDN.json +++ b/build/contracts/TokenRDN.json @@ -953,7 +953,7 @@ }, "networks": {}, "schemaVersion": "3.0.10", - "updatedAt": "2019-05-31T13:47:05.345Z", + "updatedAt": "2019-06-03T08:59:02.082Z", "devdoc": { "methods": { "allowance(address,address)": { diff --git a/src/store/index.js b/src/store/index.js index 9542511c..b9703c0f 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,5 +1,5 @@ // @flow -import { createBrowserHistory } from 'history' +import { createBrowserHistory, createMemoryHistory } from 'history' import { connectRouter, routerMiddleware } from 'connected-react-router' import { combineReducers, createStore, applyMiddleware, compose, type Reducer, type Store, @@ -14,7 +14,7 @@ import transactions, { TRANSACTIONS_REDUCER_ID, } from '~/routes/safe/store/reducer/transactions' -export const history = createBrowserHistory() +export const history = process.env.NODE_ENV === 'test' ? createMemoryHistory() : createBrowserHistory() // eslint-disable-next-line const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose diff --git a/src/test/safe.dom.funds.test.js b/src/test/safe.dom.funds.test.js index d675e0a3..cf09cca5 100644 --- a/src/test/safe.dom.funds.test.js +++ b/src/test/safe.dom.funds.test.js @@ -8,6 +8,9 @@ import { EXPAND_BALANCE_INDEX, renderSafeView } from '~/test/builder/safe.dom.ut import { getWeb3 } from '~/logic/wallets/getWeb3' import { sendMoveTokensForm, dispatchTknBalance } from '~/test/utils/transactions/moveTokens.helper' import { sleep } from '~/utils/timer' +import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers' +import { calculateBalanceOf } from '~/routes/safe/store/actions/fetchTokenBalances' +import 'jest-dom/extend-expect' afterEach(cleanup) @@ -37,20 +40,33 @@ describe('DOM > Feature > Funds', () => { await sleep(800) const balanceRows = SafeDom.getAllByTestId('balance-row') - const buttons = TestUtils.scryRenderedDOMComponentsWithTag(SafeDom, 'button') - const expandBalance = buttons[EXPAND_BALANCE_INDEX] - const receiver = accounts[2] - await sendMoveTokensForm(SafeDom, expandBalance, 20, accounts[2]) + expect(balanceRows[0]).toHaveTextContent(`${ethAmount} ETH`) + const sendButton = SafeDom.getByTestId('balance-send-btn') + fireEvent.click(sendButton) + + // Fill first send funds screen + const recipientInput = SafeDom.getByPlaceholderText('Recipient*') + const amountInput = SafeDom.getByPlaceholderText('Amount*') + const reviewBtn = SafeDom.getByTestId('review-tx-btn') + fireEvent.change(recipientInput, { target: { value: accounts[0] } }) + fireEvent.change(amountInput, { target: { value: ethAmount } }) + await sleep(200) + fireEvent.click(reviewBtn) + + // Submit the tx (Review Tx screen) + const submitBtn = SafeDom.getByTestId('submit-tx-btn') + fireEvent.click(submitBtn) + await sleep(1000) // THEN - const safeFunds = await fetchBalancesAction.calculateBalanceOf(tokenAddress, safeAddress, 18) - expect(Number(safeFunds)).toBe(80) - const receiverFunds = await fetchBalancesAction.calculateBalanceOf(tokenAddress, receiver, 18) - expect(Number(receiverFunds)).toBe(20) + const safeFunds = await calculateBalanceOf(ETH_ADDRESS, safeAddress, 18) + expect(Number(safeFunds)).toBe(0) - const token = await getFirstTokenContract(getWeb3(), accounts[0]) - const nativeSafeFunds = await token.balanceOf(safeAddress) - expect(Number(nativeSafeFunds.valueOf())).toEqual(80 * 10 ** 18) + const receiverFunds = await getBalanceInEtherOf(accounts[0]) + const ESTIMATED_GASCOSTS = 0.1 + expect(Number(parseInt(receiverFunds, 10) - parseInt(balanceAfterSendingEthToSafe, 10))).toBeGreaterThan( + parseInt(ethAmount, 10) - ESTIMATED_GASCOSTS, + ) }) it('Sends Tokens', async () => { diff --git a/src/test/safe.dom.tokens.test.js b/src/test/safe.dom.tokens.test.js new file mode 100644 index 00000000..fe5e856c --- /dev/null +++ b/src/test/safe.dom.tokens.test.js @@ -0,0 +1,73 @@ +// @flow + +import TestUtils from 'react-dom/test-utils' +import * as fetchBalancesAction from '~/logic/tokens/store/actions/fetchTokens' +import { aNewStore } from '~/store' +import { aMinedSafe } from '~/test/builder/safe.redux.builder' +import { addTknTo, getFirstTokenContract } from '~/test/utils/tokenMovements' +import { EXPAND_BALANCE_INDEX, travelToSafe } from '~/test/builder/safe.dom.utils' +import { getWeb3 } from '~/logic/wallets/getWeb3' +import { sendMoveTokensForm, dispatchTknBalance } from '~/test/utils/transactions/moveTokens.helper' +import { sleep } from '~/utils/timer' + +describe('DOM > Feature > SAFE ERC20 TOKENS', () => { + let accounts + beforeEach(async () => { + accounts = await getWeb3().eth.getAccounts() + }) + + it('sends ERC20 tokens', async () => { + // GIVEN + const store = aNewStore() + const safeAddress = await aMinedSafe(store) + + 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, 18) + expect(Number(safeFunds)).toBe(80) + const receiverFunds = await fetchBalancesAction.calculateBalanceOf(tokenAddress, receiver, 18) + expect(Number(receiverFunds)).toBe(20) + + const token = await getFirstTokenContract(getWeb3(), accounts[0]) + const nativeSafeFunds = await token.balanceOf(safeAddress) + expect(Number(nativeSafeFunds.valueOf())).toEqual(80 * 10 ** 18) + }) + + it('disables send token button when balance is 0', async () => { + // GIVEN + const token = await getFirstTokenContract(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 aa86866f..60cbaec5 100644 --- a/src/test/utils/tokenMovements.js +++ b/src/test/utils/tokenMovements.js @@ -22,7 +22,9 @@ export const checkBalanceOf = async (addressToTest: string, value: string) => { const createTokenOMGContract = async (web3: any, creator: string) => { const token = contract(TokenOMG) const { toBN } = web3.utils - const amount = toBN(50000).mul(toBN(10).pow(toBN(18))).toString() + const amount = toBN(50000) + .mul(toBN(10).pow(toBN(18))) + .toString() token.setProvider(web3.currentProvider) return token.new(amount, { from: creator }) @@ -31,7 +33,9 @@ const createTokenOMGContract = async (web3: any, creator: string) => { const createTokenRDNContract = async (web3: any, creator: string) => { const token = contract(TokenRDN) const { toBN } = web3.utils - const amount = toBN(50000).mul(toBN(10).pow(toBN(18))).toString() + const amount = toBN(50000) + .mul(toBN(10).pow(toBN(18))) + .toString() token.setProvider(web3.currentProvider) return token.new(amount, { from: creator }) diff --git a/yarn.lock b/yarn.lock index 26b34c81..7c295498 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1294,12 +1294,6 @@ resolved "https://registry.yarnpkg.com/@emotion/weak-memoize/-/weak-memoize-0.2.2.tgz#63985d3d8b02530e0869962f4da09142ee8e200e" integrity sha512-n/VQ4mbfr81aqkx/XmVicOLjviMuy02eenSdJY33SVA7S2J42EU0P1H0mOogfYedb3wXA0d/LVtBrgTSm04WEA== -"@gnosis.pm/mock-contract@github:gnosis/mock-contract#sol_0_5_0": - version "2.0.2" - resolved "https://codeload.github.com/gnosis/mock-contract/tar.gz/2ec1266eeef1baf7514cdb65b3a3a1aef224f3f2" - dependencies: - ethereumjs-abi "^0.6.5" - "@gnosis.pm/safe-contracts@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@gnosis.pm/safe-contracts/-/safe-contracts-1.0.0.tgz#2b562b1e23a0da1047a9f38ef71a70f811e75dd9" @@ -6860,7 +6854,7 @@ ethereumjs-abi@0.6.5: bn.js "^4.10.0" ethereumjs-util "^4.3.0" -ethereumjs-abi@^0.6.5, ethereumjs-abi@^0.6.7, "ethereumjs-abi@git+https://github.com/ethereumjs/ethereumjs-abi.git": +ethereumjs-abi@^0.6.7, "ethereumjs-abi@git+https://github.com/ethereumjs/ethereumjs-abi.git": version "0.6.7" resolved "git+https://github.com/ethereumjs/ethereumjs-abi.git#8431eab7b3384e65e8126a4602520b78031666fb" dependencies: