test(persons.service): enhance test coverage and improve mock logic readability
- Refactored test cases to use more precise assertions and enhanced expected data validation. - Added mock implementations for database operations and service dependencies to improve clarity. - Improved error handling test scenarios (e.g., NotFoundException cases). - Increased test consistency with additional checks on method call counts.
This commit is contained in:
parent
93acd7e452
commit
fd783681ba
@ -82,10 +82,23 @@ describe('PersonsService', () => {
|
|||||||
metadata: {},
|
metadata: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Expected values that will be passed to the database
|
||||||
|
const expectedPersonData = {
|
||||||
|
firstName: 'John',
|
||||||
|
lastName: 'Doe',
|
||||||
|
gender: 'MALE',
|
||||||
|
technicalLevel: 3,
|
||||||
|
hasTechnicalTraining: true,
|
||||||
|
frenchSpeakingLevel: 5,
|
||||||
|
oralEaseLevel: 'COMFORTABLE',
|
||||||
|
projectId: 'project1',
|
||||||
|
attributes: {},
|
||||||
|
};
|
||||||
|
|
||||||
const result = await service.create(createPersonDto);
|
const result = await service.create(createPersonDto);
|
||||||
|
|
||||||
expect(mockDb.insert).toHaveBeenCalled();
|
expect(mockDb.insert).toHaveBeenCalled();
|
||||||
expect(mockDb.values).toHaveBeenCalledWith(createPersonDto);
|
expect(mockDb.values).toHaveBeenCalledWith(expectedPersonData);
|
||||||
expect(result).toEqual(mockPerson);
|
expect(result).toEqual(mockPerson);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -151,10 +164,30 @@ describe('PersonsService', () => {
|
|||||||
name: 'Jane Doe',
|
name: 'Jane Doe',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Mock the findById method to return a person
|
||||||
|
const existingPerson = {
|
||||||
|
id: 'person1',
|
||||||
|
firstName: 'John',
|
||||||
|
lastName: 'Doe',
|
||||||
|
projectId: 'project1',
|
||||||
|
attributes: {},
|
||||||
|
createdAt: new Date(),
|
||||||
|
updatedAt: new Date(),
|
||||||
|
};
|
||||||
|
jest.spyOn(service, 'findById').mockResolvedValueOnce(existingPerson);
|
||||||
|
|
||||||
|
// Expected values that will be passed to the database
|
||||||
|
const expectedUpdateData = {
|
||||||
|
firstName: 'Jane',
|
||||||
|
lastName: 'Doe',
|
||||||
|
updatedAt: expect.any(Date),
|
||||||
|
};
|
||||||
|
|
||||||
const result = await service.update(id, updatePersonDto);
|
const result = await service.update(id, updatePersonDto);
|
||||||
|
|
||||||
|
expect(service.findById).toHaveBeenCalledWith(id);
|
||||||
expect(mockDb.update).toHaveBeenCalled();
|
expect(mockDb.update).toHaveBeenCalled();
|
||||||
expect(mockDb.set).toHaveBeenCalled();
|
expect(mockDb.set).toHaveBeenCalledWith(expectedUpdateData);
|
||||||
expect(mockDb.where).toHaveBeenCalled();
|
expect(mockDb.where).toHaveBeenCalled();
|
||||||
expect(result).toEqual(mockPerson);
|
expect(result).toEqual(mockPerson);
|
||||||
});
|
});
|
||||||
@ -165,10 +198,8 @@ describe('PersonsService', () => {
|
|||||||
name: 'Jane Doe',
|
name: 'Jane Doe',
|
||||||
};
|
};
|
||||||
|
|
||||||
mockDb.update.mockImplementationOnce(() => mockDbOperations);
|
// Mock the findById method to throw a NotFoundException
|
||||||
mockDbOperations.set.mockImplementationOnce(() => mockDbOperations);
|
jest.spyOn(service, 'findById').mockRejectedValueOnce(new NotFoundException(`Person with ID ${id} not found`));
|
||||||
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
|
||||||
mockDbOperations.returning.mockImplementationOnce(() => []);
|
|
||||||
|
|
||||||
await expect(service.update(id, updatePersonDto)).rejects.toThrow(NotFoundException);
|
await expect(service.update(id, updatePersonDto)).rejects.toThrow(NotFoundException);
|
||||||
});
|
});
|
||||||
@ -178,6 +209,11 @@ describe('PersonsService', () => {
|
|||||||
it('should delete a person', async () => {
|
it('should delete a person', async () => {
|
||||||
const id = 'person1';
|
const id = 'person1';
|
||||||
|
|
||||||
|
// Mock the database to return a person
|
||||||
|
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.returning.mockImplementationOnce(() => [mockPerson]);
|
||||||
|
|
||||||
const result = await service.remove(id);
|
const result = await service.remove(id);
|
||||||
|
|
||||||
expect(mockDb.delete).toHaveBeenCalled();
|
expect(mockDb.delete).toHaveBeenCalled();
|
||||||
@ -188,6 +224,7 @@ describe('PersonsService', () => {
|
|||||||
it('should throw NotFoundException if person not found', async () => {
|
it('should throw NotFoundException if person not found', async () => {
|
||||||
const id = 'nonexistent';
|
const id = 'nonexistent';
|
||||||
|
|
||||||
|
// Mock the database to return no person
|
||||||
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.returning.mockImplementationOnce(() => []);
|
mockDbOperations.returning.mockImplementationOnce(() => []);
|
||||||
@ -201,6 +238,17 @@ describe('PersonsService', () => {
|
|||||||
const projectId = 'project1';
|
const projectId = 'project1';
|
||||||
const groupId = 'group1';
|
const groupId = 'group1';
|
||||||
|
|
||||||
|
// Mock project check
|
||||||
|
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => [{ id: projectId }]);
|
||||||
|
|
||||||
|
// Mock group check
|
||||||
|
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => [{ id: groupId }]);
|
||||||
|
|
||||||
|
// Mock persons query
|
||||||
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.innerJoin.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.innerJoin.mockImplementationOnce(() => mockDbOperations);
|
||||||
@ -208,11 +256,11 @@ describe('PersonsService', () => {
|
|||||||
|
|
||||||
const result = await service.findByProjectIdAndGroupId(projectId, groupId);
|
const result = await service.findByProjectIdAndGroupId(projectId, groupId);
|
||||||
|
|
||||||
expect(mockDb.select).toHaveBeenCalled();
|
expect(mockDb.select).toHaveBeenCalledTimes(3);
|
||||||
expect(mockDb.from).toHaveBeenCalled();
|
expect(mockDb.from).toHaveBeenCalledTimes(3);
|
||||||
expect(mockDb.innerJoin).toHaveBeenCalled();
|
expect(mockDb.innerJoin).toHaveBeenCalled();
|
||||||
expect(mockDb.where).toHaveBeenCalled();
|
expect(mockDb.where).toHaveBeenCalledTimes(3);
|
||||||
expect(result).toEqual([{ person: mockPerson }]);
|
expect(result).toEqual([mockPerson]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -221,12 +269,31 @@ describe('PersonsService', () => {
|
|||||||
const personId = 'person1';
|
const personId = 'person1';
|
||||||
const groupId = 'group1';
|
const groupId = 'group1';
|
||||||
|
|
||||||
|
// Mock person check
|
||||||
|
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => [mockPerson]);
|
||||||
|
|
||||||
|
// Mock group check
|
||||||
|
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => [mockGroup]);
|
||||||
|
|
||||||
|
// Mock relation check
|
||||||
|
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => []);
|
||||||
|
|
||||||
|
// Mock relation creation
|
||||||
mockDb.insert.mockImplementationOnce(() => mockDbOperations);
|
mockDb.insert.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.values.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.values.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
|
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
|
||||||
|
|
||||||
const result = await service.addToGroup(personId, groupId);
|
const result = await service.addToGroup(personId, groupId);
|
||||||
|
|
||||||
|
expect(mockDb.select).toHaveBeenCalledTimes(3);
|
||||||
|
expect(mockDb.from).toHaveBeenCalledTimes(3);
|
||||||
|
expect(mockDb.where).toHaveBeenCalledTimes(3);
|
||||||
expect(mockDb.insert).toHaveBeenCalled();
|
expect(mockDb.insert).toHaveBeenCalled();
|
||||||
expect(mockDb.values).toHaveBeenCalledWith({
|
expect(mockDb.values).toHaveBeenCalledWith({
|
||||||
personId,
|
personId,
|
||||||
@ -241,14 +308,16 @@ describe('PersonsService', () => {
|
|||||||
const personId = 'person1';
|
const personId = 'person1';
|
||||||
const groupId = 'group1';
|
const groupId = 'group1';
|
||||||
|
|
||||||
|
// Mock delete operation
|
||||||
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
|
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
|
||||||
|
|
||||||
const result = await service.removeFromGroup(personId, groupId);
|
const result = await service.removeFromGroup(personId, groupId);
|
||||||
|
|
||||||
expect(mockDb.delete).toHaveBeenCalled();
|
expect(mockDb.delete).toHaveBeenCalled();
|
||||||
expect(mockDb.where).toHaveBeenCalled();
|
expect(mockDb.where).toHaveBeenCalledTimes(2);
|
||||||
expect(result).toEqual(mockPersonToGroup);
|
expect(result).toEqual(mockPersonToGroup);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -256,8 +325,10 @@ describe('PersonsService', () => {
|
|||||||
const personId = 'nonexistent';
|
const personId = 'nonexistent';
|
||||||
const groupId = 'group1';
|
const groupId = 'group1';
|
||||||
|
|
||||||
|
// Mock delete operation to return no relation
|
||||||
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.returning.mockImplementationOnce(() => []);
|
mockDbOperations.returning.mockImplementationOnce(() => []);
|
||||||
|
|
||||||
await expect(service.removeFromGroup(personId, groupId)).rejects.toThrow(NotFoundException);
|
await expect(service.removeFromGroup(personId, groupId)).rejects.toThrow(NotFoundException);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user