Commit cd860923 by Jaxon

test: 单元/集成测试补充 , 并修复测试问题

1 parent 8ecaf3e6
......@@ -124,6 +124,37 @@ describe('taskCenter 集成测试', () => {
// 验证 optionList 使用的是 overdueFlagOptions
expect(overdueItem.optionList).toBe(overdueFlagOptions);
});
it('表格列应该包含任务分类列', async () => {
const mockCategories = [
{ bizType: 'type1', categoryName: '分类 1' },
{ bizType: 'type2', categoryName: '分类 2' },
];
mockGetTaskCenterBizCategoryListApi.mockResolvedValue(mockCategories);
const columns = await getTableColumns();
const bizTypeCol = columns.find((col) => col.prop === 'bizTypeName');
expect(bizTypeCol).toBeDefined();
expect(bizTypeCol.label).toBe('任务分类');
expect(bizTypeCol.prop).toBe('bizTypeName');
});
it('表格搜索表单的任务分类选项应该来自 API', async () => {
const mockCategories = [
{ bizType: 'type1', categoryName: '分类 1' },
{ bizType: 'type2', categoryName: '分类 2' },
];
mockGetTaskCenterBizCategoryListApi.mockResolvedValue(mockCategories);
const schema = await getTableFormSchema();
const bizTypeItem = schema.find((item) => item.name === 'condition.bizTypes');
expect(bizTypeItem).toBeDefined();
expect(bizTypeItem.optionList).toEqual(mockCategories);
expect(bizTypeItem.optionId).toBe('bizType');
expect(bizTypeItem.optionLabel).toBe('categoryName');
});
});
describe('数据结构完整性测试', () => {
......@@ -141,7 +172,7 @@ describe('taskCenter 集成测试', () => {
const tableFields = tableColumns.map((col) => col.prop);
// 共同的字段应该都存在
const commonFields = ['taskNo', 'createdBy', 'createdDate', 'dueTime', 'completeTime'];
const commonFields = ['taskNo', 'createdBy', 'createdDate', 'dueTime', 'completeTime', 'bizTypeName'];
commonFields.forEach((field) => {
expect(detailFields).toContain(field);
expect(tableFields).toContain(field);
......
......@@ -69,7 +69,7 @@ export async function getTaskCenterTaskDescriptionSchema() {
col: 24,
labelWidth: "120px",
render: (h, { data }) => {
const names = data.assignees.map(item => item.assigneeName).join(",") || "-";
const names = (data.assignees || []).map(item => item.assigneeName).join(",") || "-";
return h("span", {}, names);
},
},
......
import { getTaskCenterTaskDescriptionSchema } from './detail.data.js';
import { getTaskCenterTaskDescriptionSchema } from "./detail.data.js";
// Mock h 函数(Vue 的渲染函数)
const createMockH = () => {
function createMockH() {
return jest.fn((tag, data, children) => ({ tag, data, children }));
};
}
describe('detail.data.js 测试', () => {
describe('getTaskCenterTaskDescriptionSchema', () => {
it('应该返回一个数组', async () => {
describe("detail.data.js 测试", () => {
describe("getTaskCenterTaskDescriptionSchema", () => {
it("应该返回一个数组", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
expect(Array.isArray(schema)).toBe(true);
});
it('应该包含基本的 schema 项', async () => {
it("应该包含基本的 schema 项", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const names = schema.map((item) => item.name).filter(Boolean);
expect(names).toContain('description');
expect(names).toContain('bizTypeName');
expect(names).toContain('status');
expect(names).toContain('priority');
expect(names).toContain('assignees');
expect(names).toContain('taskNo');
expect(names).toContain('createdBy');
expect(names).toContain('createdDate');
expect(names).toContain('dueTime');
expect(names).toContain('completeTime');
const names = schema.map(item => item.name).filter(Boolean);
expect(names).toContain("description");
expect(names).toContain("bizTypeName");
expect(names).toContain("status");
expect(names).toContain("priority");
expect(names).toContain("assignees");
expect(names).toContain("taskNo");
expect(names).toContain("createdBy");
expect(names).toContain("createdDate");
expect(names).toContain("dueTime");
expect(names).toContain("completeTime");
});
it('每个 schema 项应该包含 name 和 label 属性', async () => {
it("每个 schema 项应该包含 name 和 label 属性", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const itemsWithName = schema.filter((item) => item.name);
const itemsWithName = schema.filter(item => item.name);
itemsWithName.forEach((item) => {
expect(item).toHaveProperty('name');
expect(item).toHaveProperty('label');
expect(item).toHaveProperty("name");
expect(item).toHaveProperty("label");
});
});
it('description 字段应该有正确的配置', async () => {
it("description 字段应该有正确的配置", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const descriptionItem = schema.find((item) => item.name === 'description');
const descriptionItem = schema.find(item => item.name === "description");
expect(descriptionItem).toEqual({
name: 'description',
label: '任务描述',
name: "description",
label: "任务描述",
col: 24,
labelWidth: '120px',
labelWidth: "120px",
tooltip: true,
parentProps: {
style: {
display: 'inline-block',
height: '32px',
width: 'auto',
maxWidth: '100%',
verticalAlign: 'top',
display: "inline-block",
height: "32px",
width: "auto",
maxWidth: "100%",
verticalAlign: "top",
},
},
render: expect.any(Function),
......@@ -62,29 +62,29 @@ describe('detail.data.js 测试', () => {
});
});
it('status 字段应该有 render 函数', async () => {
it("status 字段应该有 render 函数", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const statusItem = schema.find((item) => item.name === 'status');
const statusItem = schema.find(item => item.name === "status");
expect(statusItem.render).toBeInstanceOf(Function);
});
it('priority 字段应该有 render 函数', async () => {
it("priority 字段应该有 render 函数", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const priorityItem = schema.find((item) => item.name === 'priority');
const priorityItem = schema.find(item => item.name === "priority");
expect(priorityItem.render).toBeInstanceOf(Function);
});
it('assignees 字段的 render 函数应该正确处理执行人列表', async () => {
it("assignees 字段的 render 函数应该正确处理执行人列表", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const assigneesItem = schema.find((item) => item.name === 'assignees');
const assigneesItem = schema.find(item => item.name === "assignees");
const h = createMockH();
const data = {
assignees: [
{ assigneeName: '张三' },
{ assigneeName: '李四' },
{ assigneeName: "张三" },
{ assigneeName: "李四" },
],
};
......@@ -94,9 +94,9 @@ describe('detail.data.js 测试', () => {
expect(h).toHaveBeenCalled();
});
it('assignees 字段的 render 函数应该处理空列表', async () => {
it("assignees 字段的 render 函数应该处理空列表", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const assigneesItem = schema.find((item) => item.name === 'assignees');
const assigneesItem = schema.find(item => item.name === "assignees");
const h = createMockH();
const data = { assignees: [] };
......@@ -106,29 +106,41 @@ describe('detail.data.js 测试', () => {
expect(h).toHaveBeenCalled();
});
it('description 的 isHide 函数应该在 formModel.description 为空时返回 true', async () => {
it("assignees 字段的 render 函数应该处理 undefined 情况", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const descriptionItem = schema.find((item) => item.name === 'description');
const assigneesItem = schema.find(item => item.name === "assignees");
const h = createMockH();
const data = { };
const result = assigneesItem.render(h, { data });
expect(h).toHaveBeenCalledWith("span", {}, "-");
});
it("description 的 isHide 函数应该在 formModel.description 为空时返回 true", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const descriptionItem = schema.find(item => item.name === "description");
const result = descriptionItem.isHide({ formModel: {} });
expect(result).toBe(true);
});
it('description 的 isHide 函数应该在 formModel.description 有值时返回 false', async () => {
it("description 的 isHide 函数应该在 formModel.description 有值时返回 false", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const descriptionItem = schema.find((item) => item.name === 'description');
const descriptionItem = schema.find(item => item.name === "description");
const result = descriptionItem.isHide({
formModel: { description: '测试描述' },
formModel: { description: "测试描述" },
});
expect(result).toBe(false);
});
it('应该包含两个 divider 项', async () => {
it("应该包含两个 divider 项", async () => {
const schema = await getTaskCenterTaskDescriptionSchema();
const dividers = schema.filter((item) => item.divider === true);
const dividers = schema.filter(item => item.divider === true);
expect(dividers).toHaveLength(2);
});
......
......@@ -15,6 +15,17 @@ jest.mock("@/views/residentManage/taskCenter/api/index.js", () => ({
getTaskCenterBizCategoryListApi: mockGetTaskCenterBizCategoryListApi,
}));
// Mock lodash-es 的 keyBy
jest.mock("lodash-es", () => ({
keyBy: jest.fn((arr, key) => {
const result = {};
arr.forEach(item => {
result[item[key]] = item;
});
return result;
}),
}));
// 在 mock 之后才导入被测试模块
const { getTableFormSchema, getTableColumns } = require("./table.data.js");
......@@ -268,6 +279,18 @@ describe("table.data.js 测试", () => {
expect(dueTimeCol.valueClass).toBeInstanceOf(Function);
});
it("应该包含任务分类列", async () => {
const columns = await getTableColumns();
const bizTypeCol = columns.find(col => col.prop === "bizTypeName");
expect(bizTypeCol).toEqual({
label: "任务分类",
prop: "bizTypeName",
width: "180px",
align: "left",
});
});
it("应该包含 10 个列", async () => {
const columns = await getTableColumns();
expect(columns).toHaveLength(10);
......
......@@ -37,6 +37,7 @@
<el-form-item prop="userName">
<el-input v-model="formLogin.userName"
v-focus
data-testid="username"
placeholder="账户"
type="text"
clearable
......@@ -51,6 +52,7 @@
</el-form-item>
<el-form-item prop="password">
<el-input v-model="formLogin.password"
data-testid="password"
placeholder="密码"
type="password"
clearable
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!