admin/context/AlertModalContext.test.tsx
alisaza e1eaf5eff5 feat: initial ghabilee-admin backoffice app
Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js
app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
2026-09-05 13:12:59 +03:30

65 lines
1.9 KiB
TypeScript

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<void> }) {
const { showAlert } = useAlertModal()
return (
<button
type="button"
onClick={() => {
showAlert('این عملیات قابل بازگشت نیست.', onConfirm, undefined, { dangerAccept: true })
}}
>
حذف
</button>
)
}
describe('AlertModalProvider', () => {
it('renders a semantic alert dialog and runs the confirmation once', async () => {
const onConfirm = vi.fn()
render(
<AlertModalProvider>
<AlertHarness onConfirm={onConfirm} />
</AlertModalProvider>
)
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(
<AlertModalProvider>
<AlertHarness onConfirm={onConfirm} />
</AlertModalProvider>
)
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()
})
})