feat(miniprogram): service 层测试框架搭建
- 新增 __tests__/helpers/: mock-taro (Taro API mock) + mock-api (request mock) - 示例测试: patient.test.ts (3 用例) + appointment.test.ts (9 用例) - 覆盖 list/create/update/cancel/calendar 等核心场景 - 全部 42 测试通过(含 4 个已有 BLE 测试)
This commit is contained in:
56
apps/miniprogram/__tests__/services/patient.test.ts
Normal file
56
apps/miniprogram/__tests__/services/patient.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user