From dc2b5d0ef47be2125e58018470539d65d0d64c75 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 24 May 2018 22:23:54 -0230 Subject: [PATCH] Move formShouldBeDisabled from send-footer util to component. --- .../send-footer/send-footer.component.js | 9 ++- .../send_/send-footer/send-footer.utils.js | 6 -- .../tests/send-footer-component.test.js | 65 ++++++++++++++++++- .../tests/send-footer-utils.test.js | 32 --------- 4 files changed, 72 insertions(+), 40 deletions(-) diff --git a/ui/app/components/send_/send-footer/send-footer.component.js b/ui/app/components/send_/send-footer/send-footer.component.js index de2a885f0..7ff44c9d5 100644 --- a/ui/app/components/send_/send-footer/send-footer.component.js +++ b/ui/app/components/send_/send-footer/send-footer.component.js @@ -17,6 +17,7 @@ export default class SendFooter extends Component { gasPrice: PropTypes.string, gasTotal: PropTypes.string, history: PropTypes.object, + inError: PropTypes.bool, selectedToken: PropTypes.object, sign: PropTypes.func, to: PropTypes.string, @@ -75,12 +76,18 @@ export default class SendFooter extends Component { this.props.history.push(CONFIRM_TRANSACTION_ROUTE) } + formShouldBeDisabled () { + const { inError, selectedToken, tokenBalance, gasTotal } = this.props + const missingTokenBalance = selectedToken && !tokenBalance + return inError || !gasTotal || missingTokenBalance + } + render () { return ( this.onCancel()} onSubmit={e => this.onSubmit(e)} - disabled={this.props.disabled} + disabled={this.formShouldBeDisabled()} /> ) } diff --git a/ui/app/components/send_/send-footer/send-footer.utils.js b/ui/app/components/send_/send-footer/send-footer.utils.js index 149d9e357..d5639629d 100644 --- a/ui/app/components/send_/send-footer/send-footer.utils.js +++ b/ui/app/components/send_/send-footer/send-footer.utils.js @@ -2,11 +2,6 @@ const ethAbi = require('ethereumjs-abi') const ethUtil = require('ethereumjs-util') const { TOKEN_TRANSFER_FUNCTION_SIGNATURE } = require('../send.constants') -function formShouldBeDisabled ({ inError, selectedToken, tokenBalance, gasTotal }) { - const missingTokenBalance = selectedToken && !tokenBalance - return inError || !gasTotal || missingTokenBalance -} - function addHexPrefixToObjectValues (obj) { return Object.keys(obj).reduce((newObj, key) => { return { ...newObj, [key]: ethUtil.addHexPrefix(obj[key]) } @@ -81,7 +76,6 @@ function addressIsNew (toAccounts, newAddress) { module.exports = { addressIsNew, - formShouldBeDisabled, constructTxParams, constructUpdatedTx, addHexPrefixToObjectValues, diff --git a/ui/app/components/send_/send-footer/tests/send-footer-component.test.js b/ui/app/components/send_/send-footer/tests/send-footer-component.test.js index c0b8f956f..a74b6195c 100644 --- a/ui/app/components/send_/send-footer/tests/send-footer-component.test.js +++ b/ui/app/components/send_/send-footer/tests/send-footer-component.test.js @@ -37,6 +37,7 @@ describe('SendFooter Component', function () { gasPrice={'mockGasPrice'} gasTotal={'mockGasTotal'} history={historySpies} + inError={false} selectedToken={{ mockProp: 'mockSelectedTokenProp' }} sign={propsMethodSpies.sign} to={'mockTo'} @@ -73,6 +74,39 @@ describe('SendFooter Component', function () { }) }) + + describe('formShouldBeDisabled()', () => { + const config = { + 'should return true if inError is truthy': { + inError: true, + expectedResult: true, + }, + 'should return true if gasTotal is falsy': { + inError: false, + gasTotal: false, + expectedResult: true, + }, + 'should return true if selectedToken is truthy and tokenBalance is falsy': { + selectedToken: true, + tokenBalance: null, + expectedResult: true, + }, + 'should return false if inError is false and all other params are truthy': { + inError: false, + gasTotal: '0x123', + selectedToken: true, + tokenBalance: 123, + expectedResult: false, + }, + } + Object.entries(config).map(([description, obj]) => { + it(description, () => { + wrapper.setProps(obj) + assert.equal(wrapper.instance().formShouldBeDisabled(), obj.expectedResult) + }) + }) + }) + describe('onSubmit', () => { it('should call addToAddressBookIfNew with the correct params', () => { wrapper.instance().onSubmit(MOCK_EVENT) @@ -134,6 +168,35 @@ describe('SendFooter Component', function () { }) describe('render', () => { + beforeEach(() => { + sinon.stub(SendFooter.prototype, 'formShouldBeDisabled').returns('formShouldBeDisabledReturn') + wrapper = shallow(, { context: { t: str => str } }) + }) + + afterEach(() => { + SendFooter.prototype.formShouldBeDisabled.restore() + }) + it('should render a PageContainerFooter component', () => { assert.equal(wrapper.find(PageContainerFooter).length, 1) }) @@ -144,7 +207,7 @@ describe('SendFooter Component', function () { onSubmit, disabled, } = wrapper.find(PageContainerFooter).props() - assert.equal(disabled, true) + assert.equal(disabled, 'formShouldBeDisabledReturn') assert.equal(SendFooter.prototype.onSubmit.callCount, 0) onSubmit(MOCK_EVENT) diff --git a/ui/app/components/send_/send-footer/tests/send-footer-utils.test.js b/ui/app/components/send_/send-footer/tests/send-footer-utils.test.js index b235ea5e5..2d3135995 100644 --- a/ui/app/components/send_/send-footer/tests/send-footer-utils.test.js +++ b/ui/app/components/send_/send-footer/tests/send-footer-utils.test.js @@ -16,7 +16,6 @@ const sendUtils = proxyquire('../send-footer.utils.js', { }) const { addressIsNew, - formShouldBeDisabled, constructTxParams, constructUpdatedTx, addHexPrefixToObjectValues, @@ -65,37 +64,6 @@ describe('send-footer utils', () => { }) }) - describe('formShouldBeDisabled()', () => { - const config = { - 'should return true if inError is truthy': { - inError: true, - expectedResult: true, - }, - 'should return true if gasTotal is falsy': { - inError: false, - gasTotal: false, - expectedResult: true, - }, - 'should return true if selectedToken is truthy and tokenBalance is falsy': { - selectedToken: true, - tokenBalance: null, - expectedResult: true, - }, - 'should return false if inError is false and all other params are truthy': { - inError: false, - gasTotal: '0x123', - selectedToken: true, - tokenBalance: 123, - expectedResult: false, - }, - } - Object.entries(config).map(([description, obj]) => { - it(description, () => { - assert.equal(formShouldBeDisabled(obj), obj.expectedResult) - }) - }) - }) - describe('constructTxParams()', () => { it('should return a new txParams object with value and to properties if there is no selectedToken', () => { assert.deepEqual(