Skip to content

Commit

Permalink
WIP: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hupf committed Oct 16, 2024
1 parent e165eee commit f884c85
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 82 deletions.
12 changes: 6 additions & 6 deletions src/app/events/utils/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function findResult(

export function replaceResult(
result: Result,
tests: Test[],
tests: ReadonlyArray<Test>,
ignore?: "grade" | "points",
): Test[] {
return tests.map((test) =>
Expand All @@ -39,14 +39,14 @@ export function replaceResult(
export function deleteResult(
testId: number,
studentId: number,
tests: Test[],
tests: ReadonlyArray<Test>,
): Test[] {
return tests.map((test) =>
test.Id === testId ? deleteResultByStudentId(studentId, test) : test,
);
}

export function toggleIsPublished(id: number, tests: Test[]) {
export function toggleIsPublished(id: number, tests: ReadonlyArray<Test>) {
return tests.map((test) =>
test.Id === id ? { ...test, IsPublished: !test.IsPublished } : test,
);
Expand Down Expand Up @@ -80,7 +80,7 @@ export function resultOfStudent(studentId: number, test: Test): Maybe<Result> {

export function removeTestById(
testId: number,
tests: Test[] | null,
tests: ReadonlyArray<Test> | null,
): Test[] | null {
if (tests === null) return null;
return tests.filter((test) => test.Id !== testId);
Expand Down Expand Up @@ -121,15 +121,15 @@ export function replaceResultInTest(
return { ...test, Results: [...filteredResults, newResult] };
}

export function sortByDate(tests: Test[]) {
export function sortByDate(tests: ReadonlyArray<Test>) {
return tests
.slice()
.sort((test1, test2) => test2.Date.getTime() - test1.Date.getTime());
}

export function gradingScaleOfTest(
test: Test,
gradingScales: GradingScale[],
gradingScales: ReadonlyArray<GradingScale>,
): Option<GradingScale> {
return (
gradingScales?.find(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
<div
class="bkd-container bkd-container-limited"
*bkdLet="{
loading: myGradesService.loading$ | async,
studentId: myGradesService.studentId$ | async,
courses: myGradesService.studentCoursesSorted$ | async,
gradingScales: myGradesService.gradingScales$ | async,
} as data"
>
<div class="bkd-container bkd-container-limited">
<h1>{{ "my-grades.title" | translate }}</h1>
<bkd-my-grades-header></bkd-my-grades-header>
@if (!data.loading) {
@if (myGradesService.loading$ | async) {
<bkd-spinner></bkd-spinner>
} @else {
<bkd-dossier-grades-view
[courses]="data.courses"
[studentId]="data.studentId"
[gradingScales]="data.gradingScales"
[courses]="myGradesService.studentCoursesSorted$ | async"
[studentId]="myGradesService.studentId$ | async"
[gradingScales]="myGradesService.gradingScales$ | async"
[isEditable]="false"
></bkd-dossier-grades-view>
}
@if (data.loading) {
<bkd-spinner></bkd-spinner>
}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ describe("MyGradesShowComponent", () => {
{
provide: MyGradesService,
useValue: {
loading$: of(false),
studentId$: of(1),
studentCourses$: of([]),
studentCoursesSorted$: of([]),
gradingScales$: of([]),
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export interface CourseWithGrades {
],
})
export class DossierGradesViewComponent implements OnChanges {
@Input() courses: Course[];
@Input() courses: ReadonlyArray<Course>;
@Input() studentId: number;
@Input() gradingScales: GradingScale[];
@Input() gradingScales: ReadonlyArray<GradingScale>;
@Input() isEditable: boolean = true;

constructor(public dossierGradeService: DossierGradesService) {}
constructor(public dossierGradesService: DossierGradesService) {}

decoratedCoursesSubject$ = new BehaviorSubject<CourseWithGrades[]>([]);

Expand All @@ -64,12 +64,12 @@ export class DossierGradesViewComponent implements OnChanges {
}

private decorateCourses(): CourseWithGrades[] {
return this.courses?.map((course) => {
const finalGrade = this.dossierGradeService.getFinalGradeForStudent(
return this.courses.map((course) => {
const finalGrade = this.dossierGradesService.getFinalGradeForStudent(
course,
this.studentId,
);
const grades = this.dossierGradeService.getGradesForStudent(
const grades = this.dossierGradesService.getGradesForStudent(
course,
this.studentId,
this.gradingScales,
Expand All @@ -78,11 +78,11 @@ export class DossierGradesViewComponent implements OnChanges {
return {
course,
finalGrade,
grading: this.dossierGradeService.getGradingForStudent(
grading: this.dossierGradesService.getGradingForStudent(
course,
this.studentId,
),
gradingScale: this.dossierGradeService.getGradingScaleOfCourse(
gradingScale: this.dossierGradesService.getGradingScaleOfCourse(
course,
this.gradingScales,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
<ng-container
*bkdLet="{
studentId: state.studentId$ | async,
loading: dossierGradesService.loading$ | async,
courses: dossierGradesService.studentCourses$ | async,
gradingScales: dossierGradesService.gradingScales$ | async,
} as data"
>
@if (!data.loading) {
<bkd-dossier-grades-view
[courses]="data.courses"
[studentId]="data.studentId"
[gradingScales]="data.gradingScales"
></bkd-dossier-grades-view>
}
@if (data.loading) {
<bkd-spinner></bkd-spinner>
}
</ng-container>
@if (dossierGradesService.loading$ | async) {
<bkd-spinner></bkd-spinner>
} @else {
<bkd-dossier-grades-view
[courses]="dossierGradesService.studentCourses$ | async"
[studentId]="state.studentId$ | async"
[gradingScales]="dossierGradesService.gradingScales$ | async"
></bkd-dossier-grades-view>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
import { StorageService } from "src/app/shared/services/storage.service";
import { buildTestModuleMetadata } from "src/spec-helpers";
import { buildCourse, buildGradingScale } from "../../../../../spec-builders";
import { Course } from "../../../models/course.model";
import { GradingScale } from "../../../models/grading-scale.model";
import { DossierGradesComponent } from "./dossier-grades.component";

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand All @@ -22,12 +20,6 @@ describe("DossierGradesComponent", () => {
let currentDossier$: BehaviorSubject<DossierPage>;

let dossierGradesServiceMock: DossierGradesService;
let loading$: BehaviorSubject<boolean>;
let studentCourses$: BehaviorSubject<Course[]>;
let gradingScales$: BehaviorSubject<readonly GradingScale[]>;

const courses = [buildCourse(1234)];
const gradingScales = [buildGradingScale(1)];

beforeEach(async () => {
currentDossier$ = new BehaviorSubject<DossierPage>("grades");
Expand All @@ -37,16 +29,17 @@ describe("DossierGradesComponent", () => {
studentId$: of(123),
} as unknown as DossierStateService;

loading$ = new BehaviorSubject<any>(false);
studentCourses$ = new BehaviorSubject<Course[]>(courses);
gradingScales$ = new BehaviorSubject<readonly GradingScale[]>(
gradingScales,
);

dossierGradesServiceMock = {
loading$,
studentCourses$,
gradingScales$,
loading$: of(false),
studentCourses$: of([buildCourse(1234)]),
gradingScales$: of([buildGradingScale(1)]),
setStudentId: jasmine.createSpy("setStudentId"),
getFinalGradeForStudent: jasmine.createSpy("getFinalGradeForStudent"),
getGradesForStudent: jasmine
.createSpy("getGradesForStudent")
.and.returnValue([]),
getGradingForStudent: jasmine.createSpy("getGradingForStudent"),
getGradingScaleOfCourse: jasmine.createSpy("getGradingScaleOfCourse"),
} as unknown as DossierGradesService;

await TestBed.configureTestingModule(
Expand All @@ -56,10 +49,7 @@ describe("DossierGradesComponent", () => {
{ provide: DossierStateService, useValue: dossierStateServiceMock },
{
provide: DossierGradesService,
useValue: {
dossierGradesServiceMock,
setStudentId: jasmine.createSpy("setStudentId"),
},
useValue: dossierGradesServiceMock,
},
{
provide: StorageService,
Expand Down
21 changes: 10 additions & 11 deletions src/app/shared/services/dossier-grades.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ export class DossierGradesService {
return course?.Gradings?.find((grade) => grade.StudentId === studentId);
}

getGradingScaleOfCourse(course: Course, gradingScales: GradingScale[]) {
getGradingScaleOfCourse(
course: Course,
gradingScales: ReadonlyArray<GradingScale>,
) {
return gradingScales?.find(
(gradingScale) => gradingScale.Id === course.GradingScaleId,
);
Expand All @@ -122,7 +125,7 @@ export class DossierGradesService {
getGradesForStudent(
course: Course,
studentId: number,
gradingScales: GradingScale[],
gradingScales: ReadonlyArray<GradingScale>,
): ValueWithWeight[] {
return (
course.Tests?.flatMap((test) => {
Expand Down Expand Up @@ -169,14 +172,12 @@ export class DossierGradesService {
// if it is merged, integrate changes and dry it up.

private tests$ = this.studentCourses$.pipe(
map((courses) =>
courses.flatMap((course: Course) => course.Tests).filter(notNull),
),
map((courses) => courses.flatMap((course) => course.Tests).filter(notNull)),
);

private gradingScaleIdsFromTests$ = this.tests$.pipe(
map((tests: Test[]) =>
[...tests.map((test: Test) => test.GradingScaleId)]
map((tests) =>
[...tests.map((test) => test.GradingScaleId)]
.filter(notNull)
.filter(unique),
),
Expand All @@ -185,7 +186,7 @@ export class DossierGradesService {
private gradingScaleIdsFromCourses$ = this.studentCourses$.pipe(
map((courses) =>
courses
.flatMap((course: Course) => course.GradingScaleId)
.flatMap((course) => course.GradingScaleId)
.filter(notNull)
.filter(unique),
),
Expand All @@ -203,9 +204,7 @@ export class DossierGradesService {
gradingScales$ = this.gradingScaleIds$.pipe(
switchMap((ids) =>
forkJoin(
ids.map((id: number) =>
this.gradingScalesRestService.getGradingScale(id),
),
ids.map((id) => this.gradingScalesRestService.getGradingScale(id)),
),
),
);
Expand Down

0 comments on commit f884c85

Please sign in to comment.