Files
hms/apps/miniprogram/__tests__/services/consultation.test.ts
iven 935ca70dfa test(mp): service 层测试扩展 — health + consultation + request
新增 3 个测试文件(+23 个测试用例),总计 9 文件 75 测试:
- request.test.ts: HTTP 方法、查询参数构建、缓存、错误处理
- health.test.ts: 体征录入字段映射、日常监测、阈值查找
- consultation.test.ts: 咨询会话/消息 CRUD、已读标记

- 添加 vitest setup.ts mock @tarojs/taro 和 @tarojs/runtime
- vitest.config.ts 增加 setupFiles 配置

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-13 14:10:27 +08:00

113 lines
3.2 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import '../helpers/mock-api';
import { api } from '@/services/request';
import {
listConsultations,
getSession,
listMessages,
sendMessage,
markSessionRead,
} from '@/services/consultation';
describe('consultation service', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('listConsultations', () => {
it('calls api.get with correct path', async () => {
vi.mocked(api.get).mockResolvedValueOnce({ data: [], total: 0 });
await listConsultations();
expect(api.get).toHaveBeenCalledWith('/health/consultation-sessions', undefined);
});
it('passes pagination params', async () => {
vi.mocked(api.get).mockResolvedValueOnce({ data: [], total: 0 });
await listConsultations({ page: 2, page_size: 10 });
expect(api.get).toHaveBeenCalledWith('/health/consultation-sessions', {
page: 2,
page_size: 10,
});
});
});
describe('getSession', () => {
it('calls api.get with session id', async () => {
const mock = { id: 's-1', status: 'active' } as any;
vi.mocked(api.get).mockResolvedValueOnce(mock);
const result = await getSession('s-1');
expect(api.get).toHaveBeenCalledWith('/health/consultation-sessions/s-1');
expect(result).toEqual(mock);
});
});
describe('listMessages', () => {
it('calls api.get with session id and params', async () => {
vi.mocked(api.get).mockResolvedValueOnce({ data: [], total: 0 });
await listMessages('s-1', { page: 1, page_size: 20 });
expect(api.get).toHaveBeenCalledWith(
'/health/consultation-sessions/s-1/messages',
{ page: 1, page_size: 20 },
);
});
it('passes after_id for incremental loading', async () => {
vi.mocked(api.get).mockResolvedValueOnce({ data: [], total: 0 });
await listMessages('s-1', { after_id: 'm-50' });
expect(api.get).toHaveBeenCalledWith(
'/health/consultation-sessions/s-1/messages',
expect.objectContaining({ after_id: 'm-50' }),
);
});
});
describe('sendMessage', () => {
it('calls api.post with session_id, content_type and content', async () => {
const mockMsg = { id: 'm-1', content: 'hello' } as any;
vi.mocked(api.post).mockResolvedValueOnce(mockMsg);
const result = await sendMessage('s-1', 'hello');
expect(api.post).toHaveBeenCalledWith('/health/consultation-messages', {
session_id: 's-1',
content_type: 'text',
content: 'hello',
});
expect(result).toEqual(mockMsg);
});
it('supports custom content_type', async () => {
vi.mocked(api.post).mockResolvedValueOnce({} as any);
await sendMessage('s-1', '<img src="x"/>', 'html');
expect(api.post).toHaveBeenCalledWith('/health/consultation-messages', {
session_id: 's-1',
content_type: 'html',
content: '<img src="x"/>',
});
});
});
describe('markSessionRead', () => {
it('calls api.put with session id', async () => {
vi.mocked(api.put).mockResolvedValueOnce(undefined);
await markSessionRead('s-1');
expect(api.put).toHaveBeenCalledWith('/health/consultation-sessions/s-1/read');
});
});
});