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,
|
||||
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 (
|
||||
<PageContainerFooter
|
||||
onCancel={() => this.onCancel()}
|
||||
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 { 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,
|
||||
|
@ -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(<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', () => {
|
||||
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)
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user