Files
nj/apps/web/src/api/health/deviceReadings.test.ts
iven 8111471e93
Some checks failed
Main Merge / backend (push) Has been cancelled
Main Merge / frontend (push) Has been cancelled
feat: 添加管理端前端 (HMS 基座 React 管理面板)
- 从 HMS 基座复制 apps/web/ (React + Ant Design + Vite + TypeScript)
- 管理端自动代理 API 到 localhost:3000 (vite.config.ts)
- 更新 scripts/dev.sh 支持三端启动: backend/admin/app
- 登录验证通过, 用户管理/角色权限/审计日志等页面正常
- 添加 .gitignore 排除 node_modules/dist
2026-06-02 10:03:13 +08:00

83 lines
2.7 KiB
TypeScript

/**
* deviceReadings + devices API 契约测试
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
const mockGet = vi.fn()
const mockPost = vi.fn()
const mockPut = vi.fn()
const mockDelete = vi.fn()
vi.mock('../client', () => ({
default: {
get: (...args: unknown[]) => mockGet(...args),
post: (...args: unknown[]) => mockPost(...args),
put: (...args: unknown[]) => mockPut(...args),
delete: (...args: unknown[]) => mockDelete(...args),
},
}))
import { deviceReadingApi } from './deviceReadings'
import { deviceApi } from './devices'
beforeEach(() => {
vi.clearAllMocks()
})
describe('deviceReadingApi', () => {
const fakeRes = { data: { data: {} } }
it('batchCreate 应调用 POST /health/patients/:id/device-readings/batch', async () => {
mockPost.mockResolvedValue(fakeRes)
const data = {
device_id: 'dev-001',
readings: [
{ device_type: 'blood_pressure', values: { systolic: 130, diastolic: 85 }, measured_at: '2026-05-03T08:00:00Z' },
],
}
await deviceReadingApi.batchCreate('p-001', data)
expect(mockPost).toHaveBeenCalledWith('/health/patients/p-001/device-readings/batch', data)
})
it('query 应调用 GET /health/patients/:id/device-readings 并剥离 patient_id', async () => {
mockGet.mockResolvedValue(fakeRes)
await deviceReadingApi.query({ patient_id: 'p-001', device_type: 'blood_pressure', hours: 24 })
expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/device-readings', {
params: { device_type: 'blood_pressure', hours: 24 },
})
})
it('queryHourly 应调用 GET /health/patients/:id/device-readings/hourly', async () => {
mockGet.mockResolvedValue(fakeRes)
await deviceReadingApi.queryHourly({ patient_id: 'p-001', device_type: 'blood_pressure', days: 7 })
expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/device-readings/hourly', {
params: { device_type: 'blood_pressure', days: 7 },
})
})
})
describe('deviceApi', () => {
const fakeRes = { data: { data: {} } }
it('listDevices 应调用 GET /health/devices 并传递查询参数', async () => {
mockGet.mockResolvedValue(fakeRes)
await deviceApi.listDevices({ patient_id: 'p-001', device_type: 'blood_pressure', page: 1, page_size: 10 })
expect(mockGet).toHaveBeenCalledWith('/health/devices', {
params: { patient_id: 'p-001', device_type: 'blood_pressure', page: 1, page_size: 10 },
})
})
it('unbindDevice 应调用 DELETE /health/devices/:id 并在 body 传递 version', async () => {
mockDelete.mockResolvedValue(fakeRes)
await deviceApi.unbindDevice('dev-001', 2)
expect(mockDelete).toHaveBeenCalledWith('/health/devices/dev-001', {
data: { version: 2 },
})
})
})