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