import { render, screen, fireEvent } from '@testing-library/react'; import { describe, it, expect, vi } from 'vitest'; import { AlertDialog, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogFooter, AlertDialogTitle, AlertDialogDescription, AlertDialogAction, AlertDialogCancel, } from './alert-dialog'; import React from 'react'; // Mocking Portal to render content inline for easier testing in jsdom vi.mock('@radix-ui/react-alert-dialog', async () => { const actual = await vi.importActual('@radix-ui/react-alert-dialog'); return { ...actual, Portal: ({ children }: { children: React.ReactNode }) => (
{children}
), }; }); describe('AlertDialog', () => { it('renders correctly', () => { render( Open Are you sure? This action cannot be undone. Cancel Continue ); expect(screen.getByText('Open')).toBeInTheDocument(); }); it('opens content on trigger click', () => { render( Open Title ); fireEvent.click(screen.getByText('Open')); expect(screen.getByText('Title')).toBeInTheDocument(); }); it('has correct data-slot attributes on components', () => { const { container } = render( Open ); expect(container.querySelector('[data-slot="alert-dialog-trigger"]')).toBeInTheDocument(); }); });