From bfa5a52bc42caad293b1386c78469fda3d2521af Mon Sep 17 00:00:00 2001 From: mmv Date: Thu, 30 May 2019 19:11:53 +0400 Subject: [PATCH] refactor safe dom load test, fix safe address validator --- .../load/components/DetailsForm/index.jsx | 16 +++---- src/routes/load/components/Layout.jsx | 46 ++++++++----------- src/test/safe.dom.load.test.js | 44 ++++++++++++------ 3 files changed, 54 insertions(+), 52 deletions(-) diff --git a/src/routes/load/components/DetailsForm/index.jsx b/src/routes/load/components/DetailsForm/index.jsx index e14d9585..9879dd4e 100644 --- a/src/routes/load/components/DetailsForm/index.jsx +++ b/src/routes/load/components/DetailsForm/index.jsx @@ -37,9 +37,11 @@ const styles = () => ({ export const SAFE_INSTANCE_ERROR = 'Address given is not a safe instance' export const SAFE_MASTERCOPY_ERROR = 'Mastercopy used by this safe is not the same' +// In case of an error here, it will be swallowed by final-form +// So if you're experiencing any strang behaviours like freeze or hanging +// Don't mind to check if everything is OK inside this function :) export const safeFieldsValidation = async (values: Object) => { const errors = {} - const web3 = getWeb3() const safeAddress = values[FIELD_LOAD_ADDRESS] if (!safeAddress || mustBeEthereumAddress(safeAddress) !== undefined) { @@ -51,27 +53,21 @@ export const safeFieldsValidation = async (values: Object) => { const code = await web3.eth.getCode(safeAddress) const codeWithoutMetadata = code.substring(0, code.lastIndexOf(metaData)) - const proxyCode = SafeProxy.deployedBytecode const proxyCodeWithoutMetadata = proxyCode.substring(0, proxyCode.lastIndexOf(metaData)) - const safeInstance = codeWithoutMetadata === proxyCodeWithoutMetadata if (!safeInstance) { errors[FIELD_LOAD_ADDRESS] = SAFE_INSTANCE_ERROR - return errors } // check mastercopy - const proxy = contract(SafeProxy) - proxy.setProvider(web3.currentProvider) - const proxyInstance = await proxy.at(safeAddress) - const proxyImplementation = await proxyInstance.implementation() - + const proxyAddressFromStorage = await web3.eth.getStorageAt(safeAddress, 0) + const checksummedProxyAddress = web3.utils.toChecksumAddress(proxyAddressFromStorage) const safeMaster = await getSafeMasterContract() const masterCopy = safeMaster.address - const sameMasterCopy = proxyImplementation === masterCopy + const sameMasterCopy = checksummedProxyAddress === masterCopy if (!sameMasterCopy) { errors[FIELD_LOAD_ADDRESS] = SAFE_MASTERCOPY_ERROR } diff --git a/src/routes/load/components/Layout.jsx b/src/routes/load/components/Layout.jsx index d5741028..167ce9c8 100644 --- a/src/routes/load/components/Layout.jsx +++ b/src/routes/load/components/Layout.jsx @@ -12,9 +12,7 @@ import { history } from '~/store' import { secondary } from '~/theme/variables' import { type SelectorProps } from '~/routes/load/container/selector' -const getSteps = () => [ - 'Details', 'Review', -] +const getSteps = () => ['Details', 'Review'] type Props = SelectorProps & { onLoadSafeSubmit: (values: Object) => Promise, @@ -37,30 +35,24 @@ const Layout = ({ return ( - { provider - ? ( - - - - - - Load existing Safe - - - - { DetailsForm } - - - { ReviewInformation } - - - - ) - :
No metamask detected
- } + {provider ? ( + + + + + + Load existing Safe + + + {DetailsForm} + + {ReviewInformation} + + + + ) : ( +
No metamask detected
+ )}
) } diff --git a/src/test/safe.dom.load.test.js b/src/test/safe.dom.load.test.js index f0f0fef6..d909127b 100644 --- a/src/test/safe.dom.load.test.js +++ b/src/test/safe.dom.load.test.js @@ -1,8 +1,8 @@ // @flow import * as React from 'react' import { type Store } from 'redux' -import TestUtils from 'react-dom/test-utils' import { Provider } from 'react-redux' +import { render, fireEvent, cleanup } from 'react-testing-library' import { ConnectedRouter } from 'connected-react-router' import Load from '~/routes/load/container/Load' import { aNewStore, history, type GlobalState } from '~/store' @@ -13,12 +13,30 @@ import { makeProvider } from '~/logic/wallets/store/model/provider' import { aMinedSafe } from './builder/safe.redux.builder' import { whenSafeDeployed } from './builder/safe.dom.utils' -const travelToLoadRoute = async (localStore: Store) => { +afterEach(cleanup) + +// https://github.com/testing-library/react-testing-library/issues/281 +const originalError = console.error +beforeAll(() => { + console.error = (...args) => { + if (/Warning.*not wrapped in act/.test(args[0])) { + return + } + originalError.call(console, ...args) + } +}) + +afterAll(() => { + console.error = originalError +}) + + +const renderLoadSafe = async (localStore: Store) => { const provider = await getProviderInfo() const walletRecord = makeProvider(provider) localStore.dispatch(addProvider(walletRecord)) - return TestUtils.renderIntoDocument( + return render( @@ -31,26 +49,22 @@ describe('DOM > Feature > LOAD a safe', () => { it('load correctly a created safe', async () => { const store = aNewStore() const address = await aMinedSafe(store) - const LoadDom = await travelToLoadRoute(store) - - const form = TestUtils.findRenderedDOMComponentWithTag(LoadDom, 'form') - const inputs = TestUtils.scryRenderedDOMComponentsWithTag(LoadDom, 'input') + const LoadSafePage = await renderLoadSafe(store) + const form = LoadSafePage.getByTestId('load-safe-form') + const safeNameInput = LoadSafePage.getByPlaceholderText('Name of the Safe') + const safeAddressInput = LoadSafePage.getByPlaceholderText('Safe Address*') // Fill Safe's name - const fieldName = inputs[0] - TestUtils.Simulate.change(fieldName, { target: { value: 'Adolfo Safe' } }) - const fieldAddress = inputs[1] - TestUtils.Simulate.change(fieldAddress, { target: { value: address } }) + fireEvent.change(safeNameInput, { target: { value: 'A Safe To Load' } }) + fireEvent.change(safeAddressInput, { target: { value: address } }) await sleep(400) // Click next - TestUtils.Simulate.submit(form) + fireEvent.submit(form) await sleep(400) // Submit - TestUtils.Simulate.submit(form) - await sleep(400) - + fireEvent.submit(form) const deployedAddress = await whenSafeDeployed() expect(deployedAddress).toBe(address) })