WA-232 Test localStorage's tokens

This commit is contained in:
apanizo 2018-07-13 09:20:34 +02:00
parent 3fa7a10a16
commit 9f99d007ca
2 changed files with 57 additions and 34 deletions

View File

@ -0,0 +1,29 @@
// @flow
import * as TestUtils from 'react-dom/test-utils'
import { travelToTokens } from '~/test/builder/safe.dom.utils'
import { sleep } from '~/utils/timer'
import { type Token } from '~/routes/tokens/store/model/token'
export const enableFirstToken = async (store: Store, safeAddress: string) => {
const TokensDom = await travelToTokens(store, safeAddress)
await sleep(400)
// WHEN
const inputs = TestUtils.scryRenderedDOMComponentsWithTag(TokensDom, 'input')
const ethTokenInput = inputs[2]
expect(ethTokenInput.hasAttribute('disabled')).toBe(true)
const firstTokenInput = inputs[0]
expect(firstTokenInput.hasAttribute('disabled')).toBe(false)
TestUtils.Simulate.change(firstTokenInput, { target: { checked: 'true' } })
}
export const testToken = (token: Token | typeof undefined, symbol: string, status: boolean, funds?: string) => {
if (!token) throw new Error()
expect(token.get('symbol')).toBe(symbol)
expect(token.get('status')).toBe(status)
if (funds) {
expect(token.get('funds')).toBe(funds)
}
}

View File

@ -1,5 +1,6 @@
// @flow // @flow
import * as TestUtils from 'react-dom/test-utils' import * as TestUtils from 'react-dom/test-utils'
import { List } from 'immutable'
import { getWeb3 } from '~/wallets/getWeb3' import { getWeb3 } from '~/wallets/getWeb3'
import { type Match } from 'react-router-dom' import { type Match } from 'react-router-dom'
import { promisify } from '~/utils/promisify' import { promisify } from '~/utils/promisify'
@ -12,7 +13,8 @@ import { travelToTokens } from '~/test/builder/safe.dom.utils'
import { sleep } from '~/utils/timer' import { sleep } from '~/utils/timer'
import { buildMathPropsFrom } from '~/test/utils/buildReactRouterProps' import { buildMathPropsFrom } from '~/test/utils/buildReactRouterProps'
import { tokenListSelector, activeTokensSelector } from '~/routes/tokens/store/selectors' import { tokenListSelector, activeTokensSelector } from '~/routes/tokens/store/selectors'
import { type Token } from '~/routes/tokens/store/model/token' import { getTokens } from '~/utils/localStorage/tokens'
import { enableFirstToken, testToken } from '~/test/builder/tokens.dom.utils'
const fetchTokensModule = require('../routes/tokens/store/actions/fetchTokens') const fetchTokensModule = require('../routes/tokens/store/actions/fetchTokens')
@ -49,7 +51,7 @@ describe('DOM > Feature > Enable and disable default tokens', () => {
])) ]))
}) })
it('retrieves only ether as active token', async () => { it('retrieves only ether as active token in first moment', async () => {
// GIVEN // GIVEN
const store = aNewStore() const store = aNewStore()
const safeAddress = await aMinedSafe(store) const safeAddress = await aMinedSafe(store)
@ -63,32 +65,15 @@ describe('DOM > Feature > Enable and disable default tokens', () => {
const tokens = TestUtils.scryRenderedComponentsWithType(TokensDom, TokenComponent) const tokens = TestUtils.scryRenderedComponentsWithType(TokensDom, TokenComponent)
expect(tokens.length).toBe(3) expect(tokens.length).toBe(3)
const firstToken = tokens[0] testToken(tokens[0].props.token, 'FTE', false)
expect(firstToken.props.token.get('symbol')).toBe('FTE') testToken(tokens[1].props.token, 'STE', false)
expect(firstToken.props.token.get('status')).toBe(false) testToken(tokens[2].props.token, 'ETH', true)
const secontToken = tokens[1] const ethCheckbox = TestUtils.findRenderedComponentWithType(tokens[2], Checkbox)
expect(secontToken.props.token.get('symbol')).toBe('STE')
expect(secontToken.props.token.get('status')).toBe(false)
const etherToken = tokens[2]
expect(etherToken.props.token.get('symbol')).toBe('ETH')
expect(etherToken.props.token.get('status')).toBe(true)
const ethCheckbox = TestUtils.findRenderedComponentWithType(etherToken, Checkbox)
if (!ethCheckbox) throw new Error() if (!ethCheckbox) throw new Error()
expect(ethCheckbox.props.disabled).toBe(true) expect(ethCheckbox.props.disabled).toBe(true)
}) })
const testToken = (token: Token | typeof undefined, symbol: string, status: boolean, funds?: string) => {
if (!token) throw new Error()
expect(token.get('symbol')).toBe(symbol)
expect(token.get('status')).toBe(status)
if (funds) {
expect(token.get('funds')).toBe(funds)
}
}
it('fetch balances of only enabled tokens', async () => { it('fetch balances of only enabled tokens', async () => {
// GIVEN // GIVEN
const store = aNewStore() const store = aNewStore()
@ -96,17 +81,7 @@ describe('DOM > Feature > Enable and disable default tokens', () => {
await addTknTo(safeAddress, 50, firstErc20Token) await addTknTo(safeAddress, 50, firstErc20Token)
await addTknTo(safeAddress, 50, secondErc20Token) await addTknTo(safeAddress, 50, secondErc20Token)
await store.dispatch(fetchTokensModule.fetchTokens(safeAddress)) await store.dispatch(fetchTokensModule.fetchTokens(safeAddress))
const TokensDom = await travelToTokens(store, safeAddress) await enableFirstToken(store, safeAddress)
await sleep(400)
// WHEN
const inputs = TestUtils.scryRenderedDOMComponentsWithTag(TokensDom, 'input')
const ethTokenInput = inputs[2]
expect(ethTokenInput.hasAttribute('disabled')).toBe(true)
const firstTokenInput = inputs[0]
expect(firstTokenInput.hasAttribute('disabled')).toBe(false)
TestUtils.Simulate.change(firstTokenInput, { target: { checked: 'true' } })
// THEN // THEN
const match: Match = buildMathPropsFrom(safeAddress) const match: Match = buildMathPropsFrom(safeAddress)
@ -131,4 +106,23 @@ describe('DOM > Feature > Enable and disable default tokens', () => {
testToken(fundedTokenList.get(1), 'STE', false, '0') testToken(fundedTokenList.get(1), 'STE', false, '0')
testToken(fundedTokenList.get(2), 'ETH', true, '0') testToken(fundedTokenList.get(2), 'ETH', true, '0')
}) })
it('localStorage always returns a list', async () => {
const store = aNewStore()
const safeAddress = await aMinedSafe(store)
let tokens: List<string> = getTokens(safeAddress)
expect(tokens).toEqual(List([]))
await store.dispatch(fetchTokensModule.fetchTokens(safeAddress))
tokens = getTokens(safeAddress)
expect(tokens.count()).toBe(0)
await enableFirstToken(store, safeAddress)
tokens = getTokens(safeAddress)
expect(tokens.count()).toBe(1)
await store.dispatch(fetchTokensModule.fetchTokens(safeAddress))
tokens = getTokens(safeAddress)
expect(tokens.count()).toBe(1)
})
}) })