Skip to content

Commit ea8e901

Browse files
authored
feat: 修改补充单测 (#437)
1 parent 351f18f commit ea8e901

37 files changed

+1182
-664
lines changed

server/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@
9595
"^.+\\.(t|j)s$": "ts-jest"
9696
},
9797
"collectCoverageFrom": [
98-
"**/*.(t|j)s"
98+
"**/*.(t|j)s",
99+
"!**/*.module.ts",
100+
"!**/upgrade.*.ts"
99101
],
100102
"coverageDirectory": "../coverage",
101103
"testEnvironment": "node",

server/src/models/__test/base.entity.spec.ts

-30
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { SurveyMeta } from '../surveyMeta.entity';
2+
import { RECORD_STATUS, RECORD_SUB_STATUS } from 'src/enums';
3+
4+
// 模拟日期
5+
const mockDateNow = Date.now();
6+
7+
describe('SurveyMeta Entity', () => {
8+
let surveyMeta: SurveyMeta;
9+
10+
// 在每个测试之前,初始化 SurveyMeta 实例
11+
beforeEach(() => {
12+
surveyMeta = new SurveyMeta();
13+
// 模拟 Date.now() 返回固定的时间
14+
jest.spyOn(Date, 'now').mockReturnValue(mockDateNow);
15+
});
16+
17+
afterEach(() => {
18+
jest.restoreAllMocks(); // 每次测试后还原所有 mock
19+
});
20+
21+
it('should set default curStatus and subStatus on insert when they are not provided', () => {
22+
surveyMeta.initDefaultInfo();
23+
24+
// 验证 curStatus 是否被初始化为默认值
25+
expect(surveyMeta.curStatus).toEqual({
26+
status: RECORD_STATUS.NEW,
27+
date: mockDateNow,
28+
});
29+
30+
// 验证 statusList 是否包含 curStatus
31+
expect(surveyMeta.statusList).toEqual([
32+
{
33+
status: RECORD_STATUS.NEW,
34+
date: mockDateNow,
35+
},
36+
]);
37+
38+
// 验证 subStatus 是否被初始化为默认值
39+
expect(surveyMeta.subStatus).toEqual({
40+
status: RECORD_SUB_STATUS.DEFAULT,
41+
date: mockDateNow,
42+
});
43+
});
44+
45+
it('should initialize statusList if curStatus is provided but statusList is empty', () => {
46+
surveyMeta.curStatus = null;
47+
48+
surveyMeta.initDefaultInfo();
49+
50+
expect(surveyMeta.statusList).toEqual([
51+
{
52+
status: RECORD_STATUS.NEW,
53+
date: expect.any(Number),
54+
},
55+
]);
56+
});
57+
});

server/src/modules/auth/__test/user.service.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ describe('UserService', () => {
206206
it('should return a list of users by username', async () => {
207207
const username = 'test';
208208
const userList = [
209-
{ _id: new ObjectId(), username: 'testUser1', createDate: new Date() },
210-
{ _id: new ObjectId(), username: 'testUser2', createDate: new Date() },
209+
{ _id: new ObjectId(), username: 'testUser1', createdAt: new Date() },
210+
{ _id: new ObjectId(), username: 'testUser2', createdAt: new Date() },
211211
];
212212

213213
jest
@@ -226,7 +226,7 @@ describe('UserService', () => {
226226
},
227227
skip: 0,
228228
take: 10,
229-
select: ['_id', 'username', 'createDate'],
229+
select: ['_id', 'username', 'createdAt'],
230230
});
231231
expect(result).toEqual(userList);
232232
});
@@ -237,12 +237,12 @@ describe('UserService', () => {
237237
{
238238
_id: new ObjectId(idList[0]),
239239
username: 'testUser1',
240-
createDate: new Date(),
240+
createdAt: new Date(),
241241
},
242242
{
243243
_id: new ObjectId(idList[1]),
244244
username: 'testUser2',
245-
createDate: new Date(),
245+
createdAt: new Date(),
246246
},
247247
];
248248

@@ -258,7 +258,7 @@ describe('UserService', () => {
258258
$in: idList.map((id) => new ObjectId(id)),
259259
},
260260
},
261-
select: ['_id', 'username', 'createDate'],
261+
select: ['_id', 'username', 'createdAt'],
262262
});
263263
expect(result).toEqual(userList);
264264
});

