test(groups): improve mocks and assertions in group service tests
This commit is contained in:
parent
2a47417b47
commit
93acd7e452
@ -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);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user