import userEvent from '@testing-library/user-event'; import React from 'react'; import { render, screen } from '../../../../jest/test-helpers'; import Modal from '../modal'; describe('', () => { it('renders', () => { render(); expect(screen.getByTestId('modal')).toBeInTheDocument(); }); it('renders children', () => { render(
); expect(screen.getByTestId('child')).toBeInTheDocument(); }); it('focuses the primary action', () => { render( null} confirmationText='Click me' />, ); expect(screen.getByRole('button')).toHaveFocus(); }); describe('onClose prop', () => { it('renders the Icon to close the modal', async() => { const mockFn = vi.fn(); const user = userEvent.setup(); render(); expect(screen.getByTestId('icon-button')).toBeInTheDocument(); expect(mockFn).not.toBeCalled(); await user.click(screen.getByTestId('icon-button')); expect(mockFn).toBeCalled(); }); it('does not render the Icon to close the modal', () => { render(); expect(screen.queryAllByTestId('icon-button')).toHaveLength(0); }); }); describe('confirmationAction prop', () => { it('renders the confirmation button', async() => { const mockFn = vi.fn(); const user = userEvent.setup(); render( , ); expect(mockFn).not.toBeCalled(); await user.click(screen.getByRole('button')); expect(mockFn).toBeCalled(); }); it('does not render the actions to', () => { render(); expect(screen.queryAllByTestId('modal-actions')).toHaveLength(0); }); describe('with secondaryAction', () => { it('renders the secondary button', async() => { const confirmationAction = vi.fn(); const secondaryAction = vi.fn(); const user = userEvent.setup(); render( , ); await user.click(screen.getByText(/secondary/i)); expect(secondaryAction).toBeCalled(); expect(confirmationAction).not.toBeCalled(); }); it('does not render the secondary action', () => { render( null} confirmationText='Click me' />, ); expect(screen.queryAllByRole('button')).toHaveLength(1); }); }); describe('with cancelAction', () => { it('renders the cancel button', async() => { const confirmationAction = vi.fn(); const cancelAction = vi.fn(); const user = userEvent.setup(); render( , ); await user.click(screen.getByText(/cancel/i)); expect(cancelAction).toBeCalled(); expect(confirmationAction).not.toBeCalled(); }); it('does not render the cancel action', () => { render( null} confirmationText='Click me' />, ); expect(screen.queryAllByRole('button')).toHaveLength(1); }); }); }); });