import { describe, it, expect, beforeEach, vi } from 'vitest'; import { screen, fireEvent, waitFor, within } from '@testing-library/react'; import { renderWithProviders } from '../test/utils'; vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); vi.mock('../lib/api', () => ({ api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() }, ApiError: class extends Error {}, })); import { api } from '../lib/api'; import AdminSubscriptionPage from './AdminSubscriptionPage'; const get = api.get as ReturnType; const patch = api.patch as ReturnType; const PLANS = [ { uuid: 'p-basic', name: 'basic', level: 1, max_secretaries: 3, max_resources: 3, features: { patient_records: true, services: true, sms_panel: false }, active: true, periods: [], }, { uuid: 'p-pro', name: 'professional', level: 2, max_secretaries: 10, max_resources: -1, features: { patient_records: true, services: true, sms_panel: true }, active: true, periods: [], }, ]; function mockApi() { get.mockImplementation((url: string) => { if (url.includes('/admin/subscription/plans')) return Promise.resolve({ success: true, data: PLANS }); return Promise.resolve({ success: true, data: [], meta: { totalRecords: 0 } }); }); patch.mockResolvedValue({ success: true, data: PLANS[0] }); } /** مودالِ ویرایشِ پلنِ داده‌شده را باز می‌کند. هر کارت یک دکمهٔ مداد دارد. */ async function openEdit(planLabel: string) { const card = (await screen.findByText(planLabel)).closest('.card') as HTMLElement; const buttons = within(card).getAllByRole('button'); fireEvent.click(buttons[buttons.length - 1]); } describe('AdminSubscriptionPage — سقف منابع پلن', () => { beforeEach(() => { get.mockReset(); patch.mockReset(); mockApi(); }); it('سقف منابع هر پلن را روی کارتش نشان می‌دهد', async () => { renderWithProviders(, { route: '/admin/admin-subscription' }); expect(await screen.findByText('حداکثر ۳ منبع')).toBeInTheDocument(); expect(screen.getByText('حداکثر نامحدود منبع')).toBeInTheDocument(); }); it('فرم ویرایش با مقدار فعلی پر می‌شود و همان را می‌فرستد', async () => { renderWithProviders(, { route: '/admin/admin-subscription' }); await openEdit('پایه'); const input = await screen.findByLabelText('حداکثر منبع *'); expect(input).toHaveValue('3'); fireEvent.change(input, { target: { value: '5' } }); fireEvent.click(screen.getByText('ذخیره')); await waitFor(() => expect(patch).toHaveBeenCalled()); expect(patch.mock.calls[0][0]).toBe('/api/v1/admin/subscription/plan/p-basic'); expect(patch.mock.calls[0][1]).toMatchObject({ max_resources: 5 }); }); /** ادمین «۱-» تایپ نمی‌کند؛ سوییچ همان قرارداد را می‌سازد. */ it('سوییچ «منابع نامحدود» مقدار ۱- می‌فرستد', async () => { renderWithProviders(, { route: '/admin/admin-subscription' }); await openEdit('پایه'); await screen.findByLabelText('حداکثر منبع *'); fireEvent.click(screen.getByText('منابع نامحدود')); fireEvent.click(screen.getByText('ذخیره')); await waitFor(() => expect(patch).toHaveBeenCalled()); expect(patch.mock.calls[0][1]).toMatchObject({ max_resources: -1 }); }); it('پلن نامحدود با سوییچِ روشن باز می‌شود و فیلد عددی‌اش قفل است', async () => { renderWithProviders(, { route: '/admin/admin-subscription' }); await openEdit('حرفه‌ای'); expect(await screen.findByLabelText('حداکثر منبع *')).toBeDisabled(); }); });