import { describe, it, expect, beforeEach, vi } from 'vitest'; import { waitFor } from '@testing-library/react'; import { renderHookWithClient } from '@/test/utils'; 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 { usePatient, useUpdatePatient } from '@/hooks/usePatient'; const get = api.get as ReturnType; const patch = api.patch as ReturnType; beforeEach(() => { get.mockReset(); patch.mockReset(); }); describe('usePatient', () => { it('پروفایل را از data.data.profile استخراج می‌کند', async () => { get.mockResolvedValue({ data: { uuid: 'u-1', profile: { name: 'ساغر', national_code: '0012345678', city_id: 42 } }, }); const { result } = renderHookWithClient(() => usePatient('u-1')); await waitFor(() => expect(result.current.profile).not.toBeNull()); expect(get).toHaveBeenCalledWith('/api/v1/patient/u-1'); expect(result.current.profile?.name).toBe('ساغر'); expect(result.current.profile?.city_id).toBe(42); expect(result.current.record?.uuid).toBe('u-1'); }); it('بدون uuid کوئری اجرا نمی‌شود', () => { const { result } = renderHookWithClient(() => usePatient(undefined)); expect(get).not.toHaveBeenCalled(); expect(result.current.profile).toBeNull(); }); }); describe('useUpdatePatient', () => { it('PATCH را با بدنه به آدرس بیمار می‌فرستد', async () => { patch.mockResolvedValue({ data: { uuid: 'u-1', profile: {} } }); const { result } = renderHookWithClient(() => useUpdatePatient('u-1')); result.current.mutate({ fathers_name: 'رضا', city_id: 42 }); await waitFor(() => expect(patch).toHaveBeenCalled()); expect(patch).toHaveBeenCalledWith('/api/v1/patient/u-1', { fathers_name: 'رضا', city_id: 42 }); }); });