Skip to content
Snippets Groups Projects
Commit e324173f authored by Bastien DUMONT's avatar Bastien DUMONT :angel:
Browse files

Merge branch 'tests/jobsGroup' into 'dev'

test(jobsGroup): increase coverage

See merge request !392
parents 7e92c0cd d716210d
No related branches found
No related tags found
2 merge requests!404V3.1.0 (sans impression),!392test(jobsGroup): increase coverage
...@@ -56,34 +56,19 @@ describe('JobsGroupsController', () => { ...@@ -56,34 +56,19 @@ describe('JobsGroupsController', () => {
describe('findAll', () => { describe('findAll', () => {
it('should findAll', async () => { it('should findAll', async () => {
jobsGroupsServiceMock.findAll.mockResolvedValue([ await jobsGroupsController.findAll();
{ expect(jobsGroupsServiceMock.findAll).toHaveBeenCalled();
_id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
name: 'Pilotage de projet',
},
{
_id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
name: 'Coordination',
},
]);
const reply = await jobsGroupsController.findAll();
expect(reply.length).toBe(2);
}); });
}); });
describe('createJobsGroups', () => { describe('findAllWithJobs', () => {
it('should create job group `Dev`', async () => { it('should findAll with jobs', async () => {
const newJobsGroups: CreateJobsGroupsDto = { await jobsGroupsController.findAllWithJobs();
name: 'Dev', expect(jobsGroupsServiceMock.findAll).toHaveBeenCalledWith(true);
};
const newCreatedJobsGroups: JobsGroups = {
name: 'Dev',
};
jobsGroupsServiceMock.findByName.mockResolvedValueOnce(null);
jobsGroupsServiceMock.create.mockResolvedValueOnce(newCreatedJobsGroups);
const createReply = await jobsGroupsController.createJobsGroups(newJobsGroups);
expect(createReply).toEqual(newCreatedJobsGroups);
}); });
});
describe('createJobsGroups', () => {
it('should throw error on already existing job group `Dev`', async () => { it('should throw error on already existing job group `Dev`', async () => {
const newJobsGroups: CreateJobsGroupsDto = { const newJobsGroups: CreateJobsGroupsDto = {
name: 'Dev', name: 'Dev',
...@@ -97,9 +82,22 @@ describe('JobsGroupsController', () => { ...@@ -97,9 +82,22 @@ describe('JobsGroupsController', () => {
expect(true).toBe(false); expect(true).toBe(false);
} catch (e) { } catch (e) {
expect(e.message).toBe('Job group already exist'); expect(e.message).toBe('Job group already exist');
expect(e.status).toBe(422); expect(e.status).toBe(HttpStatus.UNPROCESSABLE_ENTITY);
} }
}); });
it('should create job group `Dev`', async () => {
const newJobsGroups: CreateJobsGroupsDto = {
name: 'Dev',
};
const newCreatedJobsGroups: JobsGroups = {
name: 'Dev',
};
jobsGroupsServiceMock.findByName.mockResolvedValueOnce(null);
jobsGroupsServiceMock.create.mockResolvedValueOnce(newCreatedJobsGroups);
await jobsGroupsController.createJobsGroups(newJobsGroups);
expect(jobsGroupsServiceMock.create).toHaveBeenCalled();
});
}); });
describe('Edit Job Group', () => { describe('Edit Job Group', () => {
...@@ -118,14 +116,15 @@ describe('JobsGroupsController', () => { ...@@ -118,14 +116,15 @@ describe('JobsGroupsController', () => {
deletedCount: 1, deletedCount: 1,
}); });
it('should delete job group', async () => { it('should not be able to find job group', async () => {
jobsGroupsServiceMock.findOne.mockResolvedValueOnce({ jobsGroupsServiceMock.findOne.mockResolvedValueOnce(null);
_id: new Types.ObjectId('6231aefe76598527c8d0b5ba'), try {
name: 'Dev', await jobsGroupsController.deleteJobsGroups({ id: '6231aefe76598527c8d0b5ba' });
}); expect(true).toBe(false);
jobServiceMock.getJobsGroupsAttachedJobs.mockResolvedValueOnce([]); } catch (e) {
const reply = await jobsGroupsController.deleteJobsGroups({ id: '6231aefe76598527c8d0b5ba' }); expect(e.message).toEqual('Job group not found');
expect(reply).toBeTruthy(); expect(e.status).toEqual(HttpStatus.NOT_FOUND);
}
}); });
it('should not delete job group if a job is linked', async () => { it('should not delete job group if a job is linked', async () => {
...@@ -142,5 +141,15 @@ describe('JobsGroupsController', () => { ...@@ -142,5 +141,15 @@ describe('JobsGroupsController', () => {
expect(e.status).toEqual(HttpStatus.FORBIDDEN); expect(e.status).toEqual(HttpStatus.FORBIDDEN);
} }
}); });
it('should delete job group', async () => {
jobsGroupsServiceMock.findOne.mockResolvedValueOnce({
_id: new Types.ObjectId('6231aefe76598527c8d0b5ba'),
name: 'Dev',
});
jobServiceMock.getJobsGroupsAttachedJobs.mockResolvedValueOnce([]);
await jobsGroupsController.deleteJobsGroups({ id: '6231aefe76598527c8d0b5ba' });
expect(jobsGroupsServiceMock.deleteOne).toHaveBeenCalled();
});
}); });
}); });
...@@ -81,7 +81,6 @@ export class JobsGroupsController { ...@@ -81,7 +81,6 @@ export class JobsGroupsController {
@Roles('admin') @Roles('admin')
@ApiParam({ name: 'id', type: String, required: true }) @ApiParam({ name: 'id', type: String, required: true })
public async deleteJobsGroups(@Param() params): Promise<JobsGroups> { public async deleteJobsGroups(@Param() params): Promise<JobsGroups> {
// look for job group
const jobsGroups = await this.jobsGroupsService.findOne(params.id); const jobsGroups = await this.jobsGroupsService.findOne(params.id);
if (!jobsGroups) { if (!jobsGroups) {
throw new HttpException('Job group not found', HttpStatus.NOT_FOUND); throw new HttpException('Job group not found', HttpStatus.NOT_FOUND);
......
...@@ -154,6 +154,7 @@ export class JobsService { ...@@ -154,6 +154,7 @@ export class JobsService {
} }
public getJobsGroupsAttachedJobs(jobsGroupId: string): Promise<JobDocument[]> { public getJobsGroupsAttachedJobs(jobsGroupId: string): Promise<JobDocument[]> {
this.logger.debug(`getJobsGroupsAttachedJobs: ${jobsGroupId}`);
return this.jobModel.find({ jobsGroup: jobsGroupId.toString() }).exec(); return this.jobModel.find({ jobsGroup: jobsGroupId.toString() }).exec();
} }
......
...@@ -9,6 +9,7 @@ import { CreateJobsGroupsDto } from '../dto/create-jobsGroups.dto'; ...@@ -9,6 +9,7 @@ import { CreateJobsGroupsDto } from '../dto/create-jobsGroups.dto';
import { JobsService } from './jobs.service'; import { JobsService } from './jobs.service';
import { JobsGroupsService } from './jobsGroups.service'; import { JobsGroupsService } from './jobsGroups.service';
import { UsersService } from './users.service'; import { UsersService } from './users.service';
import { JobsGroupsDocument } from '../schemas/jobsGroups.schema';
describe('JobsGroupsService', () => { describe('JobsGroupsService', () => {
let jobsGroupsService: JobsGroupsService; let jobsGroupsService: JobsGroupsService;
...@@ -79,7 +80,7 @@ describe('JobsGroupsService', () => { ...@@ -79,7 +80,7 @@ describe('JobsGroupsService', () => {
}); });
describe('findAll', () => { describe('findAll', () => {
it('should findAll all jobs groups', async () => { it('should findAll', async () => {
const result = [ const result = [
{ {
_id: new Types.ObjectId('6231aefe76598527c8d0b5bc'), _id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
...@@ -90,6 +91,20 @@ describe('JobsGroupsService', () => { ...@@ -90,6 +91,20 @@ describe('JobsGroupsService', () => {
const reply = await jobsGroupsService.findAll(false); const reply = await jobsGroupsService.findAll(false);
expect(reply.length).toBe(1); expect(reply.length).toBe(1);
}); });
it('should findAll withJobs', async () => {
const populateSpy = jest.spyOn(jobsGroupsService, 'populateJobsGroupsWithJobs');
const result = [
{
_id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
name: 'Technique',
toJSON: jest.fn(),
},
];
mockJobsGroupsModel.exec.mockResolvedValueOnce(result);
const reply = await jobsGroupsService.findAll(true);
expect(reply.length).toBe(1);
expect(populateSpy).toHaveBeenCalled();
});
}); });
it('findByName', async () => { it('findByName', async () => {
...@@ -124,10 +139,16 @@ describe('JobsGroupsService', () => { ...@@ -124,10 +139,16 @@ describe('JobsGroupsService', () => {
}); });
describe('update', () => { describe('update', () => {
it('should throw error if not found', async () => {
mockJobsGroupsModel.exec.mockResolvedValueOnce(null);
try {
await jobsGroupsService.update('623aed68c5d45b6fbbaa7e60', { name: 'Technique' });
expect(true).toBe(false);
} catch (e) {
expect(e.status).toBe(HttpStatus.NOT_FOUND);
}
});
it('should update', async () => { it('should update', async () => {
const newJobsGroupsDto = {
name: 'Technique',
};
const newJobsGroups = { const newJobsGroups = {
_id: new Types.ObjectId('6231aefe76598527c8d0b5bc'), _id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
name: 'Technique', name: 'Technique',
...@@ -137,21 +158,40 @@ describe('JobsGroupsService', () => { ...@@ -137,21 +158,40 @@ describe('JobsGroupsService', () => {
name: 'Technique old', name: 'Technique old',
save: jest.fn().mockResolvedValueOnce(newJobsGroups), save: jest.fn().mockResolvedValueOnce(newJobsGroups),
}); });
const reply = await jobsGroupsService.update('623aed68c5d45b6fbbaa7e60', newJobsGroupsDto); const reply = await jobsGroupsService.update('623aed68c5d45b6fbbaa7e60', { name: 'Technique' });
expect(reply.name).toBe('Technique'); expect(reply.name).toBe('Technique');
}); });
}); });
it('should throw error if not found', async () => { describe('populateJobsGroupsWithJobs', () => {
const newJobsGroupsDto = { it('should populate jobs groups with jobs', async () => {
name: 'Technique', const populateSpy = jest.spyOn(jobsGroupsService, 'populateJobsGroupsWithJobs');
}; const result = [
mockJobsGroupsModel.exec.mockResolvedValueOnce(null); {
try { _id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
await jobsGroupsService.update('623aed68c5d45b6fbbaa7e60', newJobsGroupsDto); name: 'Technique',
expect(true).toBe(false); jobs: [
} catch (e) { {
expect(e.status).toBe(HttpStatus.NOT_FOUND); _id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
} name: 'Technique',
},
],
},
];
mockJobsGroupsModel.exec.mockResolvedValueOnce(result);
const jobsGroups = [
{
_id: new Types.ObjectId('65fd51d64bce0eed5ea1309f'),
name: 'Pilotage de projet',
},
{
_id: new Types.ObjectId('65fd51d64bce0eed5ea130a2'),
name: 'Travail social',
},
] as JobsGroupsDocument[];
populateSpy.mockImplementationOnce(() => jobsGroups as any);
await jobsGroupsService.populateJobsGroupsWithJobs(jobsGroups);
expect(populateSpy).toHaveBeenCalled();
});
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment