Files
hms/apps/miniprogram/__tests__/helpers/mock-taro.ts
iven af3eb0c7a1 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 测试)
2026-05-11 13:58:58 +08:00

51 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { vi } from 'vitest';
/**
* Taro API mock — 所有小程序测试共享的基础 mock。
* storage 使用内存 Map可在 beforeEach 中 clear。
*/
export function createTaroMock() {
const storage = new Map<string, any>();
return {
storage,
mock: {
default: {
getStorageSync: vi.fn((key: string) => storage.get(key) ?? ''),
setStorageSync: vi.fn((key: string, val: any) => storage.set(key, val)),
removeStorageSync: vi.fn((key: string) => storage.delete(key)),
showToast: vi.fn(),
hideToast: vi.fn(),
showLoading: vi.fn(),
hideLoading: vi.fn(),
request: vi.fn(),
reLaunch: vi.fn(),
navigateTo: vi.fn(),
redirectTo: vi.fn(),
switchTab: vi.fn(),
getCurrentPages: vi.fn(() => []),
getAccountInfoSync: vi.fn(() => ({ miniProgram: { envVersion: 'develop' } })),
getSystemInfoSync: vi.fn(() => ({
windowHeight: 800,
windowWidth: 375,
pixelRatio: 2,
})),
},
},
};
}
/** 一键 mock @tarojs/taro — 在 vi.mock 回调中使用 */
export function mockTaro() {
const { storage, mock } = createTaroMock();
vi.mock('@tarojs/taro', () => mock);
vi.mock('@/utils/secure-storage', () => ({
secureGet: vi.fn((key: string) => storage.get(key) ?? ''),
secureSet: vi.fn((key: string, val: string) => storage.set(key, val)),
secureRemove: vi.fn((key: string) => storage.delete(key)),
}));
return { storage };
}