From d716210d98cb75a665d1bc27edc399573ff992dc Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Tue, 21 May 2024 12:09:54 +0000
Subject: [PATCH] test(jobsGroup): increase coverage

---
 .../controllers/jobsGroups.controller.spec.ts | 75 +++++++++++--------
 .../controllers/jobsGroups.controller.ts      |  1 -
 src/users/services/jobs.service.ts            |  1 +
 src/users/services/jobsGroups.service.spec.ts | 72 ++++++++++++++----
 4 files changed, 99 insertions(+), 50 deletions(-)

diff --git a/src/users/controllers/jobsGroups.controller.spec.ts b/src/users/controllers/jobsGroups.controller.spec.ts
index 05d3748c2..b0529568e 100644
--- a/src/users/controllers/jobsGroups.controller.spec.ts
+++ b/src/users/controllers/jobsGroups.controller.spec.ts
@@ -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();
+    });
   });
 });
diff --git a/src/users/controllers/jobsGroups.controller.ts b/src/users/controllers/jobsGroups.controller.ts
index e78ff9d7f..83d109472 100644
--- a/src/users/controllers/jobsGroups.controller.ts
+++ b/src/users/controllers/jobsGroups.controller.ts
@@ -83,7 +83,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);
diff --git a/src/users/services/jobs.service.ts b/src/users/services/jobs.service.ts
index 873f5b84d..ba717a706 100644
--- a/src/users/services/jobs.service.ts
+++ b/src/users/services/jobs.service.ts
@@ -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();
   }
 
diff --git a/src/users/services/jobsGroups.service.spec.ts b/src/users/services/jobsGroups.service.spec.ts
index 0c00b2e54..5c414b774 100644
--- a/src/users/services/jobsGroups.service.spec.ts
+++ b/src/users/services/jobsGroups.service.spec.ts
@@ -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();
+    });
   });
 });
-- 
GitLab