Skip to content

Commit

Permalink
fix: 修改单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Liang-Yaxin committed Oct 11, 2024
1 parent 31cb2af commit fbe710a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion server/src/models/surveyGroup.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export class SurveyGroup extends BaseEntity {

@Column()
name: string;
}
}
2 changes: 1 addition & 1 deletion server/src/models/surveyMeta.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SurveyMeta extends BaseEntity {

@Column()
workspaceId: string;

@Column()
groupId: string;

Expand Down
23 changes: 15 additions & 8 deletions server/src/modules/survey/__test/surveyGroup.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ describe('SurveyGroupController', () => {

describe('create', () => {
it('should create a survey group', async () => {
const result = { _id: new ObjectId(), name: 'Test Group', ownerId: '123', createdAt: new Date(), updatedAt: new Date() }; // 确保这里返回的对象结构符合预期
const result = {
_id: new ObjectId(),
name: 'Test Group',
ownerId: '123',
createdAt: new Date(),
updatedAt: new Date(),
}; // 确保这里返回的对象结构符合预期
jest.spyOn(service, 'create').mockResolvedValue(result);

// 创建模拟的请求对象
Expand All @@ -63,9 +69,7 @@ describe('SurveyGroupController', () => {
},
};

expect(
await controller.create({ name: 'Test Group' }, req),
).toEqual({
expect(await controller.create({ name: 'Test Group' }, req)).toEqual({
code: 200,
data: {
id: result._id,
Expand All @@ -83,17 +87,20 @@ describe('SurveyGroupController', () => {
const result = { total: 0, list: [], allList: [] };
jest.spyOn(service, 'findAll').mockResolvedValue(result);
const mockReq = { user: { _id: new ObjectId() } };
const mockQue = { curPage: 1, pageSize: 10, name: '' }
const mockQue = { curPage: 1, pageSize: 10, name: '' };
const userId = mockReq.user._id.toString();
expect(await controller.findAll(mockReq, mockQue)).toBe({ code: 200, data: result });
expect(await controller.findAll(mockReq, mockQue)).toEqual({
code: 200,
data: result,
});
expect(service.findAll).toHaveBeenCalledWith(userId, '', 0, 10);
});
});

describe('update', () => {
it('should update a survey group', async () => {
const updatedFields = { name: 'xxx' }
const updatedResult = { raw: 'xxx', generatedMaps: [] }
const updatedFields = { name: 'xxx' };
const updatedResult = { raw: 'xxx', generatedMaps: [] };
const id = '1';
jest.spyOn(service, 'update').mockResolvedValue(updatedResult);

Expand Down
2 changes: 1 addition & 1 deletion server/src/modules/survey/controllers/survey.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class SurveyController {
createMethod,
createFrom,
workspaceId,
groupId
groupId,
});
await this.surveyConfService.createSurveyConf({
surveyId: surveyMeta._id.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class SurveyMetaController {
const survey = req.surveyMeta;
survey.title = value.title;
survey.remark = value.remark;
survey.groupId = value.groupId && value.groupId !== ''? value.groupId : null;
survey.groupId =
value.groupId && value.groupId !== '' ? value.groupId : null;

await this.surveyMetaService.editSurveyMeta({
survey,
Expand Down
3 changes: 1 addition & 2 deletions server/src/modules/survey/dto/updateSurveyGroup.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ApiProperty } from '@nestjs/swagger';
import Joi from 'joi';

export class UpdateSurveyGroupDto {

@ApiProperty({ description: '分组名称', required: true })
name: string;

Expand All @@ -11,4 +10,4 @@ export class UpdateSurveyGroupDto {
name: Joi.string().required(),
}).validate(data);
}
}
}
24 changes: 17 additions & 7 deletions server/src/modules/survey/services/surveyMeta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,15 @@ export class SurveyMetaService {
groupId?: string;
surveyIdList?: Array<string>;
}): Promise<{ data: any[]; count: number }> {
const { pageNum, pageSize, userId, username, workspaceId, groupId, surveyIdList } =
condition;
const {
pageNum,
pageSize,
userId,
username,
workspaceId,
groupId,
surveyIdList,
} = condition;
const skip = (pageNum - 1) * pageSize;
try {
const query: Record<string, any> = Object.assign(
Expand All @@ -164,11 +171,14 @@ export class SurveyMetaService {
if (condition.filter['curStatus.status']) {
query['subStatus.status'] = RECORD_SUB_STATUS.DEFAULT;
}
if (groupId && groupId !== '-1') {
query.groupId = groupId === '-2' ? {
$exists: true,
$eq: null
}: groupId;
if (groupId && groupId !== '-1') {
query.groupId =

Check warning on line 175 in server/src/modules/survey/services/surveyMeta.service.ts

View check run for this annotation

Codecov / codecov/patch

server/src/modules/survey/services/surveyMeta.service.ts#L175

Added line #L175 was not covered by tests
groupId === '-2'
? {

Check warning on line 177 in server/src/modules/survey/services/surveyMeta.service.ts

View check run for this annotation

Codecov / codecov/patch

server/src/modules/survey/services/surveyMeta.service.ts#L177

Added line #L177 was not covered by tests
$exists: true,
$eq: null,
}
: groupId;

Check warning on line 181 in server/src/modules/survey/services/surveyMeta.service.ts

View check run for this annotation

Codecov / codecov/patch

server/src/modules/survey/services/surveyMeta.service.ts#L181

Added line #L181 was not covered by tests
}
if (workspaceId) {
query.workspaceId = workspaceId;
Expand Down
4 changes: 2 additions & 2 deletions server/src/modules/survey/survey.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SurveyUIController } from './controllers/surveyUI.controller';
import { CollaboratorController } from './controllers/collaborator.controller';
import { DownloadTaskController } from './controllers/downloadTask.controller';
import { SessionController } from './controllers/session.controller';
import { SurveyGroupController } from './controllers/surveyGroup.controller'
import { SurveyGroupController } from './controllers/surveyGroup.controller';

import { SurveyConf } from 'src/models/surveyConf.entity';
import { SurveyHistory } from 'src/models/surveyHistory.entity';
Expand All @@ -40,7 +40,7 @@ import { CounterService } from '../surveyResponse/services/counter.service';
import { FileService } from '../file/services/file.service';
import { DownloadTaskService } from './services/downloadTask.service';
import { SessionService } from './services/session.service';
import { SurveyGroupService } from './services/surveyGroup.service'
import { SurveyGroupService } from './services/surveyGroup.service';
import { Session } from 'src/models/session.entity';

@Module({
Expand Down

0 comments on commit fbe710a

Please sign in to comment.