Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Metauriel committed Dec 13, 2024
1 parent 07514ec commit 89bd3b9
Showing 1 changed file with 80 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,24 @@ describe('RoomMembershipService', () => {
schoolId,
});

groupService.findById.mockResolvedValue(group);
roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership);

return { group, room, roomMembership };
};

const mockGroupsAtSchoolAfterRemoval = (groups: Group[]) => {
groupService.findGroups.mockResolvedValue({ total: groups.length, data: groups });
};

const setupRoomRoles = () => {
const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER });
const roomEditorRole = roleFactory.buildWithId({ name: RoleName.ROOMEDITOR });
roleService.findByName.mockResolvedValue(roomOwnerRole);

return { roomOwnerRole, roomEditorRole };
};

const setupUserWithSecondarySchool = () => {
const secondarySchool = schoolFactory.build();
const otherSchool = schoolFactory.build();
Expand All @@ -194,99 +205,109 @@ describe('RoomMembershipService', () => {
roles: [role],
secondarySchools: [{ schoolId: secondarySchool.id, role: new RoleDto(guestTeacher) }],
});
const externalUserId = externalUser.id as string;

return { secondarySchool, externalUser, otherSchool };
return { secondarySchool, externalUser, externalUserId, otherSchool };
};

it('should pass the schoolId of the room', async () => {
const { secondarySchool, externalUser } = setupUserWithSecondarySchool();

const roomEditorRole = roleFactory.buildWithId({ name: RoleName.ROOMEDITOR });
describe('when removing user from a different school, with no further groups on host school', () => {
const setup = () => {
const { secondarySchool, externalUserId } = setupUserWithSecondarySchool();
const { roomEditorRole } = setupRoomRoles();

const { room, group, roomMembership } = setupGroupAndRoom(secondarySchool.id);
group.addUser({ userId: externalUser.id as string, roleId: roomEditorRole.id });
const { room, group } = setupGroupAndRoom(secondarySchool.id);
group.addUser({ userId: externalUserId, roleId: roomEditorRole.id });

roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership);
groupService.findById.mockResolvedValue(group);
groupService.removeUsersFromGroup.mockResolvedValue(group);
mockGroupsAtSchoolAfterRemoval([]);
mockGroupsAtSchoolAfterRemoval([]);

await service.removeMembersFromRoom(room.id, [externalUser.id as string]);
return { secondarySchool, externalUserId, room, group };
};

expect(groupService.findGroups).toHaveBeenCalledWith(expect.objectContaining({ schoolId: secondarySchool.id }));
});
it('should pass the schoolId of the room', async () => {
const { secondarySchool, externalUserId, room } = setup();

it('should remove roomMembership', async () => {
const user = userFactory.buildWithId();
const { room, group, roomMembership } = setupGroupAndRoom(user.school.id);
const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER });
groupService.findById.mockResolvedValue(group);
roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership);
roleService.findByName.mockResolvedValue(roomOwnerRole);
mockGroupsAtSchoolAfterRemoval([group]);
await service.removeMembersFromRoom(room.id, [externalUserId]);

await service.removeMembersFromRoom(room.id, [user.id]);
expect(groupService.findGroups).toHaveBeenCalledWith(
expect.objectContaining({ schoolId: secondarySchool.id })
);
});

