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(
,
);
expect(screen.queryByText('Hidden')).not.toBeInTheDocument();
});
it('renders content when open is true', () => {
render(
,
);
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(
,
);
// 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(
,
);
await userEvent.click(screen.getByText('Stay Open'));
expect(onOpenChange).not.toHaveBeenCalled();
});
});