Skip to content

Commit

Permalink
Most relevant run should be determined in the backend only (#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
annagav authored Aug 29, 2024
1 parent 21a4188 commit 11e500a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 156 deletions.
50 changes: 42 additions & 8 deletions frontend/public/src/components/CourseProductDetailEnroll_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,20 @@ describe("CourseProductDetailEnrollShallowRender", () => {

it("checks for enroll now button", async () => {
const courseRun = makeCourseRunDetail()
courseRun.is_enrollable = true
course.courseruns = [courseRun]
course.next_run_id = courseRun.id
const { inner } = await renderPage(
{
entities: {
courseRuns: [courseRun]
courseRuns: [courseRun],
courses: [course]
},
queries: {
courses: {
isPending: false,
status: 200
},
courseRuns: {
isPending: false,
status: 200
Expand All @@ -103,10 +111,13 @@ describe("CourseProductDetailEnrollShallowRender", () => {
{}
)

assert.equal(inner.find(".enroll-now").at(0).text(), "Enroll Now")
assert.equal(
inner.find(".btn-enrollment-button").at(0).text(),
"Enroll Now"
)
})

it("checks for enroll now button should appear disabled if enrollment start in future", async () => {
it("checks for Access Course Materials button should appear disabled if no enrollable runs", async () => {
const courseRun = makeCourseRunDetail()
courseRun["is_enrollable"] = false
const course = makeCourseDetailNoRuns()
Expand All @@ -125,13 +136,15 @@ describe("CourseProductDetailEnrollShallowRender", () => {
},
{}
)
assert.isTrue(inner.find(".enroll-now").at(0).exists())
assert.isTrue(inner.find(".enroll-now").at(0).prop("disabled"))
assert.isTrue(inner.find(".btn-enrollment-button").at(0).exists())
assert.isTrue(inner.find(".btn-enrollment-button").at(0).prop("disabled"))
})

it("checks for enroll now button should appear if enrollment start not in future", async () => {
const courseRun = makeCourseRunDetail()
courseRun.is_enrollable = true
const course = makeCourseDetailNoRuns()
course.next_run_id = courseRun.id
course.courseruns = [courseRun]
const { inner } = await renderPage(
{
Expand Down Expand Up @@ -161,7 +174,9 @@ describe("CourseProductDetailEnrollShallowRender", () => {
is_archived: true,
upgrade_deadline: null
}
courseRun.is_enrollable = true
const course = makeCourseDetailNoRuns()
course.next_run_id = courseRun.id
course.courseruns = [courseRun]
const { inner } = await renderPage(
{
Expand All @@ -187,10 +202,15 @@ describe("CourseProductDetailEnrollShallowRender", () => {

it("checks for form-based enrollment form if there is no product", async () => {
const courseRun = makeCourseRunDetail()
courseRun.is_enrollable = true
const course = makeCourseDetailNoRuns()
course.next_run_id = courseRun.id
course.courseruns = [courseRun]
const { inner } = await renderPage(
{
entities: {
courseRuns: [courseRun]
courseRuns: [courseRun],
courses: [course]
},
queries: {
courseRuns: {
Expand Down Expand Up @@ -244,6 +264,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {

const course = makeCourseDetailNoRuns()
course.courseruns = courseRuns
course.next_run_id = courseRun.id

const entities = {
currentUser: userExists ? currentUser : makeAnonymousUser(),
Expand Down Expand Up @@ -280,6 +301,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
}
}
const course = makeCourseDetailNoRuns()
course.next_run_id = courseRun.id
course.courseruns = [courseRun]
isFinancialAssistanceAvailableStub.returns(true)
const { inner } = await renderPage(
Expand Down Expand Up @@ -339,6 +361,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
courseRun["is_self_paced"] = false
}
const course = makeCourseDetailNoRuns()
course.next_run_id = courseRun.id
course.courseruns = [courseRun]
const entities = {
currentUser: currentUser,
Expand Down Expand Up @@ -372,7 +395,8 @@ describe("CourseProductDetailEnrollShallowRender", () => {
}
const course = {
...makeCourseDetailWithRuns(),
courseruns: [courseRun]
courseruns: [courseRun],
next_run_id: courseRun.id
}

const entities = {
Expand Down Expand Up @@ -451,6 +475,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
}
]
course.courseruns = [courseRun]
course.next_run_id = courseRun.id
const { inner } = await renderPage(
{
entities: {
Expand Down Expand Up @@ -495,6 +520,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
}
}
]
course.next_run_id = courseRun.id
course.courseruns = [courseRun]
const { inner } = await renderPage(
{
Expand Down Expand Up @@ -543,6 +569,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
}
]
isFinancialAssistanceAvailableStub.returns(false)
course.next_run_id = courseRun.id
course.courseruns = [courseRun]
const { inner } = await renderPage(
{
Expand Down Expand Up @@ -632,6 +659,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
product_flexible_price: {}
}
]
course.next_run_id = courseRun.id
course.courseruns = [courseRun]
const { inner } = await renderPage(
{
Expand Down Expand Up @@ -663,6 +691,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
if (multiples) {
courseRuns.push(courseRun)
}
course.next_run_id = courseRun.id
course.courseruns = courseRuns

const { inner } = await renderPage({
Expand Down Expand Up @@ -715,6 +744,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
courseRun["is_upgradable"] = false
const courseRuns = [courseRun]
courseRuns.push(courseRun)
course.next_run_id = courseRun.id
course.courseruns = courseRuns

const { inner } = await renderPage({
Expand Down Expand Up @@ -754,6 +784,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
}
const courseRuns = [courseRun]
courseRuns.push(runWithMixedInfo)
course.next_run_id = courseRun.id
course.courseruns = courseRuns

const { inner } = await renderPage({
Expand Down Expand Up @@ -828,7 +859,8 @@ describe("CourseProductDetailEnrollShallowRender", () => {
]
const course = {
...makeCourseDetailWithRuns(),
courseruns: [pastCourseRun, currentCourseRun]
courseruns: [pastCourseRun, currentCourseRun],
next_run_id: currentCourseRun.id
}

const { inner } = await renderPage({
Expand Down Expand Up @@ -884,6 +916,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
} else {
courseRun["start_date"] = moment().subtract(10, "months").toISOString()
}
course.next_run_id = courseRun.id
course.courseruns = [courseRun]

const entities = {
Expand Down Expand Up @@ -953,6 +986,7 @@ describe("CourseProductDetailEnrollShallowRender", () => {
}

course.courseruns = courseRuns
course.next_run_id = courseRun.id

const entities = {
currentUser: userExists ? currentUser : makeAnonymousUser(),
Expand Down
37 changes: 0 additions & 37 deletions frontend/public/src/lib/courseApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ export const getFirstRelevantRun = (
/*
Finds the next most relevant course run:
- If the course has a next_run_id, return that run
- If there are future runs, return the run closer to now
- If all runs are in the past, return the most recently ended run
- If there are no runs, return null
Args:
Expand All @@ -235,40 +233,5 @@ export const getFirstRelevantRun = (
if (course.next_run_id) {
return courseRuns.find(run => run.id === course.next_run_id)
}

const now = moment()
const enrollableRuns = courseRuns.filter(run => run.is_enrollable)
if (enrollableRuns.length > 0) {
if (
enrollableRuns.some(
run => run.start_date && moment(run.start_date).isSameOrAfter(now)
)
) {
return enrollableRuns
.filter(
run => run.start_date && moment(run.start_date).isSameOrAfter(now)
)
.reduce((prev, curr) =>
moment(curr.start_date).isBefore(moment(prev.start_date)) ?
curr :
prev
)
}

return enrollableRuns
.filter(run => run.start_date && moment(run.start_date).isBefore(now))
.reduce((prev, curr) =>
moment(curr.start_date).isBefore(moment(prev.start_date)) ? prev : curr
)
}
// no enrollable runs, then check for future runs
const futureRuns = courseRuns.filter(
run => run.start_date && moment(run.start_date).isSameOrAfter(now)
)
if (futureRuns.length > 0) {
return futureRuns.reduce((prev, curr) =>
moment(curr.start_date).isBefore(moment(prev.start_date)) ? curr : prev
)
}
return null
}
118 changes: 7 additions & 111 deletions frontend/public/src/lib/courseApi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ import type { LoggedInUser } from "../flow/authTypes"

describe("Course API", () => {
const past = moment().add(-10, "days").toISOString(),
farPast = moment().add(-50, "days").toISOString(),
farFarPast = moment().add(-100, "days").toISOString(),
future = moment().add(10, "days").toISOString(),
farFuture = moment().add(50, "days").toISOString(),
farFarFuture = moment().add(100, "days").toISOString(),
exampleUrl = "http://example.com"
let courseRun: CourseRunDetail, user: LoggedInUser

Expand Down Expand Up @@ -302,121 +299,20 @@ describe("Course API", () => {
assert.equal(result, null)
})

it("returns null if there aren't course runs in the course", () => {
it("returns null if next_run_id is not provided for the course", () => {
const testCourse = {
...makeCourseDetailWithRuns(),
courseruns: []
}
const result = getFirstRelevantRun(testCourse, [makeCourseRunDetail()])
assert.equal(result, null)
})
;[
["in order", true],
["out of order", false]
].forEach(([isInOrderDesc, isInOrder]) => {
it(`returns the first run that is in the future and runs are ${isInOrderDesc}`, () => {
const runs = [
{
...makeCourseRunDetail(),
start_date: future,
enrollment_start: future,
end_date: farFuture,
enrollment_end: farFuture
},
{
...makeCourseRunDetail(),
start_date: farFuture,
enrollment_start: farFuture,
end_date: farFarFuture,
enrollment_end: farFarFuture
}
]

if (!isInOrder) {
runs.reverse()
}

const testCourse = {
...makeCourseDetailWithRuns(),
next_run_id: null,
courseruns: runs
}

const result = getFirstRelevantRun(testCourse, runs)
assert.equal(result, isInOrder ? runs[0] : runs[1])
})
})
;[
["in order", true],
["out of order", false]
].forEach(([isInOrderDesc, isInOrder]) => {
it(`returns the first run that is in the future and runs are ${isInOrderDesc}`, () => {
const runs = [
{
...makeCourseRunDetail(),
start_date: future,
enrollment_start: future,
end_date: farFuture,
enrollment_end: farFuture
},
{
...makeCourseRunDetail(),
start_date: farPast,
enrollment_start: farPast,
end_date: past,
enrollment_end: past
}
]

if (!isInOrder) {
runs.reverse()
}

const testCourse = {
...makeCourseDetailWithRuns(),
next_run_id: null,
courseruns: runs
}

const result = getFirstRelevantRun(testCourse, runs)
assert.equal(result, isInOrder ? runs[0] : runs[1])
})
})
;[
["in order", true],
["out of order", false]
].forEach(([isInOrderDesc, isInOrder]) => {
it(`returns the most recent run that is in the past if all runs are done and runs are ${isInOrderDesc}`, () => {
const runs = [
{
...makeCourseRunDetail(),
start_date: farPast,
enrollment_start: farPast,
end_date: past,
enrollment_end: past
},
{
...makeCourseRunDetail(),
start_date: farFarPast,
enrollment_start: farFarPast,
end_date: farPast,
enrollment_end: farPast
}
]

if (!isInOrder) {
runs.reverse()
}

const testCourse = {
...makeCourseDetailWithRuns(),
next_run_id: null,
courseruns: runs
}

const result = getFirstRelevantRun(testCourse, runs)
assert.equal(result, isInOrder ? runs[0] : runs[1])
})
it("returns next_run_id for the course", () => {
const testCourse = makeCourseDetailWithRuns()
const courseRun = testCourse.courseruns[0]
testCourse.next_run_id = courseRun.id
const result = getFirstRelevantRun(testCourse, [courseRun])
assert.equal(result, courseRun)
})
})
})

0 comments on commit 11e500a

Please sign in to comment.