import React, { useState } from 'react'; import { fireEvent } from '@testing-library/react'; import Modal from '../index'; import renderWithTheme from '../../../testUtils/renderWithTheme'; window.scrollTo = jest.fn(); describe('rendering', () => { it('allows to customise modal contents using sub components', () => { const { getByText, getByTestId } = renderWithTheme( Customised header Customised body Customised footer ); expect(getByText('Customised header')).toBeInTheDocument(); expect(getByText('Customised body')).toBeInTheDocument(); expect(getByText('Customised footer')).toBeInTheDocument(); expect(getByText('Customised footer')).toBeInTheDocument(); expect(getByTestId('customised-close-button')).toBeInTheDocument(); }); }); describe('a modal open another', () => { it('lock scroll on body correctly', () => { const Modals = () => { const [open1, setOpen1] = useState(true); const [open2, setOpen2] = useState(true); return ( <> setOpen2(true)}> Open second Modal } open={open1} footer={ } /> setOpen2(false)}> Close second Modal } /> ); }; const { getByText } = renderWithTheme(); fireEvent.click(getByText('Open first Modal')); expect(document.body.style.position).toEqual('fixed'); fireEvent.click(getByText('Open second Modal')); expect(document.body.style.position).toEqual('fixed'); fireEvent.click(getByText('Close second Modal')); expect(document.body.style.position).toEqual('fixed'); fireEvent.click(getByText('Close first Modal')); expect(document.body.style.position).toEqual(''); }); });