import { describe, it, expect, beforeEach, vi } from 'vitest'; // mock-api.ts 的顶层 vi.mock 自动生效,无需显式调用 import '../helpers/mock-api'; import { api } from '@/services/request'; import { listPatients, createPatient, updatePatient } from '@/services/patient'; describe('patient service', () => { beforeEach(() => { vi.clearAllMocks(); }); describe('listPatients', () => { it('调用 api.get 并传递分页参数', async () => { const mockData = { data: [{ id: '1', name: '张三', version: 1 }], total: 1 }; vi.mocked(api.get).mockResolvedValueOnce(mockData); const result = await listPatients(); expect(api.get).toHaveBeenCalledWith('/health/patients', { page: 1, page_size: 100, }); expect(result).toEqual(mockData); }); }); describe('createPatient', () => { it('调用 api.post 传递患者数据', async () => { const input = { name: '李四', gender: 'male', birth_date: '1990-01-01' }; const mockPatient = { id: '2', name: '李四', version: 1 }; vi.mocked(api.post).mockResolvedValueOnce(mockPatient); const result = await createPatient(input); expect(api.post).toHaveBeenCalledWith('/health/patients', input); expect(result).toEqual(mockPatient); }); }); describe('updatePatient', () => { it('调用 api.put 传递更新数据和 version', async () => { const input = { name: '王五' }; const mockPatient = { id: '1', name: '王五', version: 2 }; vi.mocked(api.put).mockResolvedValueOnce(mockPatient); const result = await updatePatient('1', input, 1); expect(api.put).toHaveBeenCalledWith('/health/patients/1', { ...input, version: 1, }); expect(result).toEqual(mockPatient); }); }); });