mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Move formShouldBeDisabled from send-footer util to component.
This commit is contained in:
parent
3a87d9221d
commit
dc2b5d0ef4
@ -17,6 +17,7 @@ export default class SendFooter extends Component {
|
|||||||
gasPrice: PropTypes.string,
|
gasPrice: PropTypes.string,
|
||||||
gasTotal: PropTypes.string,
|
gasTotal: PropTypes.string,
|
||||||
history: PropTypes.object,
|
history: PropTypes.object,
|
||||||
|
inError: PropTypes.bool,
|
||||||
selectedToken: PropTypes.object,
|
selectedToken: PropTypes.object,
|
||||||
sign: PropTypes.func,
|
sign: PropTypes.func,
|
||||||
to: PropTypes.string,
|
to: PropTypes.string,
|
||||||
@ -75,12 +76,18 @@ export default class SendFooter extends Component {
|
|||||||
this.props.history.push(CONFIRM_TRANSACTION_ROUTE)
|
this.props.history.push(CONFIRM_TRANSACTION_ROUTE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
formShouldBeDisabled () {
|
||||||
|
const { inError, selectedToken, tokenBalance, gasTotal } = this.props
|
||||||
|
const missingTokenBalance = selectedToken && !tokenBalance
|
||||||
|
return inError || !gasTotal || missingTokenBalance
|
||||||
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
return (
|
return (
|
||||||
<PageContainerFooter
|
<PageContainerFooter
|
||||||
onCancel={() => this.onCancel()}
|
onCancel={() => this.onCancel()}
|
||||||
onSubmit={e => this.onSubmit(e)}
|
onSubmit={e => this.onSubmit(e)}
|
||||||
disabled={this.props.disabled}
|
disabled={this.formShouldBeDisabled()}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,6 @@ const ethAbi = require('ethereumjs-abi')
|
|||||||
const ethUtil = require('ethereumjs-util')
|
const ethUtil = require('ethereumjs-util')
|
||||||
const { TOKEN_TRANSFER_FUNCTION_SIGNATURE } = require('../send.constants')
|
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) {
|
function addHexPrefixToObjectValues (obj) {
|
||||||
return Object.keys(obj).reduce((newObj, key) => {
|
return Object.keys(obj).reduce((newObj, key) => {
|
||||||
return { ...newObj, [key]: ethUtil.addHexPrefix(obj[key]) }
|
return { ...newObj, [key]: ethUtil.addHexPrefix(obj[key]) }
|
||||||
@ -81,7 +76,6 @@ function addressIsNew (toAccounts, newAddress) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
addressIsNew,
|
addressIsNew,
|
||||||
formShouldBeDisabled,
|
|
||||||
constructTxParams,
|
constructTxParams,
|
||||||
constructUpdatedTx,
|
constructUpdatedTx,
|
||||||
addHexPrefixToObjectValues,
|
addHexPrefixToObjectValues,
|
||||||
|
@ -37,6 +37,7 @@ describe('SendFooter Component', function () {
|
|||||||
gasPrice={'mockGasPrice'}
|
gasPrice={'mockGasPrice'}
|
||||||
gasTotal={'mockGasTotal'}
|
gasTotal={'mockGasTotal'}
|
||||||
history={historySpies}
|
history={historySpies}
|
||||||
|
inError={false}
|
||||||
selectedToken={{ mockProp: 'mockSelectedTokenProp' }}
|
selectedToken={{ mockProp: 'mockSelectedTokenProp' }}
|
||||||
sign={propsMethodSpies.sign}
|
sign={propsMethodSpies.sign}
|
||||||
to={'mockTo'}
|
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', () => {
|
describe('onSubmit', () => {
|
||||||
it('should call addToAddressBookIfNew with the correct params', () => {
|
it('should call addToAddressBookIfNew with the correct params', () => {
|
||||||
wrapper.instance().onSubmit(MOCK_EVENT)
|
wrapper.instance().onSubmit(MOCK_EVENT)
|
||||||
@ -134,6 +168,35 @@ describe('SendFooter Component', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('render', () => {
|
describe('render', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
sinon.stub(SendFooter.prototype, 'formShouldBeDisabled').returns('formShouldBeDisabledReturn')
|
||||||
|
wrapper = shallow(<SendFooter
|
||||||
|
addToAddressBookIfNew={propsMethodSpies.addToAddressBookIfNew}
|
||||||
|
amount={'mockAmount'}
|
||||||
|
clearSend={propsMethodSpies.clearSend}
|
||||||
|
disabled={true}
|
||||||
|
editingTransactionId={'mockEditingTransactionId'}
|
||||||
|
errors={{}}
|
||||||
|
from={ { address: 'mockAddress', balance: 'mockBalance' } }
|
||||||
|
gasLimit={'mockGasLimit'}
|
||||||
|
gasPrice={'mockGasPrice'}
|
||||||
|
gasTotal={'mockGasTotal'}
|
||||||
|
history={historySpies}
|
||||||
|
inError={false}
|
||||||
|
selectedToken={{ mockProp: 'mockSelectedTokenProp' }}
|
||||||
|
sign={propsMethodSpies.sign}
|
||||||
|
to={'mockTo'}
|
||||||
|
toAccounts={['mockAccount']}
|
||||||
|
tokenBalance={'mockTokenBalance'}
|
||||||
|
unapprovedTxs={['mockTx']}
|
||||||
|
update={propsMethodSpies.update}
|
||||||
|
/>, { context: { t: str => str } })
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
SendFooter.prototype.formShouldBeDisabled.restore()
|
||||||
|
})
|
||||||
|
|
||||||
it('should render a PageContainerFooter component', () => {
|
it('should render a PageContainerFooter component', () => {
|
||||||
assert.equal(wrapper.find(PageContainerFooter).length, 1)
|
assert.equal(wrapper.find(PageContainerFooter).length, 1)
|
||||||
})
|
})
|
||||||
@ -144,7 +207,7 @@ describe('SendFooter Component', function () {
|
|||||||
onSubmit,
|
onSubmit,
|
||||||
disabled,
|
disabled,
|
||||||
} = wrapper.find(PageContainerFooter).props()
|
} = wrapper.find(PageContainerFooter).props()
|
||||||
assert.equal(disabled, true)
|
assert.equal(disabled, 'formShouldBeDisabledReturn')
|
||||||
|
|
||||||
assert.equal(SendFooter.prototype.onSubmit.callCount, 0)
|
assert.equal(SendFooter.prototype.onSubmit.callCount, 0)
|
||||||
onSubmit(MOCK_EVENT)
|
onSubmit(MOCK_EVENT)
|
||||||
|
@ -16,7 +16,6 @@ const sendUtils = proxyquire('../send-footer.utils.js', {
|
|||||||
})
|
})
|
||||||
const {
|
const {
|
||||||
addressIsNew,
|
addressIsNew,
|
||||||
formShouldBeDisabled,
|
|
||||||
constructTxParams,
|
constructTxParams,
|
||||||
constructUpdatedTx,
|
constructUpdatedTx,
|
||||||
addHexPrefixToObjectValues,
|
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()', () => {
|
describe('constructTxParams()', () => {
|
||||||
it('should return a new txParams object with value and to properties if there is no selectedToken', () => {
|
it('should return a new txParams object with value and to properties if there is no selectedToken', () => {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
|
Loading…
Reference in New Issue
Block a user