diff --git a/frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/model/dashboardViewModel.test.ts b/frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/model/dashboardViewModel.test.ts index d3d8bc68ab..5c5d62a5b1 100644 --- a/frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/model/dashboardViewModel.test.ts +++ b/frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/model/dashboardViewModel.test.ts @@ -72,6 +72,35 @@ describe("dashboardViewModel", () => { pickDisplayedEnrollmentForLegacyDashboard(course, [lower, higher]), ).toEqual(higher) }) + + test("keeps an older enrolled run when the course payload only lists the newer run", () => { + const olderRun = factories.courses.courseRun({ + id: 101, + title: "Older run", + }) + const newerRun = factories.courses.courseRun({ + id: 202, + title: "Newer run", + }) + const course = factories.courses.course({ + id: 77, + courseruns: [newerRun], + next_run_id: newerRun.id, + }) + const olderEnrollment = factories.enrollment.courseEnrollment({ + run: { + id: olderRun.id, + title: olderRun.title, + course: { id: course.id, title: course.title }, + }, + certificate: null, + grades: [], + }) + + expect( + pickDisplayedEnrollmentForLegacyDashboard(course, [olderEnrollment]), + ).toEqual(olderEnrollment) + }) }) describe("groupCourseRunEnrollmentsByCourseId", () => { diff --git a/frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/model/dashboardViewModel.ts b/frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/model/dashboardViewModel.ts index e04abd5ff5..c166cceb43 100644 --- a/frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/model/dashboardViewModel.ts +++ b/frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/model/dashboardViewModel.ts @@ -77,6 +77,17 @@ const getMaxEnrollmentGrade = (enrollment: CourseRunEnrollmentV3): number => { return Math.max(0, ...enrollment.grades.map((grade) => grade.grade ?? 0)) } +const enrollmentBelongsToCourse = ( + course: CourseWithCourseRunsSerializerV2, + enrollment: CourseRunEnrollmentV3, +): boolean => { + if (enrollment.run.course?.id === course.id) { + return true + } + + return course.courseruns.some((run) => run.id === enrollment.run.id) +} + /** * Legacy display policy used by dashboard cards. * @@ -90,7 +101,7 @@ const pickDisplayedEnrollmentForLegacyDashboard = ( enrollments: CourseRunEnrollmentV3[], ): CourseRunEnrollmentV3 | null => { const courseEnrollments = enrollments.filter((enrollment) => - course.courseruns.some((run) => run.id === enrollment.run.id), + enrollmentBelongsToCourse(course, enrollment), ) if (courseEnrollments.length === 0) { return null