table.data.spec.js 9.18 KB
/**
 * table.data.js 单元测试
 */

// 先 mock 所有依赖
const mockRequest = jest.fn();
const mockRequestMethods = { POST: "POST", GET: "GET" };

jest.mock("@/api/request", () => mockRequest);
jest.mock("@/api/request.methods", () => mockRequestMethods);

// Mock API 模块
const mockGetTaskCenterBizCategoryListApi = jest.fn();
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");

describe("table.data.js 测试", () => {
	beforeEach(() => {
		jest.clearAllMocks();
	});

	describe("getTableFormSchema", () => {
		it("应该返回一个数组", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			expect(Array.isArray(schema)).toBe(true);
		});

		it("应该调用 getTaskCenterBizCategoryListApi 获取分类数据", async () => {
			const mockCategories = [
				{ bizType: "type1", categoryName: "分类 1" },
				{ bizType: "type2", categoryName: "分类 2" },
			];
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue(mockCategories);

			await getTableFormSchema();

			expect(mockGetTaskCenterBizCategoryListApi).toHaveBeenCalled();
		});

		it("应该包含任务编号搜索项", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			const taskNoItem = schema.find(item => item.name === "condition.taskNo");

			expect(taskNoItem).toEqual({
				label: "任务编号",
				name: "condition.taskNo",
				type: "input",
				maxlength: 20,
			});
		});

		it("应该包含任务标题搜索项", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			const titleItem = schema.find(item => item.name === "condition.title");

			expect(titleItem).toEqual({
				label: "任务标题",
				name: "condition.title",
				type: "input",
				maxlength: 100,
			});
		});

		it("应该包含优先级搜索项且为多选", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			const priorityItem = schema.find(item => item.name === "condition.priorities");

			expect(priorityItem).toHaveProperty("type", "select");
			expect(priorityItem).toHaveProperty("multiple", true);
			expect(priorityItem).toHaveProperty("isClear", true);
			expect(priorityItem).toHaveProperty("optionList");
		});

		it("应该包含完成状态搜索项", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			const statusItem = schema.find(item => item.name === "condition.status");

			expect(statusItem).toEqual({
				label: "完成状态",
				name: "condition.status",
				type: "select",
				isClear: true,
				optionList: expect.any(Array),
				optionId: "value",
				optionLabel: "label",
			});
		});

		it("应该包含任务分类搜索项且为多选", 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).toHaveProperty("type", "select");
			expect(bizTypeItem).toHaveProperty("multiple", true);
			expect(bizTypeItem).toHaveProperty("isClear", true);
			expect(bizTypeItem.optionList).toEqual(mockCategories);
			expect(bizTypeItem.optionId).toBe("bizType");
			expect(bizTypeItem.optionLabel).toBe("categoryName");
		});

		it("应该包含执行人搜索项", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			const assigneeItem = schema.find(
				item => item.name === "condition.assigneeName",
			);

			expect(assigneeItem).toEqual({
				label: "执行人",
				name: "condition.assigneeName",
				type: "input",
				maxlength: 20,
			});
		});

		it("应该包含创建人搜索项", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			const creatorItem = schema.find(
				item => item.name === "condition.creatorName",
			);

			expect(creatorItem).toEqual({
				label: "创建人",
				name: "condition.creatorName",
				type: "input",
				maxlength: 20,
			});
		});

		it("应该包含是否逾期搜索项", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			const overdueItem = schema.find(
				item => item.name === "condition.overdueFlag",
			);

			expect(overdueItem).toHaveProperty("type", "select");
			expect(overdueItem).toHaveProperty("isClear", true);
			expect(overdueItem).toHaveProperty("optionList");
		});

		it("应该包含 8 个搜索项", async () => {
			mockGetTaskCenterBizCategoryListApi.mockResolvedValue([]);
			const schema = await getTableFormSchema();
			expect(schema).toHaveLength(8);
		});
	});

	describe("getTableColumns", () => {
		it("应该返回一个数组", async () => {
			const columns = await getTableColumns();
			expect(Array.isArray(columns)).toBe(true);
		});

		it("应该包含任务编号列", async () => {
			const columns = await getTableColumns();
			const taskNoCol = columns.find(col => col.prop === "taskNo");

			expect(taskNoCol).toEqual({
				label: "任务编号",
				prop: "taskNo",
				width: "180px",
				align: "left",
			});
		});

		it("应该包含任务标题列", async () => {
			const columns = await getTableColumns();
			const titleCol = columns.find(col => col.prop === "title");

			expect(titleCol).toEqual({
				label: "任务标题",
				prop: "title",
				minWidth: "200px",
				align: "left",
			});
		});

		it("应该包含优先级列且使用 tag 显示", async () => {
			const columns = await getTableColumns();
			const priorityCol = columns.find(col => col.prop === "priority");

			expect(priorityCol).toHaveProperty("useTag", true);
			expect(priorityCol).toHaveProperty("tagOptions");
			expect(priorityCol.label).toBe("优先级");
		});

		it("应该包含执行人列且有 formatter 函数", async () => {
			const columns = await getTableColumns();
			const assigneeCol = columns.find(col => col.prop === "assignees");

			expect(assigneeCol.formatter).toBeInstanceOf(Function);

			// 测试 formatter 函数
			const row = {
				assignees: [
					{ assigneeName: "张三" },
					{ assigneeName: "李四" },
				],
			};
			const result = assigneeCol.formatter(row);
			expect(result).toBe("张三,李四");
		});

		it("执行人列的 formatter 函数应该处理空列表", async () => {
			const columns = await getTableColumns();
			const assigneeCol = columns.find(col => col.prop === "assignees");

			const row = { assignees: [] };
			const result = assigneeCol.formatter(row);
			expect(result).toBe("-");
		});

		it("执行人列的 formatter 函数应该处理 undefined", async () => {
			const columns = await getTableColumns();
			const assigneeCol = columns.find(col => col.prop === "assignees");

			const row = {};
			const result = assigneeCol.formatter(row);
			expect(result).toBe("-");
		});

		it("应该包含完成状态列且有 formatter 函数", async () => {
			const columns = await getTableColumns();
			const statusCol = columns.find(col => col.prop === "status");

			expect(statusCol.formatter).toBeInstanceOf(Function);
			expect(statusCol).toHaveProperty("valueClass");
		});

		it("完成状态列的 formatter 函数应该正确处理状态值", async () => {
			const columns = await getTableColumns();
			const statusCol = columns.find(col => col.prop === "status");

			expect(statusCol.formatter({ status: "UNFINISHED" })).toBe("未完成");
			expect(statusCol.formatter({ status: "FINISHED" })).toBe("已完成");
			expect(statusCol.formatter({ status: "" })).toBe("-");
		});

		it("完成状态列的 valueClass 函数应该返回正确的类名", async () => {
			const columns = await getTableColumns();
			const statusCol = columns.find(col => col.prop === "status");

			expect(statusCol.valueClass({ status: "UNFINISHED" })).toBe("yellow-dot");
			expect(statusCol.valueClass({ status: "FINISHED" })).toBe("green-dot");
		});

		it("应该包含截止时间列且有 valueClass 函数", async () => {
			const columns = await getTableColumns();
			const dueTimeCol = columns.find(col => col.prop === "dueTime");

			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);
		});
	});
});