Skip to content

Commit

Permalink
uc unit test WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCapCoder committed Oct 27, 2023
1 parent 66458f2 commit bdf5456
Showing 1 changed file with 63 additions and 4 deletions.
67 changes: 63 additions & 4 deletions apps/server/src/modules/group/uc/group.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { GroupService } from '../service';
import { ClassInfoDto, ResolvedGroupDto } from './dto';
import { ClassRootType } from './dto/class-root-type';
import { GroupUc } from './group.uc';
import { SchoolYearQueryType } from '../controller/dto';

describe('GroupUc', () => {
let module: TestingModule;
Expand Down Expand Up @@ -124,7 +125,7 @@ describe('GroupUc', () => {
it('should throw forbidden', async () => {
const { user, error } = setup();

const func = () => uc.findAllClassesForSchool(user.id, user.school.id);
const func = () => uc.findAllClassesForSchool(user.id, user.school.id, SchoolYearQueryType.CURRENT_YEAR);

await expect(func).rejects.toThrow(error);
});
Expand Down Expand Up @@ -154,12 +155,20 @@ describe('GroupUc', () => {
roles: [{ id: studentUser.roles[0].id, name: studentUser.roles[0].name }],
});
const schoolYear: SchoolYearEntity = schoolYearFactory.buildWithId();
const nextSchoolYear: SchoolYearEntity = schoolYearFactory.buildWithId({
startDate: schoolYear.endDate,
});
const clazz: Class = classFactory.build({
name: 'A',
teacherIds: [teacherUser.id],
source: 'LDAP',
year: schoolYear.id,
});
const successorClass: Class = classFactory.build({
name: 'NEW',
teacherIds: [teacherUser.id],
year: nextSchoolYear.id,
});
const system: SystemDto = new SystemDto({
id: new ObjectId().toHexString(),
displayName: 'External System',
Expand All @@ -181,7 +190,7 @@ describe('GroupUc', () => {

schoolService.getSchoolById.mockResolvedValueOnce(school);
authorizationService.getUserWithPermissions.mockResolvedValueOnce(teacherUser);
classService.findClassesForSchool.mockResolvedValueOnce([clazz]);
classService.findClassesForSchool.mockResolvedValueOnce([clazz, successorClass]);
groupService.findClassesForSchool.mockResolvedValueOnce([group, groupWithSystem]);
systemService.findById.mockResolvedValue(system);
userService.findById.mockImplementation((userId: string): Promise<UserDO> => {
Expand All @@ -207,11 +216,13 @@ describe('GroupUc', () => {
throw new Error();
});
schoolYearService.findById.mockResolvedValue(schoolYear);
schoolYearService.getCurrentSchoolYear.mockResolvedValue(schoolYear);

return {
teacherUser,
school,
clazz,
successorClass,
group,
groupWithSystem,
system,
Expand All @@ -222,7 +233,7 @@ describe('GroupUc', () => {
it('should check the required permissions', async () => {
const { teacherUser, school } = setup();

await uc.findAllClassesForSchool(teacherUser.id, teacherUser.school.id);
await uc.findAllClassesForSchool(teacherUser.id, teacherUser.school.id, SchoolYearQueryType.CURRENT_YEAR);

expect(authorizationService.checkPermission).toHaveBeenCalledWith<[User, LegacySchoolDo, AuthorizationContext]>(
teacherUser,
Expand All @@ -238,7 +249,11 @@ describe('GroupUc', () => {
it('should return all classes sorted by name', async () => {
const { teacherUser, clazz, group, groupWithSystem, system, schoolYear } = setup();

const result: Page<ClassInfoDto> = await uc.findAllClassesForSchool(teacherUser.id, teacherUser.school.id);
const result: Page<ClassInfoDto> = await uc.findAllClassesForSchool(
teacherUser.id,
teacherUser.school.id,
SchoolYearQueryType.CURRENT_YEAR
);

expect(result).toEqual<Page<ClassInfoDto>>({
data: [
Expand Down Expand Up @@ -280,6 +295,7 @@ describe('GroupUc', () => {
const result: Page<ClassInfoDto> = await uc.findAllClassesForSchool(
teacherUser.id,
teacherUser.school.id,
SchoolYearQueryType.CURRENT_YEAR,
undefined,
undefined,
'externalSourceName',
Expand Down Expand Up @@ -326,6 +342,7 @@ describe('GroupUc', () => {
const result: Page<ClassInfoDto> = await uc.findAllClassesForSchool(
teacherUser.id,
teacherUser.school.id,
SchoolYearQueryType.CURRENT_YEAR,
1,
1,
'name',
Expand All @@ -346,6 +363,48 @@ describe('GroupUc', () => {
});
});
});

describe('when querying for classes from next school year', () => {
it('should only return classes from next school year', async () => {
const { teacherUser, successorClass } = setup();

const result: Page<ClassInfoDto> = await uc.findAllClassesForSchool(
teacherUser.id,
teacherUser.school.id,
SchoolYearQueryType.NEXT_YEAR
);

expect(result).toEqual<Page<ClassInfoDto>>({
data: [
{
id: successorClass.id,
name: successorClass.name,
type: ClassRootType.CLASS,
teachers: [teacherUser.lastName],
studentCount: 0,
},
],
total: 1,
});
});
});

describe('when querying for archived classes', () => {
it('should only return classes from previous school years', async () => {
const { teacherUser } = setup();

const result: Page<ClassInfoDto> = await uc.findAllClassesForSchool(
teacherUser.id,
teacherUser.school.id,
SchoolYearQueryType.NEXT_YEAR
);

expect(result).toEqual<Page<ClassInfoDto>>({
data: [],
total: 0,
});
});
});
});
});

Expand Down

0 comments on commit bdf5456

Please sign in to comment.