refactor sending funds test

This commit is contained in:
Mikhail Mikheev 2019-06-04 14:24:23 +04:00
parent 1d62ef700e
commit 3c1c488ee2
3 changed files with 57 additions and 82 deletions

View File

@ -50,11 +50,6 @@ export const grantedSelector: Selector<GlobalState, RouterProps, boolean> = crea
},
)
type UserToken = {
address: string,
balance: string,
}
const safeEthAsTokenSelector: Selector<GlobalState, RouterProps, ?Token> = createSelector(
safeSelector,
(safe: Safe) => {

View File

@ -3,15 +3,16 @@ import { fireEvent, cleanup } from '@testing-library/react'
import { List } from 'immutable'
import { aNewStore } from '~/store'
import { aMinedSafe } from '~/test/builder/safe.redux.builder'
import { sendTokenTo, getFirstTokenContract, sendEtherTo } from '~/test/utils/tokenMovements'
import { EXPAND_BALANCE_INDEX, renderSafeView } from '~/test/builder/safe.dom.utils'
import { sendTokenTo, sendEtherTo } from '~/test/utils/tokenMovements'
import { renderSafeView } from '~/test/builder/safe.dom.utils'
import { getWeb3, getBalanceInEtherOf } from '~/logic/wallets/getWeb3'
import { sendMoveTokensForm, dispatchAddTokenToList } from '~/test/utils/transactions/moveTokens.helper'
import { dispatchAddTokenToList } from '~/test/utils/transactions/moveTokens.helper'
import { sleep } from '~/utils/timer'
import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers'
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 'jest-dom/extend-expect'
import updateSafe from '~/routes/safe/store/actions/updateSafe'
afterEach(cleanup)
@ -33,7 +34,7 @@ describe('DOM > Feature > Funds', () => {
// WHEN
const SafeDom = renderSafeView(store, safeAddress)
await sleep(1000)
await sleep(1300)
// Open send funds modal
const balanceRows = SafeDom.getAllByTestId('balance-row')
@ -56,7 +57,7 @@ describe('DOM > Feature > Funds', () => {
await sleep(1000)
// THEN
const safeFunds = await calculateBalanceOf(ETH_ADDRESS, safeAddress, 18)
const safeFunds = await getBalanceInEtherOf(safeAddress)
expect(Number(safeFunds)).toBe(0)
const receiverFunds = await getBalanceInEtherOf(accounts[0])
@ -68,34 +69,52 @@ describe('DOM > Feature > Funds', () => {
it('Sends Tokens with threshold = 1', async () => {
// GIVEN
const numTokens = '100'
const tokenAddress = await sendTokenTo(safeAddress, numTokens)
const safeTokenBalance = await calculateBalanceOf(tokenAddress, safeAddress, 18)
store.dispatch(updateActiveTokens(safeAddress, List([tokenAddress])))
await dispatchAddTokenToList(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))
const tokensAmount = '100'
const tokenReceiver = accounts[1]
const tokenAddress = await sendTokenTo(safeAddress, tokensAmount)
await dispatchAddTokenToList(store, tokenAddress)
// WHEN
const SafeDom = await renderSafeView(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])
await sleep(1300)
// 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)
// Open send funds modal
const balanceRows = SafeDom.getAllByTestId('balance-row')
expect(balanceRows.length).toBe(2)
const sendButtons = SafeDom.getAllByTestId('balance-send-btn')
expect(sendButtons.length).toBe(2)
fireEvent.click(sendButtons[1])
// 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: tokenReceiver } })
fireEvent.change(amountInput, { target: { value: tokensAmount } })
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 token = await getFirstTokenContract(getWeb3(), accounts[0])
const nativeSafeFunds = await token.balanceOf(safeAddress)
expect(Number(nativeSafeFunds.valueOf())).toEqual(80 * 10 ** 18)
const safeFunds = await calculateBalanceOf(tokenAddress, safeAddress, 18)
expect(Number(safeFunds)).toBe(0)
const receiverFunds = await calculateBalanceOf(tokenAddress, tokenReceiver, 18)
expect(receiverFunds).toBe(tokensAmount)
})
})

View File

@ -1,52 +1,13 @@
// @flow
import * as React from 'react'
import { Map } from 'immutable'
import TestUtils from 'react-dom/test-utils'
import { sleep } from '~/utils/timer'
import * as fetchTokensAction from '~/logic/tokens/store/actions/fetchTokens'
import { checkMinedTx, checkPendingTx, EXPAND_BALANCE_INDEX } from '~/test/builder/safe.dom.utils'
import { whenExecuted } from '~/test/utils/logTransactions'
import SendToken from '~/routes/safe/components/SendToken'
import { checkMinedTx, checkPendingTx } from '~/test/builder/safe.dom.utils'
import { makeToken, type Token } from '~/logic/tokens/store/model/token'
import addTokens from '~/logic/tokens/store/actions/saveTokens'
import { calculateBalanceOf } from '~/routes/safe/store/actions/fetchTokenBalances'
export const sendMoveTokensForm = async (
SafeDom: React.Component<any, any>,
expandBalance: React.Component<any, any>,
value: number,
destination: string,
) => {
TestUtils.Simulate.click(expandBalance)
await sleep(500)
// $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(false)
// load move tokens form component
TestUtils.Simulate.click(tokenButton)
await sleep(500)
// fill the form
const inputs = TestUtils.scryRenderedDOMComponentsWithTag(SafeDom, 'input')
const destinationInput = inputs[0]
const amountofTokens = inputs[1]
TestUtils.Simulate.change(amountofTokens, { target: { value } })
TestUtils.Simulate.change(destinationInput, { target: { value: destination } })
// $FlowFixMe
const form = TestUtils.findRenderedDOMComponentWithTag(SafeDom, 'form')
// submit it
TestUtils.Simulate.submit(form)
TestUtils.Simulate.submit(form)
return whenExecuted(SafeDom, SendToken)
}
export const dispatchAddTokenToList = async (store: Store, tokenAddress: string, address: string) => {
const fetchBalancesMock = jest.fn()
const balances: Map<string, Token> = Map().set(
export const dispatchAddTokenToList = async (store: Store, tokenAddress: string) => {
const fetchTokensMock = jest.fn()
const tokens: Map<string, Token> = Map().set(
'TKN',
makeToken({
address: tokenAddress,
@ -57,9 +18,9 @@ export const dispatchAddTokenToList = async (store: Store, tokenAddress: string,
'https://github.com/TrustWallet/tokens/blob/master/images/0x6810e776880c02933d47db1b9fc05908e5386b96.png?raw=true',
}),
)
fetchBalancesMock.mockImplementation(() => store.dispatch(addTokens(address, balances)))
await store.dispatch(fetchTokensAction.fetchTokens())
fetchBalancesMock.mockRestore()
fetchTokensMock.mockImplementation(() => store.dispatch(addTokens(tokens)))
fetchTokensMock()
fetchTokensMock.mockRestore()
}
export const checkMinedMoveTokensTx = (Transaction: React.Component<any, any>, name: string) => {