test: update and refactor person and group service tests

This commit is contained in:
Mathis HERRIOT 2025-05-17 00:23:34 +02:00
parent fd783681ba
commit e5121c4e7a
No known key found for this signature in database
GPG Key ID: E7EB4A211D8D4907
2 changed files with 28 additions and 11 deletions

View File

@ -347,6 +347,9 @@ describe('GroupsService', () => {
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
// Mock getPersonsInGroup
jest.spyOn(service, 'getPersonsInGroup').mockResolvedValueOnce([mockPerson]);
const result = await service.removePersonFromGroup(groupId, personId);
expect(service.findById).toHaveBeenCalledWith(groupId);
@ -354,7 +357,7 @@ describe('GroupsService', () => {
expect(mockDb.from).toHaveBeenCalled();
expect(mockDb.delete).toHaveBeenCalled();
expect(mockDb.where).toHaveBeenCalled();
expect(result).toEqual(mockPersonToGroup);
expect(result).toEqual({ ...mockGroup, persons: [mockPerson] });
// Check if WebSocketsService.emitPersonRemovedFromGroup was called with correct parameters
expect(mockWebSocketsService.emitPersonRemovedFromGroup).toHaveBeenCalledWith(
@ -395,7 +398,7 @@ describe('GroupsService', () => {
describe('getPersonsInGroup', () => {
it('should get all persons in a group', async () => {
const groupId = 'group1';
const mockPersons = [{ person: mockPerson }];
const personIds = [{ id: 'person1' }];
// Mock findById to return the group
jest.spyOn(service, 'findById').mockResolvedValueOnce(mockGroup);
@ -403,22 +406,25 @@ describe('GroupsService', () => {
// Reset and setup mocks for this test
jest.clearAllMocks();
// Mock the select chain to return the expected result
// Mock the select chain to return person IDs
mockDb.select.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.innerJoin.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => mockPersons);
mockDbOperations.where.mockImplementationOnce(() => personIds);
// Mock the person lookup
mockDb.select.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => [mockPerson]);
const result = await service.getPersonsInGroup(groupId);
expect(service.findById).toHaveBeenCalledWith(groupId);
expect(mockDb.select).toHaveBeenCalled();
expect(mockDb.from).toHaveBeenCalled();
expect(mockDb.innerJoin).toHaveBeenCalled();
expect(mockDb.where).toHaveBeenCalled();
// Just verify the result is defined, since the mock implementation is complex
expect(result).toBeDefined();
// Verify the result is the expected array of persons
expect(result).toEqual([mockPerson]);
});
});
});

View File

@ -18,6 +18,17 @@ describe('PersonsService', () => {
updatedAt: new Date(),
};
// Updated mock person for update test
const updatedMockPerson = {
id: 'person1',
name: 'Jane Doe',
projectId: 'project1',
skills: [],
metadata: {},
createdAt: new Date(),
updatedAt: new Date(),
};
const mockGroup = {
id: 'group1',
name: 'Test Group',
@ -189,7 +200,7 @@ describe('PersonsService', () => {
expect(mockDb.update).toHaveBeenCalled();
expect(mockDb.set).toHaveBeenCalledWith(expectedUpdateData);
expect(mockDb.where).toHaveBeenCalled();
expect(result).toEqual(mockPerson);
expect(result).toEqual(updatedMockPerson);
});
it('should throw NotFoundException if person not found', async () => {
@ -310,14 +321,14 @@ describe('PersonsService', () => {
// Mock delete operation
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
// The where call with the and() condition
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
const result = await service.removeFromGroup(personId, groupId);
expect(mockDb.delete).toHaveBeenCalled();
expect(mockDb.where).toHaveBeenCalledTimes(2);
expect(mockDb.where).toHaveBeenCalledTimes(1);
expect(result).toEqual(mockPersonToGroup);
});