import { h } from 'preact'; import { axe } from 'jest-axe'; import '@testing-library/jest-dom'; import { render, waitFor } from '@testing-library/preact'; import userEvent from '@testing-library/user-event'; import { Modal } from '../Modal'; describe('Modal', () => { let user; beforeEach(() => { user = userEvent.setup(); }); it('should have no a11y violations', async () => { const { container } = render( This is the modal body content , ); const results = await axe(container); expect(results).toHaveNoViolations(); }); it('should trap focus inside the modal by default', async () => { const { getByText, getByLabelText } = render(
, ); const closeButton = getByLabelText('Close', { selector: 'button' }); await waitFor(() => expect(closeButton).toHaveFocus()); await user.tab(); expect(getByText('Modal content button')).toHaveFocus(); await user.tab(); expect(closeButton).toHaveFocus(); }); it('should trap focus in the custom selector if provided in props', async () => { const { getByText } = render(
, ); const buttonInsideFocusTrap = getByText('Inside focus trap button'); await waitFor(() => expect(buttonInsideFocusTrap).toHaveFocus()); }); it('should close when the close button is clicked', async () => { const onClose = jest.fn(); const { getByLabelText } = render( This is the modal body content , ); const closeButton = getByLabelText('Close', { selector: 'button' }); closeButton.click(); expect(onClose).toHaveBeenCalledTimes(1); }); it('should close when Escape is pressed', async () => { const onClose = jest.fn(); render( This is the modal body content , ); await user.keyboard('{Escape}'); expect(onClose).toHaveBeenCalledTimes(1); }); it("shouldn't close on outside click by default", async () => { const onClose = jest.fn(); const { getByText } = render(

Outside content

This is the modal body content
, ); await user.click(getByText('Outside content')); expect(onClose).not.toHaveBeenCalled(); }); it('should close on click outside, if enabled', async () => { const onClose = jest.fn(); const { getByText } = render(

Outside content

This is the modal body content
, ); await user.click(getByText('Outside content')); expect(onClose).toHaveBeenCalledTimes(1); }); it('should render with additional class names', async () => { const { getByTestId } = render( This is the modal body content , ); const modalContainer = getByTestId('modal-container'); expect( modalContainer.classList.contains('some-additional-class-name'), ).toEqual(true); }); it('should render with a backdrop', async () => { const { getByTestId } = render( This is the modal body content , ); const modalOverlay = getByTestId('modal-overlay'); expect(modalOverlay).not.toBeNull(); }); it('should render with a different size modal', async () => { const { getByTestId } = render( This is the modal body content , ); const modalContainer = getByTestId('modal-container'); expect(modalContainer.classList.contains('crayons-modal--large')).toEqual( true, ); }); });