From 3c82ffaf7b67db42b51ebaba078a6bec09548367 Mon Sep 17 00:00:00 2001 From: white Date: Fri, 24 May 2024 01:38:10 +0200 Subject: [PATCH] fix: improve coverage --- .../availability/availability.service.spec.ts | 29 +++++++ api/src/mocks/repositories.ts | 3 + .../recruitment-session.service.spec.ts | 83 ++++++++++++++++++- api/src/timeslots/timeslots.service.spec.ts | 75 +++++++++++++++++ 4 files changed, 189 insertions(+), 1 deletion(-) diff --git a/api/src/availability/availability.service.spec.ts b/api/src/availability/availability.service.spec.ts index 9f62ba2..43121e6 100644 --- a/api/src/availability/availability.service.spec.ts +++ b/api/src/availability/availability.service.spec.ts @@ -78,6 +78,28 @@ describe('AvailabilityService', () => { }); }); + describe('updateAvailability', () => { + it('should update the availability with the specified id', async () => { + const mockUpdatedAvailability = { + ...mockAvailability, + state: AvailabilityState.Interviewing, + }; + jest + .spyOn(mockedRepository, 'save') + .mockResolvedValue(mockUpdatedAvailability); + const result = await service.updateAvailability( + mockAvailability.id, + mockUpdatedAvailability, + ); + expect(result).toEqual(mockUpdatedAvailability); + expect(mockedRepository.save).toHaveBeenCalledTimes(1); + expect(mockedRepository.save).toHaveBeenCalledWith({ + ...mockUpdatedAvailability, + id: mockAvailability.id, + }); + }); + }); + describe('deleteAvailability', () => { it('should remove the specified availability from the database', async () => { const mockAvailabilityRepository = { @@ -142,6 +164,13 @@ describe('AvailabilityService', () => { expect(result).toEqual(mockAvailability); expect(mockedRepository.findBy).toHaveBeenCalledTimes(1); }); + + it('should return null if no availability is found', async () => { + jest.spyOn(mockedRepository, 'findBy').mockResolvedValue([]); + const result = await service.findById(mockAvailability.id); + expect(result).toBeNull(); + expect(mockedRepository.findBy).toHaveBeenCalledTimes(1); + }); }); describe('findByUserAndTimeSlot', () => { diff --git a/api/src/mocks/repositories.ts b/api/src/mocks/repositories.ts index e418727..7ac0b52 100644 --- a/api/src/mocks/repositories.ts +++ b/api/src/mocks/repositories.ts @@ -1,6 +1,9 @@ +import { count } from 'console'; + export const mockedRepository = { find: jest.fn(), findBy: jest.fn(), remove: jest.fn(), save: jest.fn(), + count: jest.fn(), }; diff --git a/api/src/recruitment-session/recruitment-session.service.spec.ts b/api/src/recruitment-session/recruitment-session.service.spec.ts index 6df16dc..888ba10 100644 --- a/api/src/recruitment-session/recruitment-session.service.spec.ts +++ b/api/src/recruitment-session/recruitment-session.service.spec.ts @@ -5,7 +5,7 @@ import { getRepositoryToken, getDataSourceToken } from '@nestjs/typeorm'; import { RecruitmentSession } from './recruitment-session.entity'; import { RecruitmentSessionService } from './recruitment-session.service'; import { mockedTimeSlotsService as mockedTimeSlotsServiceClass } from '@mocks/services'; -import { mockDataSource } from 'src/mocks/data-sources'; +import { MockedDataSource, mockDataSource } from 'src/mocks/data-sources'; import { TimeSlotsService } from 'src/timeslots/timeslots.service'; import { RecruitmentSessionState } from '@hkrecruitment/shared'; @@ -118,4 +118,85 @@ describe('Recruitment Session Service', () => { ); }); }); + + describe('findAllRecruitmentSessions', () => { + it('should return all recruitment sessions', async () => { + const recruitmentSessions = [mockRecruitmentSession]; + jest + .spyOn(mockedRepository, 'find') + .mockResolvedValue(recruitmentSessions); + const result = + await recruitmentSessionService.findAllRecruitmentSessions(); + expect(result).toEqual(recruitmentSessions); + expect(mockedRepository.find).toHaveBeenCalledTimes(1); + }); + + it('should return an empty array if there are no recruitment sessions', async () => { + jest.spyOn(mockedRepository, 'find').mockResolvedValue([]); + const result = + await recruitmentSessionService.findAllRecruitmentSessions(); + expect(result).toEqual([]); + expect(mockedRepository.find).toHaveBeenCalledTimes(1); + }); + }); + + describe('findRecruitmentSessionById', () => { + it('should return the recruitment session with the given ID', async () => { + jest + .spyOn(mockedRepository, 'findBy') + .mockResolvedValue([mockRecruitmentSession]); + const result = await recruitmentSessionService.findRecruitmentSessionById( + 1, + ); + expect(result).toEqual(mockRecruitmentSession); + expect(mockedRepository.findBy).toHaveBeenCalledTimes(1); + expect(mockedRepository.findBy).toHaveBeenCalledWith({ id: 1 }); + }); + + it('should return null if the recruitment session does not exist', async () => { + jest.spyOn(mockedRepository, 'findBy').mockResolvedValue([]); + const result = await recruitmentSessionService.findRecruitmentSessionById( + 1, + ); + expect(result).toBeNull(); + expect(mockedRepository.findBy).toHaveBeenCalledTimes(1); + expect(mockedRepository.findBy).toHaveBeenCalledWith({ id: 1 }); + }); + }); + + describe('findActiveRecruitmentSession', () => { + it('should return the active recruitment session', async () => { + jest + .spyOn(mockedRepository, 'findBy') + .mockResolvedValue([mockRecruitmentSession]); + const result = + await recruitmentSessionService.findActiveRecruitmentSession(); + expect(result).toEqual(mockRecruitmentSession); + expect(mockedRepository.findBy).toHaveBeenCalledTimes(1); + expect(mockedRepository.findBy).toHaveBeenCalledWith({ + state: RecruitmentSessionState.Active, + }); + }); + + it('should return null if there is no active recruitment session', async () => { + jest.spyOn(mockedRepository, 'findBy').mockResolvedValue([]); + const result = + await recruitmentSessionService.findActiveRecruitmentSession(); + expect(result).toBeNull(); + expect(mockedRepository.findBy).toHaveBeenCalledTimes(1); + expect(mockedRepository.findBy).toHaveBeenCalledWith({ + state: RecruitmentSessionState.Active, + }); + }); + }); + + describe('sessionHasPendingInterviews', () => { + it('should return error', async () => { + await recruitmentSessionService + .sessionHasPendingInterviews(mockRecruitmentSession) + .catch((error) => { + expect(error.message).toBe('Method not implemented.'); + }); + }); + }); }); diff --git a/api/src/timeslots/timeslots.service.spec.ts b/api/src/timeslots/timeslots.service.spec.ts index 32d6d8d..24af579 100644 --- a/api/src/timeslots/timeslots.service.spec.ts +++ b/api/src/timeslots/timeslots.service.spec.ts @@ -45,6 +45,20 @@ describe('TimeSlotsService', () => { expect(timeSlotService).toBeDefined(); }); + describe('countOverlappingTimeSlots', () => { + it('should return the number of overlapping time slots', async () => { + jest.spyOn(mockedRepository, 'count').mockResolvedValue(2); + const startDate = new Date('2022-01-01T09:00:00'); + const endDate = new Date('2022-01-01T10:00:00'); + const result = await timeSlotService.countOverlappingTimeSlots( + startDate, + endDate, + ); + expect(result).toBe(2); + expect(mockedRepository.count).toHaveBeenCalledTimes(1); + }); + }); + describe('deleteTimeSlot', () => { it('should remove the specified timeslot from the database', async () => { jest.spyOn(mockedRepository, 'remove').mockResolvedValue(mockTimeSlot); @@ -72,6 +86,13 @@ describe('TimeSlotsService', () => { expect(result).toEqual(mockTimeSlot); expect(mockedRepository.findBy).toHaveBeenCalledTimes(1); }); + + it("should return null if the timeslot doesn't exist", async () => { + jest.spyOn(mockedRepository, 'findBy').mockResolvedValue([]); + const result = await timeSlotService.findById(mockTimeSlot.id); + expect(result).toBeNull(); + expect(mockedRepository.findBy).toHaveBeenCalledTimes(1); + }); }); describe('createTimeSlot', () => { @@ -269,6 +290,14 @@ describe('TimeSlotsService', () => { id: 1, recruitmentSession: 3, availabilities: [ + { + state: AvailabilityState.Interviewing, + user: { + role: Role.None, + is_board: false, + is_expert: true, + }, + }, { state: AvailabilityState.Free, user: { @@ -350,6 +379,52 @@ describe('TimeSlotsService', () => { ]), ); }); + + it("should return an empty array if there aren't any time slots with at least 2 available people", async () => { + const mockQueryBuilder = { + innerJoinAndSelect: jest.fn().mockReturnThis(), + where: jest.fn().mockReturnThis(), + andWhere: jest.fn().mockReturnThis(), + getMany: jest.fn().mockResolvedValue([ + { + start: new Date('2022-01-01T09:00:00'), + end: new Date('2022-01-01T10:00:00'), + id: 1, + recruitmentSession: 3, + availabilities: [ + { + state: AvailabilityState.Interviewing, + user: { + role: Role.None, + is_board: false, + is_expert: true, + }, + }, + { + state: AvailabilityState.Free, + user: { + role: Role.Member, + is_board: false, + is_expert: true, + }, + }, + ], + }, + ]), + }; + + // Mock the timeSlotRepository and its methods + const mockTimeSlotRepository = { + createQueryBuilder: jest.fn().mockReturnValue(mockQueryBuilder), + }; + + const timeSlotService = new TimeSlotsService( + mockTimeSlotRepository as any, + ); + + const result = await timeSlotService.findAvailableTimeSlots(); + expect(result).toEqual([]); + }); }); });