Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
157 lines
4.1 KiB
TypeScript
157 lines
4.1 KiB
TypeScript
import type { ReactNode } from 'react'
|
||
|
||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||
|
||
import AdminEventCommissionModal from '@/features/events/detail/admin-event-detail/AdminEventCommissionModal'
|
||
|
||
const updateCommission = vi.fn()
|
||
const addToast = vi.fn()
|
||
|
||
vi.mock('@/lib/toast', () => ({ addToast: (...args: unknown[]) => addToast(...args) }))
|
||
vi.mock('@/services/events', () => ({
|
||
updateEventCommissionAsAdmin: (...args: unknown[]) => updateCommission(...args),
|
||
}))
|
||
vi.mock('@/components/formElements/Input', () => ({
|
||
default: ({ label, value, onValueChange }: { label: string; value?: unknown; onValueChange?: (value: string) => void }) => (
|
||
<label>
|
||
{label}
|
||
<input
|
||
aria-label={label}
|
||
value={String(value ?? '')}
|
||
onChange={(event) => onValueChange?.(event.target.value)}
|
||
/>
|
||
</label>
|
||
),
|
||
}))
|
||
vi.mock('@/components/formElements/Button', () => ({
|
||
default: ({
|
||
children,
|
||
onClick,
|
||
disabled,
|
||
isLoading,
|
||
}: {
|
||
children: ReactNode
|
||
onClick?: () => void
|
||
disabled?: boolean
|
||
isLoading?: boolean
|
||
}) => (
|
||
<button
|
||
disabled={disabled || isLoading}
|
||
type="button"
|
||
onClick={onClick}
|
||
>
|
||
{children}
|
||
</button>
|
||
),
|
||
}))
|
||
vi.mock('@/components/modals/Modal', () => ({
|
||
default: ({
|
||
isOpen,
|
||
title,
|
||
children,
|
||
footerChildren,
|
||
}: {
|
||
isOpen: boolean
|
||
title: string
|
||
children: ReactNode
|
||
footerChildren?: ReactNode
|
||
}) =>
|
||
isOpen ? (
|
||
<div
|
||
aria-label={title}
|
||
role="dialog"
|
||
>
|
||
{children}
|
||
{footerChildren}
|
||
</div>
|
||
) : null,
|
||
}))
|
||
|
||
describe('AdminEventCommissionModal', () => {
|
||
afterEach(cleanup)
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
updateCommission.mockResolvedValue({
|
||
id: 'event-1',
|
||
commissionPercent: 12,
|
||
effectiveCommissionPercent: 12,
|
||
})
|
||
})
|
||
|
||
it('rejects out-of-range commission and does not call the API', () => {
|
||
const onUpdated = vi.fn()
|
||
const onOpenChange = vi.fn()
|
||
|
||
render(
|
||
<AdminEventCommissionModal
|
||
isOpen
|
||
commissionPercent={10}
|
||
eventId="event-1"
|
||
onOpenChange={onOpenChange}
|
||
onUpdated={onUpdated}
|
||
/>
|
||
)
|
||
|
||
fireEvent.change(screen.getByLabelText('درصد کمیسیون (۰ تا ۱۰۰)'), { target: { value: '150' } })
|
||
fireEvent.click(screen.getByRole('button', { name: 'ذخیره' }))
|
||
|
||
expect(screen.getByText('درصد کمیسیون باید عددی بین ۰ تا ۱۰۰ باشد')).toBeTruthy()
|
||
expect(updateCommission).not.toHaveBeenCalled()
|
||
expect(onUpdated).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('saves a valid override and closes on success', async () => {
|
||
const onUpdated = vi.fn()
|
||
const onOpenChange = vi.fn()
|
||
|
||
render(
|
||
<AdminEventCommissionModal
|
||
isOpen
|
||
commissionPercent={null}
|
||
eventId="event-1"
|
||
onOpenChange={onOpenChange}
|
||
onUpdated={onUpdated}
|
||
/>
|
||
)
|
||
|
||
fireEvent.change(screen.getByLabelText('درصد کمیسیون (۰ تا ۱۰۰)'), { target: { value: '12' } })
|
||
fireEvent.click(screen.getByRole('button', { name: 'ذخیره' }))
|
||
|
||
await waitFor(() => {
|
||
expect(updateCommission).toHaveBeenCalledWith('event-1', 12)
|
||
expect(onUpdated).toHaveBeenCalled()
|
||
expect(onOpenChange).toHaveBeenCalledWith(false)
|
||
})
|
||
})
|
||
|
||
it('resets to platform default when an override exists', async () => {
|
||
updateCommission.mockResolvedValue({
|
||
id: 'event-1',
|
||
commissionPercent: null,
|
||
effectiveCommissionPercent: 15,
|
||
})
|
||
const onUpdated = vi.fn()
|
||
const onOpenChange = vi.fn()
|
||
|
||
render(
|
||
<AdminEventCommissionModal
|
||
isOpen
|
||
commissionPercent={20}
|
||
eventId="event-1"
|
||
onOpenChange={onOpenChange}
|
||
onUpdated={onUpdated}
|
||
/>
|
||
)
|
||
|
||
fireEvent.click(screen.getByRole('button', { name: 'بازگشت به پیشفرض پلتفرم' }))
|
||
|
||
await waitFor(() => {
|
||
expect(updateCommission).toHaveBeenCalledWith('event-1', null)
|
||
expect(onUpdated).toHaveBeenCalled()
|
||
expect(onOpenChange).toHaveBeenCalledWith(false)
|
||
})
|
||
})
|
||
})
|