server/src/modules/message/__test/messagePushingTask.dto.spec.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
MESSAGE_PUSHING_TYPE,
99
MESSAGE_PUSHING_HOOK,
1010
} from 'src/enums/messagePushing';
11-
import { RECORD_STATUS } from 'src/enums';
1211

1312
describe('MessagePushingTaskDto', () => {
1413
let dto: MessagePushingTaskDto;
@@ -34,9 +33,9 @@ describe('MessagePushingTaskDto', () => {
3433
});
3534

3635
it('should have a type', () => {
37-
dto.type = MESSAGE_PUSHING_TYPE.HTTP; // Set your desired type here
36+
dto.type = MESSAGE_PUSHING_TYPE.HTTP;
3837
expect(dto.type).toBeDefined();
39-
expect(dto.type).toEqual(MESSAGE_PUSHING_TYPE.HTTP); // Adjust based on your enum
38+
expect(dto.type).toEqual(MESSAGE_PUSHING_TYPE.HTTP);
4039
});
4140

4241
it('should have a push address', () => {
@@ -46,13 +45,13 @@ describe('MessagePushingTaskDto', () => {
4645
});
4746

4847
it('should have a trigger hook', () => {
49-
dto.triggerHook = MESSAGE_PUSHING_HOOK.RESPONSE_INSERTED; // Set your desired hook here
48+
dto.triggerHook = MESSAGE_PUSHING_HOOK.RESPONSE_INSERTED;
5049
expect(dto.triggerHook).toBeDefined();
51-
expect(dto.triggerHook).toEqual(MESSAGE_PUSHING_HOOK.RESPONSE_INSERTED); // Adjust based on your enum
50+
expect(dto.triggerHook).toEqual(MESSAGE_PUSHING_HOOK.RESPONSE_INSERTED);
5251
});
5352

5453
it('should have an array of surveys', () => {
55-
dto.surveys = ['survey1', 'survey2']; // Set your desired surveys here
54+
dto.surveys = ['survey1', 'survey2'];
5655
expect(dto.surveys).toBeDefined();
5756
expect(dto.surveys).toEqual(['survey1', 'survey2']);
5857
});
@@ -62,13 +61,6 @@ describe('MessagePushingTaskDto', () => {
6261
expect(dto.owner).toBeDefined();
6362
expect(dto.owner).toBe('test_owner');
6463
});
65-
66-
it('should have current status', () => {
67-
dto.curStatus = { status: RECORD_STATUS.NEW, date: Date.now() };
68-
expect(dto.curStatus).toBeDefined();
69-
expect(dto.curStatus.status).toEqual(RECORD_STATUS.NEW);
70-
expect(dto.curStatus.date).toBeDefined();
71-
});
7264
});
7365

7466
describe('CodeDto', () => {

server/src/modules/message/__test/messagePushingTask.service.spec.ts

+79-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ describe('MessagePushingTaskService', () => {
117117
expect(result).toEqual(tasks);
118118
expect(repository.find).toHaveBeenCalledWith({
119119
where: {
120+
isDeleted: {
121+
$ne: true,
122+
},
120123
ownerId: mockOwnerId,
121124
surveys: { $all: [surveyId] },
122125
triggerHook: hook,
@@ -144,9 +147,19 @@ describe('MessagePushingTaskService', () => {
144147
where: {
145148
ownerId: mockOwnerId,
146149
_id: new ObjectId(taskId),
150+
isDeleted: {
151+
$ne: true,
152+
},
147153
},
148154
});
149155
});
156+
it('should throw an error when message pushing task is not found', async () => {
157+
const taskId = '65afc62904d5db18534c0f78';
158+
jest.spyOn(repository, 'findOne').mockResolvedValue(null); // 模拟未找到任务
159+
const mockOwnerId = '66028642292c50f8b71a9eee';
160+
161+
await expect(service.findOne({ id: taskId, ownerId: mockOwnerId }));
162+
});
150163
});
151164

152165
describe('update', () => {
@@ -183,6 +196,20 @@ describe('MessagePushingTaskService', () => {
183196
});
184197
expect(repository.save).toHaveBeenCalledWith(updatedTask);
185198
});
199+
it('should throw an error if the task to be updated is not found', async () => {
200+
const taskId = '65afc62904d5db18534c0f78';
201+
const updateDto: UpdateMessagePushingTaskDto = { name: 'Updated Task' };
202+
jest.spyOn(repository, 'findOne').mockResolvedValue(null); // 模拟任务未找到
203+
const mockOwnerId = '66028642292c50f8b71a9eee';
204+
205+
await expect(
206+
service.update({
207+
ownerId: mockOwnerId,
208+
id: taskId,
209+
updateData: updateDto,
210+
}),
211+
).rejects.toThrow(`Message pushing task with id ${taskId} not found`);
212+
});
186213
});
187214

188215
describe('remove', () => {
@@ -204,16 +231,40 @@ describe('MessagePushingTaskService', () => {
204231
expect(result).toEqual(updateResult);
205232
expect(repository.updateOne).toHaveBeenCalledWith(
206233
{
207-
ownerId: mockOperatorId,
208234
_id: new ObjectId(taskId),
209235
},
210236
{
211237
$set: {
212238
isDeleted: true,
239+
operatorId: mockOperatorId,
240+
operator: mockOperator,
241+
deletedAt: expect.any(Date),
213242
},
214243
},
215244
);
216245
});
246+
it('should throw an error if the task to be removed is not found', async () => {
247+
const taskId = '65afc62904d5db18534c0f78';
248+
jest
249+
.spyOn(repository, 'updateOne')
250+
.mockResolvedValue({ modifiedCount: 0 }); // 模拟删除失败
251+
const mockOperatorId = '66028642292c50f8b71a9eee';
252+
const mockOperator = 'mockOperator';
253+
254+
const result = await service.remove({
255+
id: taskId,
256+
operatorId: mockOperatorId,
257+
operator: mockOperator,
258+
});
259+
260+
expect(result.modifiedCount).toBe(0);
261+
expect(repository.updateOne).toHaveBeenCalledWith(
262+
{
263+
_id: new ObjectId(taskId),
264+
},
265+
expect.any(Object),
266+
);
267+
});
217268
});
218269

219270
describe('surveyAuthorizeTask', () => {
@@ -243,7 +294,34 @@ describe('MessagePushingTaskService', () => {
243294
$push: {
244295
surveys: surveyId,
245296
},
297+
$set: {
298+
updatedAt: expect.any(Date),
299+
},
300+
},
301+
);
302+
});
303+
it('should not add the surveyId if it already exists in the task', async () => {
304+
const taskId = '65afc62904d5db18534c0f78';
305+
const surveyId = '65af380475b64545e5277dd9';
306+
const mockOwnerId = '66028642292c50f8b71a9eee';
307+
308+
jest
309+
.spyOn(repository, 'updateOne')
310+
.mockResolvedValue({ modifiedCount: 0 }); // 模拟重复添加
311+
const result = await service.surveyAuthorizeTask({
312+
taskId,
313+
surveyId,
314+
ownerId: mockOwnerId,
315+
});
316+
317+
expect(result.modifiedCount).toBe(0);
318+
expect(repository.updateOne).toHaveBeenCalledWith(
319+
{
320+
_id: new ObjectId(taskId),
321+
surveys: { $nin: [surveyId] }, // 确保只有不包含时才插入
322+
ownerId: mockOwnerId,
246323
},
324+
expect.any(Object),
247325
);
248326
});
249327
});

server/src/modules/message/__test/updateMessagePushingTask.dto.spec.ts

-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,4 @@ describe('UpdateMessagePushingTaskDto', () => {
3737
dto.surveys = null;
3838
expect(dto.surveys).toBeNull();
3939
});
40-
41-
it('should have a nullable curStatus', () => {
42-
dto.curStatus = null;
43-
expect(dto.curStatus).toBeNull();
44-
});
4540
});

server/src/modules/message/services/messagePushingTask.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ export class MessagePushingTaskService {
6363
});
6464
}
6565

66-
async findOne({
66+
findOne({
6767
id,
6868
ownerId,
6969
}: {
7070
id: string;
7171
ownerId: string;
7272
}): Promise<MessagePushingTask> {
73-
return await this.messagePushingTaskRepository.findOne({
73+
return this.messagePushingTaskRepository.findOne({
7474
where: {
7575
ownerId,
7676
_id: new ObjectId(id),

0 commit comments

Comments
 (0)