index.spec.js
4.94 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
/**
* API 层单元测试
* 直接 mock request 返回值,不依赖实际 request 模块
*/
// 先 mock 所有依赖
const mockRequest = jest.fn();
const mockRequestMethods = { POST: 'POST', GET: 'GET' };
// 使用 jest.mock 的工厂函数形式
jest.mock('@/api/request', () => mockRequest);
jest.mock('@/api/request.methods', () => mockRequestMethods);
// 在 mock 之后才导入被测试模块
const {
getTaskCenterListApi,
getTaskCenterBizCategoryListApi,
getTaskCenterDetailApi,
getTaskCenterCountApi,
} = require('./index.js');
describe('taskCenter API 测试', () => {
beforeEach(() => {
// 每次测试前重置 mock
jest.clearAllMocks();
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('getTaskCenterListApi', () => {
it('应该调用正确的 API 方法获取任务列表', async () => {
// Arrange
const mockData = { page: 1, pageSize: 10 };
const mockResponse = {
list: [{ taskId: '1', title: '测试任务' }],
total: 1,
};
mockRequest.mockResolvedValue(mockResponse);
// Act
const result = await getTaskCenterListApi(mockData);
// Assert
expect(mockRequest).toHaveBeenCalledWith(
{ ...mockData, method: 'dj.api.taskcenter.task.page' },
mockRequestMethods.POST,
);
expect(result).toEqual(mockResponse);
});
it('应该在没有参数时使用默认空对象', async () => {
// Arrange
const mockResponse = { list: [], total: 0 };
mockRequest.mockResolvedValue(mockResponse);
// Act
const result = await getTaskCenterListApi();
// Assert
expect(mockRequest).toHaveBeenCalledWith(
{ method: 'dj.api.taskcenter.task.page' },
mockRequestMethods.POST,
);
expect(result).toEqual(mockResponse);
});
});
describe('getTaskCenterBizCategoryListApi', () => {
it('应该调用正确的 API 方法获取任务分类列表', async () => {
// Arrange
const mockResponse = [
{ bizType: 'type1', categoryName: '分类 1' },
{ bizType: 'type2', categoryName: '分类 2' },
];
mockRequest.mockResolvedValue(mockResponse);
// Act
const result = await getTaskCenterBizCategoryListApi();
// Assert
expect(mockRequest).toHaveBeenCalledWith(
{ method: 'dj.api.taskcenter.bizcategory.list' },
mockRequestMethods.POST,
);
expect(result).toEqual(mockResponse);
});
it('应该支持传入自定义参数', async () => {
// Arrange
const mockData = { status: 'active' };
const mockResponse = [{ bizType: 'type1', categoryName: '分类 1' }];
mockRequest.mockResolvedValue(mockResponse);
// Act
const result = await getTaskCenterBizCategoryListApi(mockData);
// Assert
expect(mockRequest).toHaveBeenCalledWith(
{ ...mockData, method: 'dj.api.taskcenter.bizcategory.list' },
mockRequestMethods.POST,
);
expect(result).toEqual(mockResponse);
});
});
describe('getTaskCenterDetailApi', () => {
it('应该调用正确的 API 方法获取任务详情', async () => {
// Arrange
const mockData = { taskId: 'test-id-123' };
const mockResponse = {
taskId: 'test-id-123',
title: '测试任务详情',
description: '任务描述',
};
mockRequest.mockResolvedValue(mockResponse);
// Act
const result = await getTaskCenterDetailApi(mockData);
// Assert
expect(mockRequest).toHaveBeenCalledWith(
{ ...mockData, method: 'dj.api.taskcenter.task.detail' },
mockRequestMethods.POST,
);
expect(result).toEqual(mockResponse);
});
it('应该在没有参数时使用默认空对象', async () => {
// Arrange
const mockResponse = { taskId: '', title: '' };
mockRequest.mockResolvedValue(mockResponse);
// Act
const result = await getTaskCenterDetailApi();
// Assert
expect(mockRequest).toHaveBeenCalledWith(
{ method: 'dj.api.taskcenter.task.detail' },
mockRequestMethods.POST,
);
expect(result).toEqual(mockResponse);
});
});
describe('getTaskCenterCountApi', () => {
it('应该调用正确的 API 方法获取任务统计', async () => {
// Arrange
const mockData = { status: 'UNFINISHED' };
const mockResponse = {
totalCount: 100,
finishedCount: 60,
unfinishedCount: 40,
overdueCount: 10,
};
mockRequest.mockResolvedValue(mockResponse);
// Act
const result = await getTaskCenterCountApi(mockData);
// Assert
expect(mockRequest).toHaveBeenCalledWith(
{ ...mockData, method: 'dj.api.taskcenter.task.count' },
mockRequestMethods.POST,
);
expect(result).toEqual(mockResponse);
});
it('应该在没有参数时使用默认空对象', async () => {
// Arrange
const mockResponse = {
totalCount: 0,
finishedCount: 0,
unfinishedCount: 0,
overdueCount: 0,
};
mockRequest.mockResolvedValue(mockResponse);
// Act
const result = await getTaskCenterCountApi();
// Assert
expect(mockRequest).toHaveBeenCalledWith(
{ method: 'dj.api.taskcenter.task.count' },
mockRequestMethods.POST,
);
expect(result).toEqual(mockResponse);
});
});
});