import React from 'react'; import { fireEvent } from '@testing-library/react'; import renderWithTheme from '../../../testUtils/renderWithTheme'; import Modal from '../index'; import { noop } from '../../../fp/function'; window.scrollTo = jest.fn(); describe('rendering', () => { it('renders modal contents', () => { const { getByText } = renderWithTheme( ); expect(getByText('A title')).toBeInTheDocument(); expect(getByText('A body')).toBeInTheDocument(); expect(getByText('A footer')).toBeInTheDocument(); }); it('allows to control the visibility', () => { const { getByText } = renderWithTheme( ); expect(getByText('A title')).not.toBeVisible(); expect(getByText('A body')).not.toBeVisible(); expect(getByText('A footer')).not.toBeVisible(); }); it('allows to customise modal contents using react elements', () => { const { getByText } = renderWithTheme( Customised title} body={
Customised body
} footer={
Customised footer
} /> ); expect(getByText('Customised title')).toBeInTheDocument(); expect(getByText('Customised body')).toBeInTheDocument(); expect(getByText('Customised footer')).toBeInTheDocument(); }); describe('close button', () => { it('renders close button when onClose is defined', () => { const { queryByTestId } = renderWithTheme( ); expect(queryByTestId('modal-close-button')).toBeInTheDocument(); }); it('DOES NOT renderWithTheme close button when onClose is undefined', () => { const { queryByTestId } = renderWithTheme( ); expect(queryByTestId('modal-close-button')).not.toBeInTheDocument(); }); }); }); describe('interaction', () => { it('allows to close the modal when clicking on the close button', () => { const onClose = jest.fn(); const { getByTestId } = renderWithTheme( ); fireEvent.click(getByTestId('modal-close-button')); expect(onClose).toHaveBeenCalledTimes(1); }); it('allows to click on customised footer', () => { const onClick = jest.fn(); const { getByText } = renderWithTheme( onClick()}> Footer button } /> ); fireEvent.click(getByText('Footer button')); expect(onClick).toHaveBeenCalledTimes(1); }); });