Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure consistency around start dates on course run cards in Course page #1203

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -8,6 +8,8 @@
import { MOCK_COURSE_RUN_START } from './constants';
import useCourseRunCardHeading from '../useCourseRunCardHeading';
import { COURSE_PACING_MAP } from '../../../../data/constants';
import dayjs from 'dayjs';

Check failure on line 11 in src/components/course/course-header/data/hooks/tests/useCourseRunCardHeading.test.jsx

View workflow job for this annotation

GitHub Actions / tests

`dayjs` import should occur before import of `../../../../data/utils`
import { DATE_FORMAT } from '../../constants';

jest.mock('../../../../data/utils', () => ({
...jest.requireActual('../../../../data/utils'),
Expand Down Expand Up @@ -39,32 +41,58 @@
});

it.each([
{ hasTimeToCompleteOverride: true },
{ hasTimeToCompleteOverride: false },
])('handles current, self-paced, unenrolled course run (%s)', ({ hasTimeToCompleteOverride }) => {
// test case: really old start date with time to complete
{
hasTimeToCompleteOverride: true,
startDate: dayjs(MOCK_COURSE_RUN_START).toISOString(),
expectedFormattedStartDate: `Starts ${dayjs().format(DATE_FORMAT)}`,
},
// test case: really old start date without time to complete
{
hasTimeToCompleteOverride: false,
startDate: dayjs(MOCK_COURSE_RUN_START).toISOString(),
expectedFormattedStartDate: `Starts ${dayjs().format(DATE_FORMAT)}`,
},
// test case: recent start date with time to complete
{
hasTimeToCompleteOverride: true,
startDate: dayjs().subtract(5, 'day').toISOString(),
expectedFormattedStartDate: `Starts ${dayjs().format(DATE_FORMAT)}`,
},
// test case: recent start date without time to complete
{
hasTimeToCompleteOverride: false,
startDate: dayjs().subtract(5, 'day').toISOString(),
expectedFormattedStartDate: `Started ${dayjs().subtract(5, 'day').format(DATE_FORMAT)}`,
},
])('handles current, self-paced, unenrolled course run (%s)', ({
hasTimeToCompleteOverride,
startDate,
expectedFormattedStartDate,
}) => {
hasTimeToComplete.mockReturnValue(hasTimeToCompleteOverride);

// mock current date
MockDate.set(new Date('2023-05-20T12:00:00Z'));
MockDate.set(dayjs().toDate());

const { result } = renderHook(
() => useCourseRunCardHeading({
isCourseRunCurrent: true,
isUserEnrolled: false,
courseRun: {
pacingType: COURSE_PACING_MAP.SELF_PACED,
start: MOCK_COURSE_RUN_START,
start: startDate,
},
}),
{ wrapper },
);

if (hasTimeToCompleteOverride) {
// assert shown start date matches the above mocked current date
expect(result.current).toEqual('Starts May 20');
expect(result.current).toEqual(expectedFormattedStartDate);
} else {
// assert shown start date matches the start date of the course run
expect(result.current).toEqual('Started Apr 20');
expect(result.current).toEqual(expectedFormattedStartDate);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import dayjs from 'dayjs';
import { defineMessages, useIntl } from '@edx/frontend-platform/i18n';

import { getCourseStartDate, hasTimeToComplete, isCourseSelfPaced } from '../../../data/utils';
import {
getCourseStartDate,
hasTimeToComplete,
isCourseSelfPaced,
isWithinMinimumStartDateThreshold,
} from '../../../data/utils';
import { DATE_FORMAT } from '../constants';

const messages = defineMessages({
Expand Down Expand Up @@ -48,7 +53,7 @@ const useCourseRunCardHeading = ({
return intl.formatMessage(messages.courseStarted);
}
if (isCourseSelfPaced(courseRun.pacingType)) {
if (hasTimeToComplete(courseRun)) {
if (hasTimeToComplete(courseRun) || isWithinMinimumStartDateThreshold(courseRun)) {
// always today's date (incentives enrollment)
return intl.formatMessage(messages.courseStartDate, {
startDate: dayjs().format(DATE_FORMAT),
Expand Down
2 changes: 1 addition & 1 deletion src/components/course/data/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function isCourseInstructorPaced(pacingType) {
return [COURSE_PACING_MAP.INSTRUCTOR_PACED, COURSE_PACING_MAP.INSTRUCTOR].includes(pacingType);
}

const isWithinMinimumStartDateThreshold = ({ start }) => dayjs(start).isBefore(dayjs().subtract(START_DATE_DEFAULT_TO_TODAY_THRESHOLD_DAYS, 'days'));
export const isWithinMinimumStartDateThreshold = ({ start }) => dayjs(start).isBefore(dayjs().subtract(START_DATE_DEFAULT_TO_TODAY_THRESHOLD_DAYS, 'days'));

/**
* If the start date of the course is before today offset by the START_DATE_DEFAULT_TO_TODAY_THRESHOLD_DAYS
Expand Down
Loading