前端权限管理:添加单元测试

新增测试文件:
- ApiPermissions.test.ts: 权限管理主页面测试
- ApiEndpointPermissions.test.ts: 接口级权限矩阵组件测试
- PermissionMonitoring.test.ts: 权限监控仪表板组件测试

测试覆盖:
- 组件渲染验证
- 事件发出验证
- 搜索和筛选功能测试
- 数据显示验证

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Cheng Zhou
2026-05-14 09:23:17 +08:00
parent c73c00d932
commit 35acf96d6b
4 changed files with 345 additions and 5 deletions
@@ -0,0 +1,92 @@
import { describe, it, expect } from "vitest";
import { mount } from "@vue/test-utils";
import ApiEndpointPermissions from "@/components/ApiEndpointPermissions.vue";
import type { ApiEndpointPermissionsResponse } from "@/types/api";
describe("ApiEndpointPermissions.vue", () => {
const mockMatrix: ApiEndpointPermissionsResponse = {
PM: {
"POST:/subjects": true,
"GET:/subjects": true,
"GET:/subjects/{id}": true,
"PATCH:/subjects/{id}": true,
"DELETE:/subjects/{id}": true,
},
CRA: {
"POST:/subjects": true,
"GET:/subjects": true,
"GET:/subjects/{id}": true,
"PATCH:/subjects/{id}": true,
"DELETE:/subjects/{id}": false,
},
};
it("renders permission matrix table", () => {
const wrapper = mount(ApiEndpointPermissions, {
props: {
project: { id: "test-id", name: "Test Project" },
matrix: mockMatrix,
},
global: {
stubs: {
ElTable: false,
ElTableColumn: false,
ElCheckbox: false,
},
},
});
expect(wrapper.find(".api-permissions").exists()).toBe(true);
});
it("emits update event when permission changes", async () => {
const wrapper = mount(ApiEndpointPermissions, {
props: {
project: { id: "test-id", name: "Test Project" },
matrix: mockMatrix,
},
global: {
stubs: {
ElTable: false,
ElTableColumn: false,
ElCheckbox: false,
},
},
});
// 模拟权限变更
const checkboxes = wrapper.findAll("input[type='checkbox']");
if (checkboxes.length > 0) {
await checkboxes[0].setValue(false);
}
// 验证是否发出了 update 事件
expect(wrapper.emitted("update")).toBeTruthy();
});
it("filters endpoints by search text", async () => {
const wrapper = mount(ApiEndpointPermissions, {
props: {
project: { id: "test-id", name: "Test Project" },
matrix: mockMatrix,
},
global: {
stubs: {
ElTable: false,
ElTableColumn: false,
ElCheckbox: false,
ElInput: false,
ElSelect: false,
},
},
});
// 获取搜索输入框
const searchInput = wrapper.find("input[placeholder*='搜索']");
if (searchInput.exists()) {
await searchInput.setValue("subjects");
// 验证过滤逻辑
expect(wrapper.vm.searchText).toBe("subjects");
}
});
});