import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' import { AlertModalProvider } from '@/context/AlertModalContext' import useAlertModal from '@/hooks/useAlertModal' afterEach(cleanup) function AlertHarness({ onConfirm }: { onConfirm: () => void | Promise }) { const { showAlert } = useAlertModal() return ( ) } describe('AlertModalProvider', () => { it('renders a semantic alert dialog and runs the confirmation once', async () => { const onConfirm = vi.fn() render( ) fireEvent.click(screen.getByRole('button', { name: 'حذف' })) const dialog = await screen.findByRole('alertdialog') expect(dialog).toHaveTextContent('این عملیات قابل بازگشت نیست.') fireEvent.click(screen.getByRole('button', { name: 'تأیید' })) await waitFor(() => { expect(onConfirm).toHaveBeenCalledTimes(1) }) await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()) }) it('closes without running the confirmation when cancelled', async () => { const onConfirm = vi.fn() render( ) fireEvent.click(screen.getByRole('button', { name: 'حذف' })) await screen.findByRole('alertdialog') fireEvent.click(screen.getByRole('button', { name: 'انصراف' })) await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()) expect(onConfirm).not.toHaveBeenCalled() }) })