2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
2022-09-23 16:41:35 +02:00
|
|
|
import { fireEvent } from '@testing-library/react';
|
|
|
|
import { renderWithProvider } from '../../../../test/lib/render-helpers';
|
2021-03-16 22:00:08 +01:00
|
|
|
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', () => {
|
2022-09-23 16:41:35 +02:00
|
|
|
const { container } = renderWithProvider(<Modal />);
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2022-09-23 16:41:35 +02:00
|
|
|
expect(container).toMatchSnapshot();
|
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', () => {
|
2022-09-23 16:41:35 +02:00
|
|
|
const props = {
|
|
|
|
onCancel: jest.fn(),
|
|
|
|
cancelText: 'Cancel',
|
|
|
|
onSubmit: jest.fn(),
|
|
|
|
submitText: 'Submit',
|
|
|
|
};
|
|
|
|
const { container, queryByText } = renderWithProvider(<Modal {...props} />);
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
|
|
|
const cancelButton = queryByText(props.cancelText);
|
|
|
|
const submitButton = queryByText(props.submitText);
|
|
|
|
|
|
|
|
expect(props.onCancel).not.toHaveBeenCalled();
|
|
|
|
fireEvent.click(cancelButton);
|
|
|
|
expect(props.onCancel).toHaveBeenCalled();
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2022-09-23 16:41:35 +02:00
|
|
|
expect(props.onSubmit).not.toHaveBeenCalled();
|
|
|
|
fireEvent.click(submitButton);
|
|
|
|
expect(props.onSubmit).toHaveBeenCalled();
|
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', () => {
|
2022-09-23 16:41:35 +02:00
|
|
|
const props = {
|
|
|
|
onCancel: () => undefined,
|
|
|
|
cancelText: 'Cancel',
|
|
|
|
cancelType: 'default',
|
|
|
|
onSubmit: () => undefined,
|
|
|
|
submitText: 'Submit',
|
|
|
|
submitType: 'confirm',
|
|
|
|
};
|
|
|
|
|
|
|
|
const { container } = renderWithProvider(<Modal {...props} />);
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2022-09-23 16:41:35 +02:00
|
|
|
expect(container).toMatchSnapshot();
|
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', () => {
|
2022-09-23 16:41:35 +02:00
|
|
|
const props = {
|
|
|
|
onCancel: () => undefined,
|
|
|
|
cancelText: 'Cancel',
|
|
|
|
onSubmit: () => undefined,
|
|
|
|
submitText: 'Submit',
|
|
|
|
};
|
|
|
|
const { container } = renderWithProvider(
|
|
|
|
<Modal {...props}>
|
2018-09-19 23:30:52 +02:00
|
|
|
<div className="test-child" />
|
2020-07-14 17:20:41 +02:00
|
|
|
</Modal>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-09-23 16:41:35 +02:00
|
|
|
expect(container).toMatchSnapshot();
|
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', () => {
|
2022-09-23 16:41:35 +02:00
|
|
|
const props = {
|
|
|
|
onCancel: jest.fn(),
|
|
|
|
cancelText: 'Cancel',
|
|
|
|
onSubmit: jest.fn(),
|
|
|
|
submitText: 'Submit',
|
|
|
|
headerText: 'My Header',
|
|
|
|
onClose: jest.fn(),
|
|
|
|
};
|
2018-09-19 23:30:52 +02:00
|
|
|
|
2022-09-23 16:41:35 +02:00
|
|
|
const { container, queryByTestId } = renderWithProvider(
|
|
|
|
<Modal {...props} />,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-09-23 16:41:35 +02:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
|
|
|
const modalClose = queryByTestId('modal-header-close');
|
|
|
|
fireEvent.click(modalClose);
|
|
|
|
expect(props.onClose).toHaveBeenCalled();
|
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', () => {
|
2022-09-23 16:41:35 +02:00
|
|
|
const props = {
|
|
|
|
onCancel: jest.fn(),
|
|
|
|
cancelText: 'Cancel',
|
|
|
|
onSubmit: jest.fn(),
|
|
|
|
submitText: 'Submit',
|
|
|
|
submitDisabled: true,
|
|
|
|
headerText: 'My Header',
|
|
|
|
onClose: jest.fn(),
|
|
|
|
};
|
|
|
|
const { queryByText } = renderWithProvider(<Modal {...props} />);
|
|
|
|
const submitButton = queryByText(props.submitText);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2022-09-23 16:41:35 +02:00
|
|
|
expect(submitButton).toHaveAttribute('disabled');
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2022-09-23 16:41:35 +02:00
|
|
|
fireEvent.click(submitButton);
|
|
|
|
expect(props.onSubmit).not.toHaveBeenCalled();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|