admin/hooks/useAdminAction.test.ts
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

46 lines
1.5 KiB
TypeScript

import { act, renderHook } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import useAdminAction from '@/hooks/useAdminAction'
const addToast = vi.fn()
vi.mock('@/lib/toast', () => ({ addToast: (...args: unknown[]) => addToast(...args) }))
describe('useAdminAction', () => {
beforeEach(() => addToast.mockClear())
it('tracks the pending row, reports success and refreshes', async () => {
let resolveAction!: () => void
const action = vi.fn(() => new Promise<void>((resolve) => (resolveAction = resolve)))
const onSuccess = vi.fn()
const { result } = renderHook(() => useAdminAction({ onSuccess }))
let promise!: Promise<void>
act(() => {
promise = result.current.runAction('row-1', action, 'انجام شد')
})
expect(result.current.pendingId).toBe('row-1')
await act(async () => {
resolveAction()
await promise
})
expect(result.current.pendingId).toBeNull()
expect(onSuccess).toHaveBeenCalledOnce()
expect(addToast).toHaveBeenCalledWith({ title: 'انجام شد', color: 'success' })
})
it('shows the server error and does not refresh after failure', async () => {
const onSuccess = vi.fn()
const { result } = renderHook(() => useAdminAction({ onSuccess }))
await act(() => result.current.runAction('row-1', () => Promise.reject(new Error('خطای سرور')), 'انجام شد'))
expect(onSuccess).not.toHaveBeenCalled()
expect(addToast).toHaveBeenCalledWith({ title: 'خطای سرور', color: 'danger' })
})
})