import { describe, expect, it, vi } from 'vitest'; import type { ComponentProps } from 'svelte'; import { act, render, screen, within } from '@testing-library/svelte'; import userEvent from '@testing-library/user-event'; import Subject from './modal.spec.svelte'; describe('Modal', () => { const onClose = vi.fn(); const renderSubject = (props?: Partial>) => { const result = render(Subject, { isOpen: true, onClose, ...props }); return result; }; it('should be visible if open is true', () => { renderSubject({ isOpen: true }); const modal = screen.getByRole('dialog'); const heading = screen.getByRole('heading', { name: /this is the heading/iu, level: 2, }); const message = screen.getByText(/this is the description/iu); const primary = screen.getByTestId('primary'); const secondary = screen.getByTestId('secondary'); const body = screen.getByTestId('body'); expect(modal).toHaveAttribute('aria-modal', 'true'); expect(heading).toBeInTheDocument(); expect(message).toBeInTheDocument(); expect(primary).toBeInTheDocument(); expect(secondary).toBeInTheDocument(); expect(body).toBeInTheDocument(); expect(onClose).not.toHaveBeenCalled(); }); it('should have an accessible name and description', () => { renderSubject({ isOpen: true }); const modal = screen.getByRole('dialog'); expect(modal).toHaveAccessibleName(/this is the heading/iu); expect(modal).toHaveAccessibleDescription(/this is the description/iu); }); it('should not be visible if open is false', () => { renderSubject({ isOpen: false }); const modal = screen.queryByRole('dialog'); expect(modal).not.toBeInTheDocument(); expect(onClose).not.toHaveBeenCalled(); }); it('should close modal when close icon button is clicked', async () => { const user = userEvent.setup(); renderSubject({ isOpen: true }); const modal = screen.getByRole('dialog'); const closeButton = within(modal).getByRole('button', { name: /close/iu }); await user.click(closeButton); expect(onClose).toHaveBeenCalledOnce(); }); it('should close modal when clicked outside the modal', async () => { const user = userEvent.setup(); renderSubject({ isOpen: true }); const outside = screen.getByTestId('outside'); await user.click(outside); expect(onClose).toHaveBeenCalledOnce(); }); it('should close modal when escape key is pressed', async () => { const user = userEvent.setup(); renderSubject({ isOpen: true }); await user.keyboard('{Escape}'); expect(onClose).toHaveBeenCalledOnce(); }); it('should not emit close events on escape if the modal is closed', async () => { const user = userEvent.setup(); renderSubject({ isOpen: false }); await user.keyboard('{Escape}'); expect(onClose).not.toHaveBeenCalled(); }); it('should change role to alertdialog when role is passed', () => { renderSubject({ isOpen: true, role: 'alertdialog' }); const modal = screen.getByRole('alertdialog'); expect(modal).toBeInTheDocument(); }); it('should prevent body scroll', () => { renderSubject({ isOpen: true, role: 'alertdialog' }); expect(document.body).toHaveClass('overflow-hidden'); }); it('should allow body to scroll on component unmount', async () => { const { unmount } = renderSubject({ isOpen: true, role: 'alertdialog' }); await act(() => unmount()); expect(document.body).not.toHaveClass('overflow-hidden'); }); });