Skip to content

Commit

Permalink
fix: ensure consistency around start dates on course run cards in Cou…
Browse files Browse the repository at this point in the history
…rse page (#1203)
  • Loading branch information
adamstankiewicz authored Oct 3, 2024
1 parent ac21d02 commit 3f6cf6d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { renderHook } from '@testing-library/react-hooks';
import MockDate from 'mockdate';
import dayjs from 'dayjs';
import '@testing-library/jest-dom/extend-expect';

import { hasTimeToComplete } from '../../../../data/utils';

import { MOCK_COURSE_RUN_START } from './constants';
import useCourseRunCardHeading from '../useCourseRunCardHeading';
import { COURSE_PACING_MAP } from '../../../../data/constants';
import { DATE_FORMAT } from '../../constants';

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

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

0 comments on commit 3f6cf6d

Please sign in to comment.