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'; 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()); userEvent.tab(); expect(getByText('Modal content button')).toHaveFocus(); userEvent.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', () => { const onClose = jest.fn(); const { container } = render( This is the modal body content , ); userEvent.type(container, '{esc}'); expect(onClose).toHaveBeenCalledTimes(1); }); it("shouldn't close on outside click by default", () => { const onClose = jest.fn(); const { getByText } = render(

Outside content

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

Outside content

This is the modal body content
, ); userEvent.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 an overlay', 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, ); });