Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand Down
Loading