Skip to content

Commit

Permalink
fix: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
whiitex committed May 23, 2024
1 parent d8a4b8e commit 3c82ffa
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 1 deletion.
29 changes: 29 additions & 0 deletions api/src/availability/availability.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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', () => {
Expand Down
3 changes: 3 additions & 0 deletions api/src/mocks/repositories.ts
Original file line number Diff line number Diff line change
@@ -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(),
};
83 changes: 82 additions & 1 deletion api/src/recruitment-session/recruitment-session.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.');
});
});
});
});
75 changes: 75 additions & 0 deletions api/src/timeslots/timeslots.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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([]);
});
});
});

Expand Down

0 comments on commit 3c82ffa

Please sign in to comment.