expect(groupService.removeUsersFromGroup).toHaveBeenCalledWith(group.id, [user.id]);
});
it('should remove user from room', async () => {
const { group, externalUserId, room } = setup();

describe('when after removal: user is not in any room of that secondary school', () => {
it('should remove user from secondary school', async () => {
const { secondarySchool, externalUser } = setupUserWithSecondarySchool();
await service.removeMembersFromRoom(room.id, [externalUserId]);

const { room, group, roomMembership } = setupGroupAndRoom(secondarySchool.id);
const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER });
const roomEditorRole = roleFactory.buildWithId({ name: RoleName.ROOMEDITOR });
group.addUser({ userId: externalUser.id as string, roleId: roomEditorRole.id });
expect(groupService.removeUsersFromGroup).toHaveBeenCalledWith(group.id, [externalUserId]);
});

roleService.findByName.mockResolvedValue(roomOwnerRole);
roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership);
groupService.findById.mockResolvedValue(group);
groupService.removeUsersFromGroup.mockResolvedValue(group);
mockGroupsAtSchoolAfterRemoval([]);
it('should remove user from secondary school', async () => {
const { secondarySchool, externalUserId, room } = setup();

await service.removeMembersFromRoom(room.id, [externalUser.id as string]);
await service.removeMembersFromRoom(room.id, [externalUserId]);

expect(userService.removeSecondarySchoolFromUsers).toHaveBeenCalledWith(
[externalUser.id],
secondarySchool.id
);
expect(userService.removeSecondarySchoolFromUsers).toHaveBeenCalledWith([externalUserId], secondarySchool.id);
});
});

describe('when after removal: user is still in a room of that secondary school', () => {
it('should not remove user from secondary school', async () => {
describe('when removing user from a different school, with further groups on host school', () => {
const setup = () => {
const { secondarySchool, externalUser } = setupUserWithSecondarySchool();
const { roomEditorRole } = setupRoomRoles();

const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER });
const roomEditorRole = roleFactory.buildWithId({ name: RoleName.ROOMEDITOR });

const { room, group, roomMembership } = setupGroupAndRoom(secondarySchool.id);
const { room, group } = setupGroupAndRoom(secondarySchool.id);
group.addUser({ userId: externalUser.id as string, roleId: roomEditorRole.id });
const { group: group2 } = setupGroupAndRoom(secondarySchool.id);
group2.addUser({ userId: externalUser.id as string, roleId: roomEditorRole.id });

roleService.findByName.mockResolvedValue(roomOwnerRole);
roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership);
groupService.findById.mockResolvedValue(group);
groupService.removeUsersFromGroup.mockResolvedValue(group);
mockGroupsAtSchoolAfterRemoval([group2]);

return { externalUser, room };
};

it('should not remove user from secondary school', async () => {
const { externalUser, room } = setup();

await service.removeMembersFromRoom(room.id, [externalUser.id as string]);

expect(userService.removeSecondarySchoolFromUsers).not.toHaveBeenCalled();
});
});

describe('when owner should be removed', () => {
it('should throw a badrequest exception', async () => {
describe('when removing user from the same school', () => {
const setup = () => {
const user = userFactory.buildWithId();
const { room, group, roomMembership } = setupGroupAndRoom(user.school.id);
roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership);
const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER });
const { roomEditorRole } = setupRoomRoles();
const { room, group } = setupGroupAndRoom(user.school.id);
group.addUser({ userId: user.id, roleId: roomEditorRole.id });

mockGroupsAtSchoolAfterRemoval([group]);

return { user, room, group };
};

it('should remove user from room', async () => {
const { user, group, room } = setup();

await service.removeMembersFromRoom(room.id, [user.id]);

expect(groupService.removeUsersFromGroup).toHaveBeenCalledWith(group.id, [user.id]);
});
});

describe('when removing the owner of the room', () => {
const setup = () => {
const user = userFactory.buildWithId();
const { room, group } = setupGroupAndRoom(user.school.id);
const { roomOwnerRole } = setupRoomRoles();

group.addUser({ userId: user.id, roleId: roomOwnerRole.id });
groupService.findById.mockResolvedValue(group);
roleService.findByName.mockResolvedValue(roomOwnerRole);

return { user, room };
};

it('should throw a badrequest exception', async () => {
const { user, room } = setup();

await expect(service.removeMembersFromRoom(room.id, [user.id])).rejects.toThrowError(BadRequestException);
});
Expand Down

0 comments on commit 89bd3b9

Please sign in to comment.