test: update and refactor person and group service tests
This commit is contained in:
parent
fd783681ba
commit
e5121c4e7a
@ -347,6 +347,9 @@ describe('GroupsService', () => {
|
|||||||
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
|
mockDbOperations.returning.mockImplementationOnce(() => [mockPersonToGroup]);
|
||||||
|
|
||||||
|
// Mock getPersonsInGroup
|
||||||
|
jest.spyOn(service, 'getPersonsInGroup').mockResolvedValueOnce([mockPerson]);
|
||||||
|
|
||||||
const result = await service.removePersonFromGroup(groupId, personId);
|
const result = await service.removePersonFromGroup(groupId, personId);
|
||||||
|
|
||||||
expect(service.findById).toHaveBeenCalledWith(groupId);
|
expect(service.findById).toHaveBeenCalledWith(groupId);
|
||||||
@ -354,7 +357,7 @@ describe('GroupsService', () => {
|
|||||||
expect(mockDb.from).toHaveBeenCalled();
|
expect(mockDb.from).toHaveBeenCalled();
|
||||||
expect(mockDb.delete).toHaveBeenCalled();
|
expect(mockDb.delete).toHaveBeenCalled();
|
||||||
expect(mockDb.where).toHaveBeenCalled();
|
expect(mockDb.where).toHaveBeenCalled();
|
||||||
expect(result).toEqual(mockPersonToGroup);
|
expect(result).toEqual({ ...mockGroup, persons: [mockPerson] });
|
||||||
|
|
||||||
// Check if WebSocketsService.emitPersonRemovedFromGroup was called with correct parameters
|
// Check if WebSocketsService.emitPersonRemovedFromGroup was called with correct parameters
|
||||||
expect(mockWebSocketsService.emitPersonRemovedFromGroup).toHaveBeenCalledWith(
|
expect(mockWebSocketsService.emitPersonRemovedFromGroup).toHaveBeenCalledWith(
|
||||||
@ -395,7 +398,7 @@ describe('GroupsService', () => {
|
|||||||
describe('getPersonsInGroup', () => {
|
describe('getPersonsInGroup', () => {
|
||||||
it('should get all persons in a group', async () => {
|
it('should get all persons in a group', async () => {
|
||||||
const groupId = 'group1';
|
const groupId = 'group1';
|
||||||
const mockPersons = [{ person: mockPerson }];
|
const personIds = [{ id: 'person1' }];
|
||||||
|
|
||||||
// Mock findById to return the group
|
// Mock findById to return the group
|
||||||
jest.spyOn(service, 'findById').mockResolvedValueOnce(mockGroup);
|
jest.spyOn(service, 'findById').mockResolvedValueOnce(mockGroup);
|
||||||
@ -403,22 +406,25 @@ describe('GroupsService', () => {
|
|||||||
// Reset and setup mocks for this test
|
// Reset and setup mocks for this test
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
|
|
||||||
// Mock the select chain to return the expected result
|
// Mock the select chain to return person IDs
|
||||||
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.innerJoin.mockImplementationOnce(() => mockDbOperations);
|
mockDbOperations.where.mockImplementationOnce(() => personIds);
|
||||||
mockDbOperations.where.mockImplementationOnce(() => mockPersons);
|
|
||||||
|
// Mock the person lookup
|
||||||
|
mockDb.select.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.from.mockImplementationOnce(() => mockDbOperations);
|
||||||
|
mockDbOperations.where.mockImplementationOnce(() => [mockPerson]);
|
||||||
|
|
||||||
const result = await service.getPersonsInGroup(groupId);
|
const result = await service.getPersonsInGroup(groupId);
|
||||||
|
|
||||||
expect(service.findById).toHaveBeenCalledWith(groupId);
|
expect(service.findById).toHaveBeenCalledWith(groupId);
|
||||||
expect(mockDb.select).toHaveBeenCalled();
|
expect(mockDb.select).toHaveBeenCalled();
|
||||||
expect(mockDb.from).toHaveBeenCalled();
|
expect(mockDb.from).toHaveBeenCalled();
|
||||||
expect(mockDb.innerJoin).toHaveBeenCalled();
|
|
||||||
expect(mockDb.where).toHaveBeenCalled();
|
expect(mockDb.where).toHaveBeenCalled();
|
||||||
|
|
||||||
// Just verify the result is defined, since the mock implementation is complex
|
// Verify the result is the expected array of persons
|
||||||
expect(result).toBeDefined();
|
expect(result).toEqual([mockPerson]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -18,6 +18,17 @@ describe('PersonsService', () => {
|
|||||||
updatedAt: new Date(),
|
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 = {
|
const mockGroup = {
|
||||||
id: 'group1',
|
id: 'group1',
|
||||||
name: 'Test Group',
|
name: 'Test Group',
|
||||||
@ -189,7 +200,7 @@ describe('PersonsService', () => {
|
|||||||
expect(mockDb.update).toHaveBeenCalled();
|
expect(mockDb.update).toHaveBeenCalled();
|
||||||
expect(mockDb.set).toHaveBeenCalledWith(expectedUpdateData);
|
expect(mockDb.set).toHaveBeenCalledWith(expectedUpdateData);
|
||||||
expect(mockDb.where).toHaveBeenCalled();
|
expect(mockDb.where).toHaveBeenCalled();
|
||||||
expect(result).toEqual(mockPerson);
|
expect(result).toEqual(updatedMockPerson);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw NotFoundException if person not found', async () => {
|
it('should throw NotFoundException if person not found', async () => {
|
||||||
@ -310,14 +321,14 @@ describe('PersonsService', () => {
|
|||||||
|
|
||||||
// Mock delete operation
|
// Mock delete operation
|
||||||
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
mockDb.delete.mockImplementationOnce(() => mockDbOperations);
|
||||||
mockDbOperations.where.mockImplementationOnce(() => mockDbOperations);
|
// The where call with the and() condition
|
||||||
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).toHaveBeenCalledTimes(2);
|
expect(mockDb.where).toHaveBeenCalledTimes(1);
|
||||||
expect(result).toEqual(mockPersonToGroup);
|
expect(result).toEqual(mockPersonToGroup);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user