2018-05-07 14:03:20 +02:00
|
|
|
import assert from 'assert'
|
2020-08-18 21:18:25 +02:00
|
|
|
import React from 'react'
|
2018-05-07 14:03:20 +02:00
|
|
|
import { shallow } from 'enzyme'
|
|
|
|
import sinon from 'sinon'
|
2020-06-01 19:54:32 +02:00
|
|
|
import { CONFIRM_TRANSACTION_ROUTE } from '../../../../helpers/constants/routes'
|
2020-08-14 13:46:45 +02:00
|
|
|
import SendFooter from '../send-footer.component'
|
2019-04-17 21:15:13 +02:00
|
|
|
import PageContainerFooter from '../../../../components/ui/page-container/page-container-footer'
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2018-05-10 13:37:33 +02:00
|
|
|
describe('SendFooter Component', function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
let wrapper
|
|
|
|
|
2020-02-11 21:33:32 +01:00
|
|
|
const propsMethodSpies = {
|
|
|
|
addToAddressBookIfNew: sinon.spy(),
|
|
|
|
clearSend: sinon.spy(),
|
|
|
|
sign: sinon.spy(),
|
|
|
|
update: sinon.spy(),
|
|
|
|
}
|
|
|
|
const historySpies = {
|
|
|
|
push: sinon.spy(),
|
|
|
|
}
|
2020-08-14 13:47:02 +02:00
|
|
|
const MOCK_EVENT = { preventDefault: () => undefined }
|
2020-02-11 21:33:32 +01:00
|
|
|
|
|
|
|
before(function () {
|
|
|
|
sinon.spy(SendFooter.prototype, 'onCancel')
|
|
|
|
sinon.spy(SendFooter.prototype, 'onSubmit')
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
beforeEach(function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
wrapper = shallow(
|
2019-12-03 17:35:44 +01:00
|
|
|
<SendFooter
|
|
|
|
addToAddressBookIfNew={propsMethodSpies.addToAddressBookIfNew}
|
|
|
|
amount="mockAmount"
|
|
|
|
clearSend={propsMethodSpies.clearSend}
|
|
|
|
disabled
|
|
|
|
editingTransactionId="mockEditingTransactionId"
|
|
|
|
errors={{}}
|
2020-11-03 00:41:28 +01:00
|
|
|
from={{ address: 'mockAddress', balance: 'mockBalance' }}
|
2019-12-03 17:35:44 +01:00
|
|
|
gasLimit="mockGasLimit"
|
|
|
|
gasPrice="mockGasPrice"
|
|
|
|
gasTotal="mockGasTotal"
|
|
|
|
history={historySpies}
|
|
|
|
inError={false}
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken={{ mockProp: 'mockSendTokenProp' }}
|
2019-12-03 17:35:44 +01:00
|
|
|
sign={propsMethodSpies.sign}
|
|
|
|
to="mockTo"
|
|
|
|
toAccounts={['mockAccount']}
|
|
|
|
tokenBalance="mockTokenBalance"
|
2020-01-15 17:58:23 +01:00
|
|
|
unapprovedTxs={{}}
|
2019-12-03 17:35:44 +01:00
|
|
|
update={propsMethodSpies.update}
|
|
|
|
sendErrors={{}}
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage="mostRecentOverviewPage"
|
2020-11-03 00:41:28 +01:00
|
|
|
/>,
|
|
|
|
{ context: { t: (str) => str, metricsEvent: () => ({}) } },
|
|
|
|
)
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
afterEach(function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
propsMethodSpies.clearSend.resetHistory()
|
|
|
|
propsMethodSpies.addToAddressBookIfNew.resetHistory()
|
|
|
|
propsMethodSpies.clearSend.resetHistory()
|
|
|
|
propsMethodSpies.sign.resetHistory()
|
|
|
|
propsMethodSpies.update.resetHistory()
|
|
|
|
historySpies.push.resetHistory()
|
|
|
|
SendFooter.prototype.onCancel.resetHistory()
|
|
|
|
SendFooter.prototype.onSubmit.resetHistory()
|
|
|
|
})
|
|
|
|
|
2020-02-11 21:33:32 +01:00
|
|
|
after(function () {
|
|
|
|
sinon.restore()
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('onCancel', function () {
|
|
|
|
it('should call clearSend', function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
assert.equal(propsMethodSpies.clearSend.callCount, 0)
|
|
|
|
wrapper.instance().onCancel()
|
|
|
|
assert.equal(propsMethodSpies.clearSend.callCount, 1)
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should call history.push', function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
assert.equal(historySpies.push.callCount, 0)
|
|
|
|
wrapper.instance().onCancel()
|
|
|
|
assert.equal(historySpies.push.callCount, 1)
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
historySpies.push.getCall(0).args[0],
|
|
|
|
'mostRecentOverviewPage',
|
|
|
|
)
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('formShouldBeDisabled()', function () {
|
2018-05-25 02:53:54 +02:00
|
|
|
const config = {
|
|
|
|
'should return true if inError is truthy': {
|
|
|
|
inError: true,
|
|
|
|
expectedResult: true,
|
2020-01-10 15:23:54 +01:00
|
|
|
gasIsLoading: false,
|
2018-05-25 02:53:54 +02:00
|
|
|
},
|
|
|
|
'should return true if gasTotal is falsy': {
|
|
|
|
inError: false,
|
2020-01-15 17:58:23 +01:00
|
|
|
gasTotal: '',
|
2018-05-25 02:53:54 +02:00
|
|
|
expectedResult: true,
|
2020-01-10 15:23:54 +01:00
|
|
|
gasIsLoading: false,
|
2018-05-25 02:53:54 +02:00
|
|
|
},
|
2018-05-25 19:09:31 +02:00
|
|
|
'should return true if to is truthy': {
|
|
|
|
to: '0xsomevalidAddress',
|
|
|
|
inError: false,
|
2020-01-15 17:58:23 +01:00
|
|
|
gasTotal: '',
|
2018-05-25 19:09:31 +02:00
|
|
|
expectedResult: true,
|
2020-01-10 15:23:54 +01:00
|
|
|
gasIsLoading: false,
|
2018-05-25 19:09:31 +02:00
|
|
|
},
|
2020-05-29 19:46:10 +02:00
|
|
|
'should return true if sendToken is truthy and tokenBalance is falsy': {
|
|
|
|
sendToken: { mockProp: 'mockSendTokenProp' },
|
2020-01-15 17:58:23 +01:00
|
|
|
tokenBalance: '',
|
2018-05-25 02:53:54 +02:00
|
|
|
expectedResult: true,
|
2020-01-10 15:23:54 +01:00
|
|
|
gasIsLoading: false,
|
|
|
|
},
|
|
|
|
'should return true if gasIsLoading is truthy but all other params are falsy': {
|
|
|
|
inError: false,
|
2020-01-15 17:58:23 +01:00
|
|
|
gasTotal: '',
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: null,
|
2020-01-15 17:58:23 +01:00
|
|
|
tokenBalance: '',
|
2020-01-10 15:23:54 +01:00
|
|
|
expectedResult: true,
|
|
|
|
gasIsLoading: true,
|
2018-05-25 02:53:54 +02:00
|
|
|
},
|
|
|
|
'should return false if inError is false and all other params are truthy': {
|
|
|
|
inError: false,
|
|
|
|
gasTotal: '0x123',
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: { mockProp: 'mockSendTokenProp' },
|
2020-01-15 17:58:23 +01:00
|
|
|
tokenBalance: '123',
|
2018-05-25 02:53:54 +02:00
|
|
|
expectedResult: false,
|
2020-01-10 15:23:54 +01:00
|
|
|
gasIsLoading: false,
|
2018-05-25 02:53:54 +02:00
|
|
|
},
|
|
|
|
}
|
2020-07-15 16:36:52 +02:00
|
|
|
Object.entries(config).forEach(([description, obj]) => {
|
2020-02-11 17:51:13 +01:00
|
|
|
it(description, function () {
|
2018-05-25 02:53:54 +02:00
|
|
|
wrapper.setProps(obj)
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
wrapper.instance().formShouldBeDisabled(),
|
|
|
|
obj.expectedResult,
|
|
|
|
)
|
2018-05-25 02:53:54 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('onSubmit', function () {
|
|
|
|
it('should call addToAddressBookIfNew with the correct params', function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.instance().onSubmit(MOCK_EVENT)
|
|
|
|
assert(propsMethodSpies.addToAddressBookIfNew.calledOnce)
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.deepEqual(propsMethodSpies.addToAddressBookIfNew.getCall(0).args, [
|
|
|
|
'mockTo',
|
|
|
|
['mockAccount'],
|
|
|
|
])
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
|
2020-04-29 06:59:49 +02:00
|
|
|
it('should call props.update if editingTransactionId is truthy', async function () {
|
|
|
|
await wrapper.instance().onSubmit(MOCK_EVENT)
|
2018-05-07 14:03:20 +02:00
|
|
|
assert(propsMethodSpies.update.calledOnce)
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.deepEqual(propsMethodSpies.update.getCall(0).args[0], {
|
|
|
|
data: undefined,
|
|
|
|
amount: 'mockAmount',
|
|
|
|
editingTransactionId: 'mockEditingTransactionId',
|
|
|
|
from: 'mockAddress',
|
|
|
|
gas: 'mockGasLimit',
|
|
|
|
gasPrice: 'mockGasPrice',
|
|
|
|
sendToken: { mockProp: 'mockSendTokenProp' },
|
|
|
|
to: 'mockTo',
|
|
|
|
unapprovedTxs: {},
|
|
|
|
})
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should not call props.sign if editingTransactionId is truthy', function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
assert.equal(propsMethodSpies.sign.callCount, 0)
|
|
|
|
})
|
|
|
|
|
2020-04-29 06:59:49 +02:00
|
|
|
it('should call props.sign if editingTransactionId is falsy', async function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.setProps({ editingTransactionId: null })
|
2020-04-29 06:59:49 +02:00
|
|
|
await wrapper.instance().onSubmit(MOCK_EVENT)
|
2018-05-07 14:03:20 +02:00
|
|
|
assert(propsMethodSpies.sign.calledOnce)
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.deepEqual(propsMethodSpies.sign.getCall(0).args[0], {
|
|
|
|
data: undefined,
|
|
|
|
amount: 'mockAmount',
|
|
|
|
from: 'mockAddress',
|
|
|
|
gas: 'mockGasLimit',
|
|
|
|
gasPrice: 'mockGasPrice',
|
|
|
|
sendToken: { mockProp: 'mockSendTokenProp' },
|
|
|
|
to: 'mockTo',
|
|
|
|
})
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should not call props.update if editingTransactionId is falsy', function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
assert.equal(propsMethodSpies.update.callCount, 0)
|
|
|
|
})
|
|
|
|
|
2020-04-29 06:59:49 +02:00
|
|
|
it('should call history.push', async function () {
|
|
|
|
await wrapper.instance().onSubmit(MOCK_EVENT)
|
|
|
|
assert.equal(historySpies.push.callCount, 1)
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
historySpies.push.getCall(0).args[0],
|
|
|
|
CONFIRM_TRANSACTION_ROUTE,
|
|
|
|
)
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('render', function () {
|
|
|
|
beforeEach(function () {
|
2020-01-15 17:58:23 +01:00
|
|
|
sinon.stub(SendFooter.prototype, 'formShouldBeDisabled').returns(true)
|
2020-11-03 00:41:28 +01:00
|
|
|
wrapper = shallow(
|
2019-12-03 17:35:44 +01:00
|
|
|
<SendFooter
|
|
|
|
addToAddressBookIfNew={propsMethodSpies.addToAddressBookIfNew}
|
|
|
|
amount="mockAmount"
|
|
|
|
clearSend={propsMethodSpies.clearSend}
|
|
|
|
disabled
|
|
|
|
editingTransactionId="mockEditingTransactionId"
|
|
|
|
errors={{}}
|
2020-11-03 00:41:28 +01:00
|
|
|
from={{ address: 'mockAddress', balance: 'mockBalance' }}
|
2019-12-03 17:35:44 +01:00
|
|
|
gasLimit="mockGasLimit"
|
|
|
|
gasPrice="mockGasPrice"
|
|
|
|
gasTotal="mockGasTotal"
|
|
|
|
history={historySpies}
|
|
|
|
inError={false}
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken={{ mockProp: 'mockSendTokenProp' }}
|
2019-12-03 17:35:44 +01:00
|
|
|
sign={propsMethodSpies.sign}
|
|
|
|
to="mockTo"
|
|
|
|
toAccounts={['mockAccount']}
|
|
|
|
tokenBalance="mockTokenBalance"
|
2020-01-15 17:58:23 +01:00
|
|
|
unapprovedTxs={{}}
|
2019-12-03 17:35:44 +01:00
|
|
|
update={propsMethodSpies.update}
|
2020-11-03 00:41:28 +01:00
|
|
|
/>,
|
|
|
|
{ context: { t: (str) => str, metricsEvent: () => ({}) } },
|
|
|
|
)
|
2018-05-25 02:53:54 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
afterEach(function () {
|
2018-05-25 02:53:54 +02:00
|
|
|
SendFooter.prototype.formShouldBeDisabled.restore()
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should render a PageContainerFooter component', function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
assert.equal(wrapper.find(PageContainerFooter).length, 1)
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should pass the correct props to PageContainerFooter', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { onCancel, onSubmit, disabled } = wrapper
|
|
|
|
.find(PageContainerFooter)
|
|
|
|
.props()
|
2020-01-15 17:58:23 +01:00
|
|
|
assert.equal(disabled, true)
|
2018-05-07 14:03:20 +02:00
|
|
|
|
|
|
|
assert.equal(SendFooter.prototype.onSubmit.callCount, 0)
|
|
|
|
onSubmit(MOCK_EVENT)
|
|
|
|
assert.equal(SendFooter.prototype.onSubmit.callCount, 1)
|
|
|
|
|
|
|
|
assert.equal(SendFooter.prototype.onCancel.callCount, 0)
|
|
|
|
onCancel()
|
|
|
|
assert.equal(SendFooter.prototype.onCancel.callCount, 1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|