2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import sinon from 'sinon';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { CONFIRM_TRANSACTION_ROUTE } from '../../../helpers/constants/routes';
|
|
|
|
import PageContainerFooter from '../../../components/ui/page-container/page-container-footer';
|
|
|
|
import SendFooter from './send-footer.component';
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('SendFooter Component', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
let wrapper;
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2020-02-11 21:33:32 +01:00
|
|
|
const propsMethodSpies = {
|
|
|
|
addToAddressBookIfNew: sinon.spy(),
|
|
|
|
clearSend: sinon.spy(),
|
|
|
|
sign: sinon.spy(),
|
|
|
|
update: sinon.spy(),
|
2021-04-15 20:01:46 +02:00
|
|
|
mostRecentOverviewPage: '/',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-02-11 21:33:32 +01:00
|
|
|
const historySpies = {
|
|
|
|
push: sinon.spy(),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const MOCK_EVENT = { preventDefault: () => undefined };
|
2020-02-11 21:33:32 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.spy(SendFooter.prototype, 'onCancel');
|
|
|
|
sinon.spy(SendFooter.prototype, 'onSubmit');
|
|
|
|
});
|
2020-02-11 21:33:32 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeEach(() => {
|
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"
|
2021-04-28 20:02:01 +02:00
|
|
|
noGasPrice={false}
|
2020-11-03 00:41:28 +01:00
|
|
|
/>,
|
|
|
|
{ context: { t: (str) => str, metricsEvent: () => ({}) } },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
afterEach(() => {
|
2021-02-04 19:15:23 +01: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();
|
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
afterAll(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.restore();
|
|
|
|
});
|
2020-02-11 21:33:32 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('onCancel', () => {
|
|
|
|
it('should call clearSend', () => {
|
|
|
|
expect(propsMethodSpies.clearSend.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
wrapper.instance().onCancel();
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(propsMethodSpies.clearSend.callCount).toStrictEqual(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should call history.push', () => {
|
|
|
|
expect(historySpies.push.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
wrapper.instance().onCancel();
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(historySpies.push.callCount).toStrictEqual(1);
|
|
|
|
expect(historySpies.push.getCall(0).args[0]).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
'mostRecentOverviewPage',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('formShouldBeDisabled()', () => {
|
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
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-07-15 16:36:52 +02:00
|
|
|
Object.entries(config).forEach(([description, obj]) => {
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`${description}`, () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
wrapper.setProps(obj);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper.instance().formShouldBeDisabled()).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
obj.expectedResult,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-05-25 02:53:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('onSubmit', () => {
|
|
|
|
it('should call addToAddressBookIfNew with the correct params', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
wrapper.instance().onSubmit(MOCK_EVENT);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(propsMethodSpies.addToAddressBookIfNew.calledOnce).toStrictEqual(
|
|
|
|
true,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(
|
|
|
|
propsMethodSpies.addToAddressBookIfNew.getCall(0).args,
|
|
|
|
).toStrictEqual(['mockTo', ['mockAccount']]);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should call props.update if editingTransactionId is truthy', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await wrapper.instance().onSubmit(MOCK_EVENT);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(propsMethodSpies.update.calledOnce).toStrictEqual(true);
|
|
|
|
expect(propsMethodSpies.update.getCall(0).args[0]).toStrictEqual({
|
2020-11-03 00:41:28 +01:00
|
|
|
data: undefined,
|
|
|
|
amount: 'mockAmount',
|
|
|
|
editingTransactionId: 'mockEditingTransactionId',
|
|
|
|
from: 'mockAddress',
|
|
|
|
gas: 'mockGasLimit',
|
|
|
|
gasPrice: 'mockGasPrice',
|
|
|
|
sendToken: { mockProp: 'mockSendTokenProp' },
|
|
|
|
to: 'mockTo',
|
|
|
|
unapprovedTxs: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should not call props.sign if editingTransactionId is truthy', () => {
|
|
|
|
expect(propsMethodSpies.sign.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should call props.sign if editingTransactionId is falsy', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
wrapper.setProps({ editingTransactionId: null });
|
|
|
|
await wrapper.instance().onSubmit(MOCK_EVENT);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(propsMethodSpies.sign.calledOnce).toStrictEqual(true);
|
|
|
|
expect(propsMethodSpies.sign.getCall(0).args[0]).toStrictEqual({
|
2020-11-03 00:41:28 +01:00
|
|
|
data: undefined,
|
|
|
|
amount: 'mockAmount',
|
|
|
|
from: 'mockAddress',
|
|
|
|
gas: 'mockGasLimit',
|
|
|
|
gasPrice: 'mockGasPrice',
|
|
|
|
sendToken: { mockProp: 'mockSendTokenProp' },
|
|
|
|
to: 'mockTo',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should not call props.update if editingTransactionId is falsy', () => {
|
|
|
|
expect(propsMethodSpies.update.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should call history.push', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await wrapper.instance().onSubmit(MOCK_EVENT);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(historySpies.push.callCount).toStrictEqual(1);
|
|
|
|
expect(historySpies.push.getCall(0).args[0]).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
CONFIRM_TRANSACTION_ROUTE,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('render', () => {
|
|
|
|
beforeEach(() => {
|
2021-02-04 19:15: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}
|
2021-04-15 20:01:46 +02:00
|
|
|
mostRecentOverviewPage="mostRecentOverviewPage"
|
2020-11-03 00:41:28 +01:00
|
|
|
/>,
|
|
|
|
{ context: { t: (str) => str, metricsEvent: () => ({}) } },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-05-25 02:53:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
afterEach(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
SendFooter.prototype.formShouldBeDisabled.restore();
|
|
|
|
});
|
2018-05-25 02:53:54 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should render a PageContainerFooter component', () => {
|
|
|
|
expect(wrapper.find(PageContainerFooter)).toHaveLength(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-07 14:03:20 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should pass the correct props to PageContainerFooter', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { onCancel, onSubmit, disabled } = wrapper
|
|
|
|
.find(PageContainerFooter)
|
2021-02-04 19:15:23 +01:00
|
|
|
.props();
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(disabled).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(SendFooter.prototype.onSubmit.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
onSubmit(MOCK_EVENT);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(SendFooter.prototype.onSubmit.callCount).toStrictEqual(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(SendFooter.prototype.onCancel.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
onCancel();
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(SendFooter.prototype.onCancel.callCount).toStrictEqual(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|