import { describe, it, expect, vi } from 'vitest'; import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import PatientRecordInfoForm, { type PatientFormValues, type PatientFormOptions, } from '@/components/PatientRecordInfoForm'; const options: PatientFormOptions = { gender: [{ value: 'female', label: 'زن' }, { value: 'male', label: 'مرد' }], marital: [{ value: 'single', label: 'مجرد' }], education: [{ value: 'bachelor', label: 'کارشناسی' }], insurance: [{ value: 3, label: 'تأمین اجتماعی' }], supplementary: [{ value: 5, label: 'بیمه دانا' }], province: [{ value: 8, label: 'یزد' }], city: [{ value: 42, label: 'یزد' }], referral: [{ value: 'instagram', label: 'اینستاگرام' }], }; const baseValues: PatientFormValues = { name: 'ساغر صابری نژاد', mobile: '', national_code: '', gender: null, birth_date: '', fathers_name: '', marital_status: null, basic_insurance_id: null, supplementary_insurance_id: null, field_of_study: '', education: null, job: '', province_id: null, city_id: null, home_phone: '', address: '', postal_code: '', referral_source: null, description: '', }; function setup(overrides: Partial = {}) { const onSubmit = vi.fn(); render( , ); return { onSubmit }; } describe('PatientRecordInfoForm', () => { it('برچسب‌های اصلی و شماره پروندهٔ read-only را رندر می‌کند', () => { setup(); expect(screen.getByText('نام و نام خانوادگی مراجعه کننده')).toBeInTheDocument(); expect(screen.getByText('بیمه پایه')).toBeInTheDocument(); expect(screen.getByText('بیمه تکمیلی')).toBeInTheDocument(); expect(screen.getByText('نحوه آشنایی')).toBeInTheDocument(); expect((screen.getByDisplayValue('123456789') as HTMLInputElement).readOnly).toBe(true); }); it('نام خالی → خطای الزامی و onSubmit صدا نمی‌خورد', async () => { const { onSubmit } = setup({ name: '' }); await userEvent.click(screen.getByRole('button', { name: /ثبت اطلاعات/ })); expect(await screen.findByText('نام و نام خانوادگی الزامی است')).toBeInTheDocument(); expect(onSubmit).not.toHaveBeenCalled(); }); it('کدملی نامعتبر → خطای ۱۰ رقم', async () => { setup({ national_code: '123' }); await userEvent.click(screen.getByRole('button', { name: /ثبت اطلاعات/ })); expect(await screen.findByText('کد ملی باید ۱۰ رقم باشد')).toBeInTheDocument(); }); it('ورودی معتبر → onSubmit با مقادیر فرم', async () => { const { onSubmit } = setup({ national_code: '0012345678' }); await userEvent.click(screen.getByRole('button', { name: /ثبت اطلاعات/ })); await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1)); expect(onSubmit.mock.calls[0][0]).toMatchObject({ name: 'ساغر صابری نژاد', national_code: '0012345678', }); }); });