2023-03-23 01:17:19 +01:00
|
|
|
/* eslint-disable jest/require-top-level-describe */
|
2023-05-19 22:20:15 +02:00
|
|
|
import { render, fireEvent } from '@testing-library/react';
|
2023-03-23 01:17:19 +01:00
|
|
|
import React from 'react';
|
2023-05-19 22:20:15 +02:00
|
|
|
import { Modal } from '..';
|
2023-03-23 01:17:19 +01:00
|
|
|
import { ModalContent } from './modal-content';
|
|
|
|
import { ModalContentSize } from './modal-content.types';
|
|
|
|
|
|
|
|
describe('ModalContent', () => {
|
2023-05-19 22:20:15 +02:00
|
|
|
const onClose = jest.fn();
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
onClose.mockReset();
|
|
|
|
});
|
|
|
|
|
2023-03-23 01:17:19 +01:00
|
|
|
it('should render with text inside the ModalContent', () => {
|
2023-05-19 22:20:15 +02:00
|
|
|
const { getByText, getByTestId } = render(
|
|
|
|
<Modal isOpen onClose={onClose}>
|
|
|
|
<ModalContent data-testid="modal-content">test</ModalContent>
|
|
|
|
</Modal>,
|
|
|
|
);
|
2023-03-23 01:17:19 +01:00
|
|
|
expect(getByText('test')).toBeDefined();
|
2023-05-19 22:20:15 +02:00
|
|
|
expect(getByText('test')).toHaveClass('mm-modal-content__dialog');
|
|
|
|
expect(getByTestId('modal-content')).toHaveClass('mm-modal-content');
|
2023-03-23 01:17:19 +01:00
|
|
|
});
|
|
|
|
it('should match snapshot', () => {
|
2023-05-19 22:20:15 +02:00
|
|
|
const { getByTestId } = render(
|
|
|
|
<Modal isOpen onClose={onClose}>
|
|
|
|
<ModalContent data-testid="test">test</ModalContent>
|
|
|
|
</Modal>,
|
|
|
|
);
|
|
|
|
expect(getByTestId('test')).toMatchSnapshot();
|
2023-03-23 01:17:19 +01:00
|
|
|
});
|
|
|
|
it('should render with and additional className', () => {
|
2023-05-19 22:20:15 +02:00
|
|
|
const { getByTestId } = render(
|
|
|
|
<Modal isOpen onClose={onClose}>
|
|
|
|
<ModalContent data-testid="test" className="test-class">
|
|
|
|
test
|
|
|
|
</ModalContent>
|
|
|
|
,
|
|
|
|
</Modal>,
|
2023-03-23 01:17:19 +01:00
|
|
|
);
|
2023-05-19 22:20:15 +02:00
|
|
|
expect(getByTestId('test')).toHaveClass('test-class');
|
2023-03-23 01:17:19 +01:00
|
|
|
});
|
|
|
|
it('should render with size sm', () => {
|
|
|
|
const { getByText } = render(
|
|
|
|
<>
|
2023-05-19 22:20:15 +02:00
|
|
|
<Modal isOpen onClose={onClose}>
|
|
|
|
<ModalContent>default</ModalContent>
|
|
|
|
<ModalContent size={ModalContentSize.Sm}>sm</ModalContent>
|
|
|
|
</Modal>
|
2023-03-23 01:17:19 +01:00
|
|
|
</>,
|
|
|
|
);
|
2023-05-19 22:20:15 +02:00
|
|
|
expect(getByText('sm')).toHaveClass('mm-modal-content__dialog--size-sm');
|
|
|
|
expect(getByText('default')).toHaveClass(
|
|
|
|
'mm-modal-content__dialog--size-sm',
|
|
|
|
);
|
2023-03-23 01:17:19 +01:00
|
|
|
});
|
2023-05-19 22:20:15 +02:00
|
|
|
it('should render with additional props being passed to modalDialogProps', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<Modal isOpen onClose={onClose}>
|
|
|
|
<ModalContent
|
|
|
|
modalDialogProps={{ 'data-testid': 'test' }}
|
|
|
|
data-testid="modal-content"
|
|
|
|
>
|
|
|
|
test
|
|
|
|
</ModalContent>
|
|
|
|
</Modal>,
|
|
|
|
);
|
|
|
|
expect(getByTestId('test')).toBeDefined();
|
|
|
|
});
|
|
|
|
it('should close when escape key is pressed', () => {
|
|
|
|
const { getByRole } = render(
|
|
|
|
<Modal isOpen={true} onClose={onClose}>
|
|
|
|
<ModalContent>modal dialog</ModalContent>
|
|
|
|
</Modal>,
|
|
|
|
);
|
|
|
|
fireEvent.keyDown(getByRole('dialog'), { key: 'Escape' });
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not close when isClosedOnEscapeKey is false and escape key is pressed', () => {
|
|
|
|
const { getByRole } = render(
|
|
|
|
<Modal isOpen={true} onClose={onClose} isClosedOnEscapeKey={false}>
|
|
|
|
<ModalContent>modal dialog</ModalContent>
|
|
|
|
</Modal>,
|
|
|
|
);
|
|
|
|
fireEvent.keyDown(getByRole('dialog'), { key: 'Escape' });
|
|
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should close when clicked outside', () => {
|
|
|
|
const { getByRole } = render(
|
|
|
|
<Modal isOpen={true} onClose={onClose}>
|
|
|
|
<ModalContent data-testid="modal-dialog">modal dialog</ModalContent>
|
|
|
|
</Modal>,
|
|
|
|
);
|
|
|
|
// don't close when clicked inside
|
|
|
|
fireEvent.mouseDown(getByRole('dialog'));
|
|
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
|
|
// close when clicked outside
|
|
|
|
fireEvent.mouseDown(document.body);
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not close when isClosedOnOutsideClick is false and clicked outside', () => {
|
|
|
|
const ref: React.RefObject<HTMLDivElement> = React.createRef();
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<Modal isOpen={true} onClose={onClose} isClosedOnOutsideClick={false}>
|
|
|
|
<ModalContent data-testid="modal-dialog" ref={ref}>
|
|
|
|
modal dialog
|
|
|
|
</ModalContent>
|
|
|
|
</Modal>,
|
|
|
|
);
|
|
|
|
// don't close when clicked inside
|
|
|
|
fireEvent.mouseDown(getByTestId('modal-dialog'));
|
|
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
|
|
// don't close when clicked outside
|
|
|
|
fireEvent.mouseDown(document.body);
|
|
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should focus initial focus ref when autoFocus is false', () => {
|
|
|
|
const initialRef: React.RefObject<HTMLInputElement> = React.createRef();
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<Modal isOpen={true} onClose={onClose} initialFocusRef={initialRef}>
|
|
|
|
<ModalContent>
|
|
|
|
<button>modal dialog</button>
|
|
|
|
<input data-testid="input" ref={initialRef} />
|
|
|
|
</ModalContent>
|
|
|
|
</Modal>,
|
|
|
|
);
|
|
|
|
expect(getByTestId('input')).toHaveFocus();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should focus final focus ref when modal is closed', () => {
|
|
|
|
const finalRef: React.RefObject<HTMLButtonElement> = React.createRef();
|
|
|
|
const { rerender } = render(
|
|
|
|
<>
|
|
|
|
<button ref={finalRef}>button</button>
|
|
|
|
<Modal isOpen={true} onClose={onClose} finalFocusRef={finalRef}>
|
|
|
|
<ModalContent data-testid="modal-dialog">modal dialog</ModalContent>
|
|
|
|
</Modal>
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
rerender(
|
|
|
|
<>
|
|
|
|
<button ref={finalRef}>button</button>
|
|
|
|
<Modal isOpen={false} onClose={onClose} finalFocusRef={finalRef}>
|
|
|
|
<ModalContent data-testid="modal-dialog">modal dialog</ModalContent>
|
|
|
|
</Modal>
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
expect(finalRef.current).toHaveFocus();
|
2023-03-23 01:17:19 +01:00
|
|
|
});
|
|
|
|
});
|