From 2d05557df504c4c13f0307eece095c6f73f68915 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Thu, 12 Sep 2019 22:03:06 +0400 Subject: [PATCH 1/2] Add test for balance updates --- src/test/safe.dom.balances.test.js | 75 +++++++++++++++++++ .../transactions/transactionList.helper.js | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/test/safe.dom.balances.test.js diff --git a/src/test/safe.dom.balances.test.js b/src/test/safe.dom.balances.test.js new file mode 100644 index 00000000..1fc9b57e --- /dev/null +++ b/src/test/safe.dom.balances.test.js @@ -0,0 +1,75 @@ +// @flow +import { List } from 'immutable' +import { aNewStore } from '~/store' +import { sleep } from '~/utils/timer' +import { aMinedSafe } from '~/test/builder/safe.redux.builder' +import { sendTokenTo, sendEtherTo } from '~/test/utils/tokenMovements' +import { renderSafeView } from '~/test/builder/safe.dom.utils' +import { dispatchAddTokenToList } from '~/test/utils/transactions/moveTokens.helper' +import TokenBalanceRecord from '~/routes/safe/store/models/tokenBalance' +import { calculateBalanceOf } from '~/routes/safe/store/actions/fetchTokenBalances' +import updateActiveTokens from '~/routes/safe/store/actions/updateActiveTokens' +import '@testing-library/jest-dom/extend-expect' +import updateSafe from '~/routes/safe/store/actions/updateSafe' +import { BALANCE_ROW_TEST_ID } from '~/routes/safe/components/Balances' +import { getBalanceInEtherOf } from '~/logic/wallets/getWeb3' + +describe('DOM > Feature > Balances', () => { + let store + let safeAddress: string + beforeEach(async () => { + store = aNewStore() + safeAddress = await aMinedSafe(store) + }) + + it('Updates token balances automatically', async () => { + const tokensAmount = '100' + const tokenAddress = await sendTokenTo(safeAddress, tokensAmount) + await dispatchAddTokenToList(store, tokenAddress) + + const SafeDom = await renderSafeView(store, safeAddress) + + // Activate token + const safeTokenBalance = await calculateBalanceOf(tokenAddress, safeAddress, 18) + expect(safeTokenBalance).toBe(tokensAmount) + + const balanceAsRecord = TokenBalanceRecord({ + address: tokenAddress, + balance: safeTokenBalance, + }) + store.dispatch(updateActiveTokens(safeAddress, List([tokenAddress]))) + store.dispatch(updateSafe({ address: safeAddress, balances: List([balanceAsRecord]) })) + await sleep(1000) + + const balanceRows = SafeDom.getAllByTestId(BALANCE_ROW_TEST_ID) + expect(balanceRows.length).toBe(2) + + expect(balanceRows[1]).toHaveTextContent(`${tokensAmount} FTE`) + + await sendTokenTo(safeAddress, tokensAmount) + + await sleep(2000) + + expect(balanceRows[2]).toHaveTextContent(`${parseInt(tokensAmount, 10) * 2} FTE`) + }) + + it('Updates ether balance automatically', async () => { + const etherAmount = '1' + await sendEtherTo(safeAddress, etherAmount) + + const SafeDom = await renderSafeView(store, safeAddress) + + const safeEthBalance = await getBalanceInEtherOf(safeAddress) + expect(safeEthBalance).toBe(etherAmount) + + const balanceRows = SafeDom.getAllByTestId(BALANCE_ROW_TEST_ID) + expect(balanceRows.length).toBe(1) + + expect(balanceRows[0]).toHaveTextContent(`${etherAmount} ETH`) + + await sendEtherTo(safeAddress, etherAmount) + await sleep(2000) + + expect(balanceRows[0]).toHaveTextContent(`${parseInt(etherAmount, 10) * 2} ETH`) + }) +}) diff --git a/src/test/utils/transactions/transactionList.helper.js b/src/test/utils/transactions/transactionList.helper.js index 2238dce7..7f92f08a 100644 --- a/src/test/utils/transactions/transactionList.helper.js +++ b/src/test/utils/transactions/transactionList.helper.js @@ -24,7 +24,7 @@ export const getLastTransaction = async (SafeDom: React.Component) => export const checkRegisteredTxSend = async ( SafeDom: React.Component, - ethAmount: number, + ethAmount: number | string, symbol: string, ethAddress: string, ) => { From 5203ba3cfb8f122c10d9cd312bc2d067de78b529 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Sat, 14 Sep 2019 18:37:58 +0300 Subject: [PATCH 2/2] Use waitForElement instead of sleep() in balances test --- src/test/safe.dom.balances.test.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/test/safe.dom.balances.test.js b/src/test/safe.dom.balances.test.js index 1fc9b57e..d75d3bd7 100644 --- a/src/test/safe.dom.balances.test.js +++ b/src/test/safe.dom.balances.test.js @@ -1,4 +1,5 @@ // @flow +import { waitForElement } from '@testing-library/react' import { List } from 'immutable' import { aNewStore } from '~/store' import { sleep } from '~/utils/timer' @@ -44,13 +45,11 @@ describe('DOM > Feature > Balances', () => { const balanceRows = SafeDom.getAllByTestId(BALANCE_ROW_TEST_ID) expect(balanceRows.length).toBe(2) - expect(balanceRows[1]).toHaveTextContent(`${tokensAmount} FTE`) + await waitForElement(() => SafeDom.getByText(`${tokensAmount} OMG`)) await sendTokenTo(safeAddress, tokensAmount) - await sleep(2000) - - expect(balanceRows[2]).toHaveTextContent(`${parseInt(tokensAmount, 10) * 2} FTE`) + await waitForElement(() => SafeDom.getByText(`${parseInt(tokensAmount, 10) * 2} OMG`)) }) it('Updates ether balance automatically', async () => { @@ -65,11 +64,10 @@ describe('DOM > Feature > Balances', () => { const balanceRows = SafeDom.getAllByTestId(BALANCE_ROW_TEST_ID) expect(balanceRows.length).toBe(1) - expect(balanceRows[0]).toHaveTextContent(`${etherAmount} ETH`) + await waitForElement(() => SafeDom.getByText(`${etherAmount} ETH`)) await sendEtherTo(safeAddress, etherAmount) - await sleep(2000) - expect(balanceRows[0]).toHaveTextContent(`${parseInt(etherAmount, 10) * 2} ETH`) + await waitForElement(() => SafeDom.getByText(`${parseInt(etherAmount, 10) * 2} ETH`)) }) })