table.data.spec.js
9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* 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);
});
});
});