From f884c858d48ea586da73ec6d0a2275812ac11313 Mon Sep 17 00:00:00 2001 From: Mathis Hofer Date: Wed, 16 Oct 2024 13:30:12 +0200 Subject: [PATCH] WIP: fix tests --- src/app/events/utils/tests.ts | 12 +++---- .../my-grades-show.component.html | 23 ++++--------- .../my-grades-show.component.spec.ts | 4 ++- .../dossier-grades-view.component.ts | 16 +++++----- .../dossier-grades.component.html | 28 ++++++---------- .../dossier-grades.component.spec.ts | 32 +++++++------------ .../shared/services/dossier-grades.service.ts | 21 ++++++------ 7 files changed, 54 insertions(+), 82 deletions(-) diff --git a/src/app/events/utils/tests.ts b/src/app/events/utils/tests.ts index e345958ba..5fcfb3542 100644 --- a/src/app/events/utils/tests.ts +++ b/src/app/events/utils/tests.ts @@ -26,7 +26,7 @@ export function findResult( export function replaceResult( result: Result, - tests: Test[], + tests: ReadonlyArray, ignore?: "grade" | "points", ): Test[] { return tests.map((test) => @@ -39,14 +39,14 @@ export function replaceResult( export function deleteResult( testId: number, studentId: number, - tests: Test[], + tests: ReadonlyArray, ): 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) { return tests.map((test) => test.Id === id ? { ...test, IsPublished: !test.IsPublished } : test, ); @@ -80,7 +80,7 @@ export function resultOfStudent(studentId: number, test: Test): Maybe { export function removeTestById( testId: number, - tests: Test[] | null, + tests: ReadonlyArray | null, ): Test[] | null { if (tests === null) return null; return tests.filter((test) => test.Id !== testId); @@ -121,7 +121,7 @@ export function replaceResultInTest( return { ...test, Results: [...filteredResults, newResult] }; } -export function sortByDate(tests: Test[]) { +export function sortByDate(tests: ReadonlyArray) { return tests .slice() .sort((test1, test2) => test2.Date.getTime() - test1.Date.getTime()); @@ -129,7 +129,7 @@ export function sortByDate(tests: Test[]) { export function gradingScaleOfTest( test: Test, - gradingScales: GradingScale[], + gradingScales: ReadonlyArray, ): Option { return ( gradingScales?.find( diff --git a/src/app/my-grades/components/my-grades-show/my-grades-show.component.html b/src/app/my-grades/components/my-grades-show/my-grades-show.component.html index e9c4c68a5..989c574b0 100644 --- a/src/app/my-grades/components/my-grades-show/my-grades-show.component.html +++ b/src/app/my-grades/components/my-grades-show/my-grades-show.component.html @@ -1,23 +1,14 @@ -
+

{{ "my-grades.title" | translate }}

- @if (!data.loading) { + @if (myGradesService.loading$ | async) { + + } @else { } - @if (data.loading) { - - }
diff --git a/src/app/my-grades/components/my-grades-show/my-grades-show.component.spec.ts b/src/app/my-grades/components/my-grades-show/my-grades-show.component.spec.ts index d752262e0..04d445d15 100644 --- a/src/app/my-grades/components/my-grades-show/my-grades-show.component.spec.ts +++ b/src/app/my-grades/components/my-grades-show/my-grades-show.component.spec.ts @@ -17,8 +17,10 @@ describe("MyGradesShowComponent", () => { { provide: MyGradesService, useValue: { + loading$: of(false), studentId$: of(1), - studentCourses$: of([]), + studentCoursesSorted$: of([]), + gradingScales$: of([]), }, }, { diff --git a/src/app/shared/components/student-dossier/dossier-grades-view/dossier-grades-view.component.ts b/src/app/shared/components/student-dossier/dossier-grades-view/dossier-grades-view.component.ts index c89201048..7cf1123de 100644 --- a/src/app/shared/components/student-dossier/dossier-grades-view/dossier-grades-view.component.ts +++ b/src/app/shared/components/student-dossier/dossier-grades-view/dossier-grades-view.component.ts @@ -50,12 +50,12 @@ export interface CourseWithGrades { ], }) export class DossierGradesViewComponent implements OnChanges { - @Input() courses: Course[]; + @Input() courses: ReadonlyArray; @Input() studentId: number; - @Input() gradingScales: GradingScale[]; + @Input() gradingScales: ReadonlyArray; @Input() isEditable: boolean = true; - constructor(public dossierGradeService: DossierGradesService) {} + constructor(public dossierGradesService: DossierGradesService) {} decoratedCoursesSubject$ = new BehaviorSubject([]); @@ -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, @@ -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, ), diff --git a/src/app/shared/components/student-dossier/dossier-grades/dossier-grades.component.html b/src/app/shared/components/student-dossier/dossier-grades/dossier-grades.component.html index 269a94739..55467deb7 100644 --- a/src/app/shared/components/student-dossier/dossier-grades/dossier-grades.component.html +++ b/src/app/shared/components/student-dossier/dossier-grades/dossier-grades.component.html @@ -1,19 +1,9 @@ - - @if (!data.loading) { - - } - @if (data.loading) { - - } - +@if (dossierGradesService.loading$ | async) { + +} @else { + +} diff --git a/src/app/shared/components/student-dossier/dossier-grades/dossier-grades.component.spec.ts b/src/app/shared/components/student-dossier/dossier-grades/dossier-grades.component.spec.ts index 0137fbafe..b1a3f4d71 100644 --- a/src/app/shared/components/student-dossier/dossier-grades/dossier-grades.component.spec.ts +++ b/src/app/shared/components/student-dossier/dossier-grades/dossier-grades.component.spec.ts @@ -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 */ @@ -22,12 +20,6 @@ describe("DossierGradesComponent", () => { let currentDossier$: BehaviorSubject; let dossierGradesServiceMock: DossierGradesService; - let loading$: BehaviorSubject; - let studentCourses$: BehaviorSubject; - let gradingScales$: BehaviorSubject; - - const courses = [buildCourse(1234)]; - const gradingScales = [buildGradingScale(1)]; beforeEach(async () => { currentDossier$ = new BehaviorSubject("grades"); @@ -37,16 +29,17 @@ describe("DossierGradesComponent", () => { studentId$: of(123), } as unknown as DossierStateService; - loading$ = new BehaviorSubject(false); - studentCourses$ = new BehaviorSubject(courses); - gradingScales$ = new BehaviorSubject( - 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( @@ -56,10 +49,7 @@ describe("DossierGradesComponent", () => { { provide: DossierStateService, useValue: dossierStateServiceMock }, { provide: DossierGradesService, - useValue: { - dossierGradesServiceMock, - setStudentId: jasmine.createSpy("setStudentId"), - }, + useValue: dossierGradesServiceMock, }, { provide: StorageService, diff --git a/src/app/shared/services/dossier-grades.service.ts b/src/app/shared/services/dossier-grades.service.ts index 84e9fe3a6..9ff5298ec 100644 --- a/src/app/shared/services/dossier-grades.service.ts +++ b/src/app/shared/services/dossier-grades.service.ts @@ -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, + ) { return gradingScales?.find( (gradingScale) => gradingScale.Id === course.GradingScaleId, ); @@ -122,7 +125,7 @@ export class DossierGradesService { getGradesForStudent( course: Course, studentId: number, - gradingScales: GradingScale[], + gradingScales: ReadonlyArray, ): ValueWithWeight[] { return ( course.Tests?.flatMap((test) => { @@ -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), ), @@ -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), ), @@ -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)), ), ), );