import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { describe, expect, it, vi } from 'vitest'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from '../dialog'; describe('Dialog', () => { it('renders nothing when open is false', () => { render( {}}> Hidden , ); expect(screen.queryByText('Hidden')).not.toBeInTheDocument(); }); it('renders content when open is true', () => { render( {}}> Test Dialog Dialog description

Body content

, ); expect(screen.getByText('Test Dialog')).toBeInTheDocument(); expect(screen.getByText('Dialog description')).toBeInTheDocument(); expect(screen.getByText('Body content')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'OK' })).toBeInTheDocument(); }); it('calls onOpenChange when backdrop is clicked', async () => { const onOpenChange = vi.fn(); render( Closeable , ); // Click the backdrop (the overlay div) const backdrop = document.querySelector('.bg-black\\/80'); if (backdrop) { await userEvent.click(backdrop); } expect(onOpenChange).toHaveBeenCalledWith(false); }); it('does not close when clicking inside content', async () => { const onOpenChange = vi.fn(); render( Stay Open , ); await userEvent.click(screen.getByText('Stay Open')); expect(onOpenChange).not.toHaveBeenCalled(); }); describe('a11y: DialogContext auto-labelling', () => { it('renders DialogContent with role="dialog" and aria-modal', () => { render( {}}> A11y Title , ); const dialog = screen.getByRole('dialog'); expect(dialog).toHaveAttribute('aria-modal', 'true'); }); it('auto-wires aria-labelledby from DialogTitle id', () => { render( {}}> Auto Label Auto Desc , ); const dialog = screen.getByRole('dialog'); const titleId = dialog.getAttribute('aria-labelledby'); const descId = dialog.getAttribute('aria-describedby'); expect(titleId).toBeTruthy(); expect(descId).toBeTruthy(); // The title element should carry the matching id expect(screen.getByText('Auto Label')).toHaveAttribute('id', titleId); expect(screen.getByText('Auto Desc')).toHaveAttribute('id', descId); }); it('allows explicit id override on DialogTitle', () => { render( {}}> Custom , ); expect(screen.getByText('Custom')).toHaveAttribute('id', 'custom-title'); }); }); });