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', () => {
describe('findAll', () => {
it('should findAll', async () => {
jobsGroupsServiceMock.findAll.mockResolvedValue([
{
_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);
await jobsGroupsController.findAll();
expect(jobsGroupsServiceMock.findAll).toHaveBeenCalled();
});
});
describe('createJobsGroups', () => {
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);
const createReply = await jobsGroupsController.createJobsGroups(newJobsGroups);
expect(createReply).toEqual(newCreatedJobsGroups);
describe('findAllWithJobs', () => {
it('should findAll with jobs', async () => {
await jobsGroupsController.findAllWithJobs();
expect(jobsGroupsServiceMock.findAll).toHaveBeenCalledWith(true);
});
});
describe('createJobsGroups', () => {
it('should throw error on already existing job group `Dev`', async () => {
const newJobsGroups: CreateJobsGroupsDto = {
name: 'Dev',
......@@ -97,9 +82,22 @@ describe('JobsGroupsController', () => {
expect(true).toBe(false);
} catch (e) {
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', () => {
......@@ -118,14 +116,15 @@ describe('JobsGroupsController', () => {
deletedCount: 1,
});
it('should delete job group', async () => {
jobsGroupsServiceMock.findOne.mockResolvedValueOnce({
_id: new Types.ObjectId('6231aefe76598527c8d0b5ba'),
name: 'Dev',
});
jobServiceMock.getJobsGroupsAttachedJobs.mockResolvedValueOnce([]);
const reply = await jobsGroupsController.deleteJobsGroups({ id: '6231aefe76598527c8d0b5ba' });
expect(reply).toBeTruthy();
it('should not be able to find job group', async () => {
jobsGroupsServiceMock.findOne.mockResolvedValueOnce(null);
try {
await jobsGroupsController.deleteJobsGroups({ id: '6231aefe76598527c8d0b5ba' });
expect(true).toBe(false);
} catch (e) {
expect(e.message).toEqual('Job group not found');
expect(e.status).toEqual(HttpStatus.NOT_FOUND);
}
});
it('should not delete job group if a job is linked', async () => {
......@@ -142,5 +141,15 @@ describe('JobsGroupsController', () => {
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 {
@Roles('admin')
@ApiParam({ name: 'id', type: String, required: true })
public async deleteJobsGroups(@Param() params): Promise<JobsGroups> {
// look for job group
const jobsGroups = await this.jobsGroupsService.findOne(params.id);
if (!jobsGroups) {
throw new HttpException('Job group not found', HttpStatus.NOT_FOUND);
......
......@@ -154,6 +154,7 @@ export class JobsService {
}
public getJobsGroupsAttachedJobs(jobsGroupId: string): Promise<JobDocument[]> {
this.logger.debug(`getJobsGroupsAttachedJobs: ${jobsGroupId}`);
return this.jobModel.find({ jobsGroup: jobsGroupId.toString() }).exec();
}
......
......@@ -9,6 +9,7 @@ import { CreateJobsGroupsDto } from '../dto/create-jobsGroups.dto';
import { JobsService } from './jobs.service';
import { JobsGroupsService } from './jobsGroups.service';
import { UsersService } from './users.service';
import { JobsGroupsDocument } from '../schemas/jobsGroups.schema';
describe('JobsGroupsService', () => {
let jobsGroupsService: JobsGroupsService;
......@@ -79,7 +80,7 @@ describe('JobsGroupsService', () => {
});
describe('findAll', () => {
it('should findAll all jobs groups', async () => {
it('should findAll', async () => {
const result = [
{
_id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
......@@ -90,6 +91,20 @@ describe('JobsGroupsService', () => {
const reply = await jobsGroupsService.findAll(false);
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 () => {
......@@ -124,10 +139,16 @@ describe('JobsGroupsService', () => {
});
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 () => {
const newJobsGroupsDto = {
name: 'Technique',
};
const newJobsGroups = {
_id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
name: 'Technique',
......@@ -137,21 +158,40 @@ describe('JobsGroupsService', () => {
name: 'Technique old',
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');
});
});
it('should throw error if not found', async () => {
const newJobsGroupsDto = {
name: 'Technique',
};
mockJobsGroupsModel.exec.mockResolvedValueOnce(null);
try {
await jobsGroupsService.update('623aed68c5d45b6fbbaa7e60', newJobsGroupsDto);
expect(true).toBe(false);
} catch (e) {
expect(e.status).toBe(HttpStatus.NOT_FOUND);
}
describe('populateJobsGroupsWithJobs', () => {
it('should populate jobs groups with jobs', async () => {
const populateSpy = jest.spyOn(jobsGroupsService, 'populateJobsGroupsWithJobs');
const result = [
{
_id: new Types.ObjectId('6231aefe76598527c8d0b5bc'),
name: 'Technique',
jobs: [
{
_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.
Finish editing this message first!
Please register or to comment