2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { mount, shallow } from 'enzyme';
|
|
|
|
import sinon from 'sinon';
|
2021-03-16 22:00:08 +01:00
|
|
|
import Button from '../../ui/button';
|
|
|
|
import Modal from './modal.component';
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Modal Component', () => {
|
|
|
|
it('should render a modal with a submit button', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const wrapper = shallow(<Modal />);
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper.find('.modal-container')).toHaveLength(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
const buttons = wrapper.find(Button);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(buttons).toHaveLength(1);
|
|
|
|
expect(buttons.at(0).props().type).toStrictEqual('secondary');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should render a modal with a cancel and a submit button', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const handleCancel = sinon.spy();
|
|
|
|
const handleSubmit = sinon.spy();
|
2018-09-19 23:30:52 +02:00
|
|
|
const wrapper = shallow(
|
|
|
|
<Modal
|
|
|
|
onCancel={handleCancel}
|
|
|
|
cancelText="Cancel"
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
submitText="Submit"
|
2020-07-14 17:20:41 +02:00
|
|
|
/>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const buttons = wrapper.find(Button);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(buttons).toHaveLength(2);
|
2021-02-04 19:15:23 +01:00
|
|
|
const cancelButton = buttons.at(0);
|
|
|
|
const submitButton = buttons.at(1);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(cancelButton.props().type).toStrictEqual('default');
|
|
|
|
expect(cancelButton.props().children).toStrictEqual('Cancel');
|
|
|
|
expect(handleCancel.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
cancelButton.simulate('click');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleCancel.callCount).toStrictEqual(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(submitButton.props().type).toStrictEqual('secondary');
|
|
|
|
expect(submitButton.props().children).toStrictEqual('Submit');
|
|
|
|
expect(handleSubmit.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
submitButton.simulate('click');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleSubmit.callCount).toStrictEqual(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should render a modal with different button types', () => {
|
2018-09-19 23:30:52 +02:00
|
|
|
const wrapper = shallow(
|
|
|
|
<Modal
|
2020-08-14 13:47:02 +02:00
|
|
|
onCancel={() => undefined}
|
2018-09-19 23:30:52 +02:00
|
|
|
cancelText="Cancel"
|
|
|
|
cancelType="secondary"
|
2020-08-14 13:47:02 +02:00
|
|
|
onSubmit={() => undefined}
|
2018-09-19 23:30:52 +02:00
|
|
|
submitText="Submit"
|
|
|
|
submitType="confirm"
|
2020-07-14 17:20:41 +02:00
|
|
|
/>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const buttons = wrapper.find(Button);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(buttons).toHaveLength(2);
|
|
|
|
expect(buttons.at(0).props().type).toStrictEqual('secondary');
|
|
|
|
expect(buttons.at(1).props().type).toStrictEqual('confirm');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should render a modal with children', () => {
|
2018-09-19 23:30:52 +02:00
|
|
|
const wrapper = shallow(
|
|
|
|
<Modal
|
2020-08-14 13:47:02 +02:00
|
|
|
onCancel={() => undefined}
|
2018-09-19 23:30:52 +02:00
|
|
|
cancelText="Cancel"
|
2020-08-14 13:47:02 +02:00
|
|
|
onSubmit={() => undefined}
|
2018-09-19 23:30:52 +02:00
|
|
|
submitText="Submit"
|
|
|
|
>
|
|
|
|
<div className="test-child" />
|
2020-07-14 17:20:41 +02:00
|
|
|
</Modal>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper.find('.test-child')).toHaveLength(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should render a modal with a header', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const handleCancel = sinon.spy();
|
|
|
|
const handleSubmit = sinon.spy();
|
2018-09-19 23:30:52 +02:00
|
|
|
const wrapper = shallow(
|
|
|
|
<Modal
|
|
|
|
onCancel={handleCancel}
|
|
|
|
cancelText="Cancel"
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
submitText="Submit"
|
|
|
|
headerText="My Header"
|
2018-09-21 04:35:45 +02:00
|
|
|
onClose={handleCancel}
|
2020-07-14 17:20:41 +02:00
|
|
|
/>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(wrapper.find('.modal-container__header')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.modal-container__header-text').text()).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
'My Header',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleCancel.callCount).toStrictEqual(0);
|
|
|
|
expect(handleSubmit.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
wrapper.find('.modal-container__header-close').simulate('click');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleCancel.callCount).toStrictEqual(1);
|
|
|
|
expect(handleSubmit.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-12-12 00:04:57 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should disable the submit button if submitDisabled is true', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const handleCancel = sinon.spy();
|
|
|
|
const handleSubmit = sinon.spy();
|
2018-12-12 00:04:57 +01:00
|
|
|
const wrapper = mount(
|
|
|
|
<Modal
|
|
|
|
onCancel={handleCancel}
|
|
|
|
cancelText="Cancel"
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
submitText="Submit"
|
2019-11-19 00:23:41 +01:00
|
|
|
submitDisabled
|
2018-12-12 00:04:57 +01:00
|
|
|
headerText="My Header"
|
|
|
|
onClose={handleCancel}
|
2020-07-14 17:20:41 +02:00
|
|
|
/>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const buttons = wrapper.find(Button);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(buttons).toHaveLength(2);
|
2021-02-04 19:15:23 +01:00
|
|
|
const cancelButton = buttons.at(0);
|
|
|
|
const submitButton = buttons.at(1);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleCancel.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
cancelButton.simulate('click');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleCancel.callCount).toStrictEqual(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(submitButton.props().disabled).toStrictEqual(true);
|
|
|
|
expect(handleSubmit.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
submitButton.simulate('click');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(handleSubmit.callCount).toStrictEqual(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|