test(web): API 契约测试 — 25 个模块 244 个测试全覆盖
验证每个 API 模块的 URL/HTTP Method/参数序列化: - health(14): patients/appointments/alerts/articles/consultations/ dashboard/deviceReadings/doctors/followUp/healthData/points/ followUpTemplates/api - 基础模块(11): auth/users/roles/orgs/dictionaries/messages/ plugins/pluginData/config-modules/workflow/auditLogs 前端测试总数: 140(store) + 244(api) = 384
This commit is contained in:
86
apps/web/src/api/roles.test.ts
Normal file
86
apps/web/src/api/roles.test.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* roles 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 * as rolesApi from './roles'
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('roles API', () => {
|
||||
const fakeRes = { data: { success: true, data: {} } }
|
||||
|
||||
it('listRoles 应调用 GET /roles 并传递分页参数', async () => {
|
||||
mockGet.mockResolvedValue(fakeRes)
|
||||
await rolesApi.listRoles(1, 10)
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('/roles', { params: { page: 1, page_size: 10 } })
|
||||
})
|
||||
|
||||
it('getRole 应调用 GET /roles/:id', async () => {
|
||||
mockGet.mockResolvedValue(fakeRes)
|
||||
await rolesApi.getRole('r-001')
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('/roles/r-001')
|
||||
})
|
||||
|
||||
it('createRole 应调用 POST /roles', async () => {
|
||||
mockPost.mockResolvedValue(fakeRes)
|
||||
const req = { name: '医生', code: 'doctor', description: '医生角色' }
|
||||
await rolesApi.createRole(req)
|
||||
|
||||
expect(mockPost).toHaveBeenCalledWith('/roles', req)
|
||||
})
|
||||
|
||||
it('updateRole 应调用 PUT /roles/:id', async () => {
|
||||
mockPut.mockResolvedValue(fakeRes)
|
||||
const req = { name: '高级医生', version: 1 }
|
||||
await rolesApi.updateRole('r-001', req)
|
||||
|
||||
expect(mockPut).toHaveBeenCalledWith('/roles/r-001', req)
|
||||
})
|
||||
|
||||
it('deleteRole 应调用 DELETE /roles/:id', async () => {
|
||||
mockDelete.mockResolvedValue(undefined)
|
||||
await rolesApi.deleteRole('r-001')
|
||||
|
||||
expect(mockDelete).toHaveBeenCalledWith('/roles/r-001')
|
||||
})
|
||||
|
||||
it('assignPermissions 应调用 POST /roles/:id/permissions', async () => {
|
||||
mockPost.mockResolvedValue(undefined)
|
||||
await rolesApi.assignPermissions('r-001', ['p-1', 'p-2'])
|
||||
|
||||
expect(mockPost).toHaveBeenCalledWith('/roles/r-001/permissions', { permission_ids: ['p-1', 'p-2'] })
|
||||
})
|
||||
|
||||
it('getRolePermissions 应调用 GET /roles/:id/permissions', async () => {
|
||||
mockGet.mockResolvedValue(fakeRes)
|
||||
await rolesApi.getRolePermissions('r-001')
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('/roles/r-001/permissions')
|
||||
})
|
||||
|
||||
it('listPermissions 应调用 GET /permissions', async () => {
|
||||
mockGet.mockResolvedValue(fakeRes)
|
||||
await rolesApi.listPermissions()
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('/permissions')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user