/* eslint-disable jest/require-top-level-describe */ import { render, fireEvent } from '@testing-library/react'; import React from 'react'; import { Modal } from '..'; import { ModalContent } from './modal-content'; import { ModalContentSize } from './modal-content.types'; describe('ModalContent', () => { const onClose = jest.fn(); afterEach(() => { onClose.mockReset(); }); it('should render with text inside the ModalContent', () => { const { getByText, getByTestId } = render( test , ); expect(getByText('test')).toBeDefined(); expect(getByText('test')).toHaveClass('mm-modal-content__dialog'); expect(getByTestId('modal-content')).toHaveClass('mm-modal-content'); }); it('should match snapshot', () => { const { getByTestId } = render( test , ); expect(getByTestId('test')).toMatchSnapshot(); }); it('should render with and additional className', () => { const { getByTestId } = render( test , , ); expect(getByTestId('test')).toHaveClass('test-class'); }); it('should render with size sm', () => { const { getByText } = render( <> default sm , ); expect(getByText('sm')).toHaveClass('mm-modal-content__dialog--size-sm'); expect(getByText('default')).toHaveClass( 'mm-modal-content__dialog--size-sm', ); }); it('should render with additional props being passed to modalDialogProps', () => { const { getByTestId } = render( test , ); expect(getByTestId('test')).toBeDefined(); }); it('should close when escape key is pressed', () => { const { getByRole } = render( modal dialog , ); 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 dialog , ); fireEvent.keyDown(getByRole('dialog'), { key: 'Escape' }); expect(onClose).not.toHaveBeenCalled(); }); it('should close when clicked outside', () => { const { getByRole } = render( modal dialog , ); // 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 = React.createRef(); const { getByTestId } = render( modal dialog , ); // 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 = React.createRef(); const { getByTestId } = render( , ); expect(getByTestId('input')).toHaveFocus(); }); it('should focus final focus ref when modal is closed', () => { const finalRef: React.RefObject = React.createRef(); const { rerender } = render( <> modal dialog , ); rerender( <> modal dialog , ); expect(finalRef.current).toHaveFocus(); }); });