test(groups): improve mocks and assertions in group service tests

This commit is contained in:
Mathis HERRIOT 2025-05-17 00:14:26 +02:00
parent 2a47417b47
commit 93acd7e452
No known key found for this signature in database
GPG Key ID: E7EB4A211D8D4907

View File

@ -183,8 +183,12 @@ describe('GroupsService', () => {
name: 'Updated Group',
};
// Mock findById to return the group
jest.spyOn(service, 'findById').mockResolvedValueOnce(mockGroup);
const result = await service.update(id, updateGroupDto);
expect(service.findById).toHaveBeenCalledWith(id);
expect(mockDb.update).toHaveBeenCalled();
expect(mockDb.set).toHaveBeenCalled();
expect(mockDb.where).toHaveBeenCalled();
@ -206,10 +210,8 @@ describe('GroupsService', () => {
name: 'Updated Group',
};
mockDb.update.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.set.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.returning.mockImplementationOnce(() => [undefined]);
// Mock findById to throw NotFoundException
jest.spyOn(service, 'findById').mockRejectedValueOnce(new NotFoundException(`Group with ID ${id} not found`));
await expect(service.update(id, updateGroupDto)).rejects.toThrow(NotFoundException);
});
@ -260,19 +262,22 @@ describe('GroupsService', () => {
// Mock person lookup
mockDb.select.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => [[mockPerson]]);
mockDbOperations.where.mockImplementationOnce(() => [mockPerson]);
// Mock relation lookup
mockDb.select.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => [undefined]);
mockDbOperations.where.mockImplementationOnce(() => []);
// Mock relation creation
mockDb.insert.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.values.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
// Mock getPersonsInGroup
jest.spyOn(service, 'getPersonsInGroup').mockResolvedValueOnce([mockPerson]);
const result = await service.addPersonToGroup(groupId, personId);
expect(service.findById).toHaveBeenCalledWith(groupId);
@ -283,14 +288,14 @@ describe('GroupsService', () => {
personId,
groupId,
});
expect(result).toEqual(mockPersonToGroup);
expect(result).toEqual({ ...mockGroup, persons: [mockPerson] });
// Check if WebSocketsService.emitPersonAddedToGroup was called with correct parameters
expect(mockWebSocketsService.emitPersonAddedToGroup).toHaveBeenCalledWith(
mockGroup.projectId,
{
group: mockGroup,
person: [mockPerson],
person: mockPerson,
relation: mockPersonToGroup,
}
);
@ -309,7 +314,12 @@ describe('GroupsService', () => {
// Mock person lookup to return no person
mockDb.select.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => [undefined]);
mockDbOperations.where.mockImplementationOnce(() => []);
// Mock user lookup to return no user
mockDb.select.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => []);
await expect(service.addPersonToGroup(groupId, personId)).rejects.toThrow(NotFoundException);
});