Skip to content

Commit

Permalink
fix: end date error when certificate row not shown (openedx#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinAoki authored and Ian2012 committed Nov 15, 2023
1 parent b5d9c46 commit 5738873
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/schedule-and-details/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { validateScheduleAndDetails, updateWithDefaultValues } from './utils';
const useSaveValuesPrompt = (
courseId,
updateDataQuery,
canShowCertificateAvailableDateField,
initialEditedData = {},
) => {
const intl = useIntl();
Expand All @@ -28,7 +29,7 @@ const useSaveValuesPrompt = (
}, [initialEditedData]);

useEffect(() => {
const errors = validateScheduleAndDetails(editedValues, intl);
const errors = validateScheduleAndDetails(editedValues, canShowCertificateAvailableDateField, intl);
setErrorFields(errors);
}, [editedValues]);

Expand Down
37 changes: 21 additions & 16 deletions src/schedule-and-details/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,6 @@ const ScheduleAndDetails = ({ intl, courseId }) => {
const course = useModel('courseDetails', courseId);
document.title = getPageHeadTitle(course?.name, intl.formatMessage(messages.headingTitle));

const {
errorFields,
savingStatus,
editedValues,
isQueryPending,
isEditableState,
showModifiedAlert,
showSuccessfulAlert,
dispatch,
handleResetValues,
handleValuesChange,
handleUpdateValues,
handleQueryProcessing,
handleInternetConnectionFailed,
} = useSaveValuesPrompt(courseId, updateCourseDetailsQuery, courseDetails);

const {
platformName,
isCreditCourse,
Expand All @@ -91,6 +75,27 @@ const ScheduleAndDetails = ({ intl, courseId }) => {
canShowCertificateAvailableDateField,
} = courseSettings;

const {
errorFields,
savingStatus,
editedValues,
isQueryPending,
isEditableState,
showModifiedAlert,
showSuccessfulAlert,
dispatch,
handleResetValues,
handleValuesChange,
handleUpdateValues,
handleQueryProcessing,
handleInternetConnectionFailed,
} = useSaveValuesPrompt(
courseId,
updateCourseDetailsQuery,
canShowCertificateAvailableDateField,
courseDetails,
);

const {
org,
courseId: courseNumber,
Expand Down
4 changes: 2 additions & 2 deletions src/schedule-and-details/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const isDateBeforeOrEqual = (
return new Date(dateFormer) <= new Date(dateLatter);
};

const validateScheduleAndDetails = (courseDetails, intl) => {
const validateScheduleAndDetails = (courseDetails, canShowCertificateAvailableDate, intl) => {
const errors = {};
const {
endDate,
Expand All @@ -34,7 +34,7 @@ const validateScheduleAndDetails = (courseDetails, intl) => {
errors.startDate = intl.formatMessage(messages.errorMessage7);
}

if (isDateBeforeOrEqual(certificateAvailableDate, endDate)) {
if (isDateBeforeOrEqual(certificateAvailableDate, endDate) && canShowCertificateAvailableDate) {
errors.certificateAvailableDate = intl.formatMessage(messages.errorMessage6);
}

Expand Down
294 changes: 294 additions & 0 deletions src/schedule-and-details/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
import { CERTIFICATE_DISPLAY_BEHAVIOR } from './schedule-section/certificate-display-row';
import { validateScheduleAndDetails } from './utils';

const intl = { formatMessage: (message) => message };

describe('validateScheduleAndDetails', () => {
describe('startDate', () => {
it('should return without start date errors', () => {
const errors = validateScheduleAndDetails({ startDate: '01/01/1998' }, false, intl);
const hasStartDateError = Object.keys(errors).includes('startDate');

expect(hasStartDateError).toBeFalsy();
});

it('should return with start date error', () => {
const errors = validateScheduleAndDetails({ startDate: null }, false, intl);
const hasStartDateError = Object.keys(errors).includes('startDate');

expect(hasStartDateError).toBeTruthy();
});
});

describe('endDate', () => {
it('should return without end date errors', () => {
const errors = validateScheduleAndDetails(
{ startDate: '01/01/1998', endDate: '01/01/1999' },
false,
intl,
);
const hasEndDateError = Object.keys(errors).includes('endDate');

expect(hasEndDateError).toBeFalsy();
});

it('should return with end date error', () => {
const errors = validateScheduleAndDetails(
{ startDate: '01/01/1998', endDate: '01/01/1997' },
false,
intl,
);
const hasEndDateError = Object.keys(errors).includes('endDate');

expect(hasEndDateError).toBeTruthy();
});
});

describe('enrollmentStart', () => {
it('should return without enrollment start errors when start dates are equal', () => {
const errors = validateScheduleAndDetails(
{ startDate: '01/01/1998', enrollmentStart: '01/01/1998' },
false,
intl,
);
const hasEnrollmentStartError = Object.keys(errors).includes('enrollmentStart');

expect(hasEnrollmentStartError).toBeFalsy();
});

it('should return without enrollment start error when enrollment start is before course start', () => {
const errors = validateScheduleAndDetails(
{ startDate: '01/01/1999', enrollmentStart: '01/01/1998' },
false,
intl,
);
const hasEnrollmentStartError = Object.keys(errors).includes('enrollmentStart');

expect(hasEnrollmentStartError).toBeFalsy();
});

it('should return without enrollment start error when enrollment start is before enrollment end', () => {
const errors = validateScheduleAndDetails(
{ enrollmentEnd: '01/01/1999', enrollmentStart: '01/01/1998' },
false,
intl,
);
const hasEnrollmentStartError = Object.keys(errors).includes('enrollmentStart');

expect(hasEnrollmentStartError).toBeFalsy();
});

it('should return with enrollment start error when enrolllments starts after course start', () => {
const errors = validateScheduleAndDetails(
{ startDate: '01/01/1998', enrollmentStart: '01/02/1998' },
false,
intl,
);
const hasEnrollmentStartError = Object.keys(errors).includes('enrollmentStart');

expect(hasEnrollmentStartError).toBeTruthy();
});

it('should return with enrollment start error when enrolllments starts after enrollment end', () => {
const errors = validateScheduleAndDetails(
{ enrollmentEnd: '01/01/1998', enrollmentStart: '01/02/1998' },
false,
intl,
);
const hasEnrollmentStartError = Object.keys(errors).includes('enrollmentStart');

expect(hasEnrollmentStartError).toBeTruthy();
});
});

describe('enrollmentEnd', () => {
it('should return without enrollment start errors when end dates are equal', () => {
const errors = validateScheduleAndDetails(
{ enrollmentEnd: '01/01/1998', endDate: '01/01/1999' },
false,
intl,
);
const hasEnrollmentEndError = Object.keys(errors).includes('enrollmentEnd');

expect(hasEnrollmentEndError).toBeFalsy();
});

it('should return without enrollment start error when enrollment end is before course end', () => {
const errors = validateScheduleAndDetails(
{ enrollmentEnd: '01/01/1998', endDate: '01/01/1999' },
false,
intl,
);
const hasEnrollmentEndError = Object.keys(errors).includes('enrollmentEnd');

expect(hasEnrollmentEndError).toBeFalsy();
});

it('should return with enrollment date error', () => {
const errors = validateScheduleAndDetails(
{ enrollmentEnd: '01/01/1998', endDate: '01/01/1997' },
false,
intl,
);
const hasEnrollmentEndError = Object.keys(errors).includes('enrollmentEnd');

expect(hasEnrollmentEndError).toBeTruthy();
});
});

describe('certificateAvailableDate', () => {
describe('with canShowCertificateAvailableDate false', () => {
it('should return without certificate available errors when dates are vaild', () => {
const errors = validateScheduleAndDetails(
{ certificateAvailableDate: '01/01/1999', endDate: '01/01/1998' },
false,
intl,
);
const hasCertificateAvailableError = Object.keys(errors).includes('certificateAvailableDate');

expect(hasCertificateAvailableError).toBeFalsy();
});

it('should return without certificate available errors when dates are invaild', () => {
const errors = validateScheduleAndDetails(
{ certificateAvailableDate: '01/01/1997', endDate: '01/01/1998' },
false,
intl,
);
const hasCertificateAvailableError = Object.keys(errors).includes('certificateAvailableDate');

expect(hasCertificateAvailableError).toBeFalsy();
});
});

describe('with canShowCertificateAvailableDate true', () => {
it('should return without certificate available errors when dates are vaild', () => {
const errors = validateScheduleAndDetails(
{ certificateAvailableDate: '01/01/1999', endDate: '01/01/1998' },
true,
intl,
);
const hasCertificateAvailableError = Object.keys(errors).includes('certificateAvailableDate');

expect(hasCertificateAvailableError).toBeFalsy();
});

it('should return with certificate available error', () => {
const errors = validateScheduleAndDetails(
{ certificateAvailableDate: '01/01/1997', endDate: '01/01/1998' },
true,
intl,
);
const hasCertificateAvailableError = Object.keys(errors).includes('certificateAvailableDate');

expect(hasCertificateAvailableError).toBeTruthy();
});
});

describe('with certificatesDisplayBehavior equal to end_with_date', () => {
it('should return without certificate available errors when date has a value', () => {
const errors = validateScheduleAndDetails(
{
certificateAvailableDate: '01/01/1999',
certificatesDisplayBehavior: CERTIFICATE_DISPLAY_BEHAVIOR.endWithDate,
},
true,
intl,
);
const hasCertificateAvailableError = Object.keys(errors).includes('certificateAvailableDate');

expect(hasCertificateAvailableError).toBeFalsy();
});

it('should return with certificate available errors when date is empty', () => {
const errors = validateScheduleAndDetails(
{
certificateAvailableDate: '',
certificatesDisplayBehavior: CERTIFICATE_DISPLAY_BEHAVIOR.endWithDate,
},
true,
intl,
);
const hasCertificateAvailableError = Object.keys(errors).includes('certificateAvailableDate');

expect(hasCertificateAvailableError).toBeTruthy();
});
});

describe('with canShowCertificateAvailableDate not equal to end_with_date', () => {
it('should return without certificate available errors when date is empty', () => {
const errors = validateScheduleAndDetails(
{
certificateAvailableDate: '',
certificatesDisplayBehavior: CERTIFICATE_DISPLAY_BEHAVIOR.end,
},
true,
intl,
);
const hasCertificateAvailableError = Object.keys(errors).includes('certificateAvailableDate');

expect(hasCertificateAvailableError).toBeFalsy();
});

it('should return without certificate available errors when date is empty', () => {
const errors = validateScheduleAndDetails(
{
certificateAvailableDate: '',
certificatesDisplayBehavior: CERTIFICATE_DISPLAY_BEHAVIOR.earlyNoInfo,
},
true,
intl,
);
const hasCertificateAvailableError = Object.keys(errors).includes('certificateAvailableDate');

expect(hasCertificateAvailableError).toBeFalsy();
});
});
});

describe('entranceExamMinimumScore', () => {
it('should return without exam minimum score errors', () => {
const errors = validateScheduleAndDetails(
{ entranceExamMinimumScorePct: '25' },
false,
intl,
);
const hasExamMinimumScoreError = Object.keys(errors).includes('entranceExamMinimumScorePct');

expect(hasExamMinimumScoreError).toBeFalsy();
});

it('should return with exam minimum score error with negative value', () => {
const errors = validateScheduleAndDetails(
{ entranceExamMinimumScorePct: '-1' },
false,
intl,
);
const hasExamMinimumScoreError = Object.keys(errors).includes('entranceExamMinimumScorePct');

expect(hasExamMinimumScoreError).toBeTruthy();
});

it('should return with exam minimum score error with value greater than 100', () => {
const errors = validateScheduleAndDetails(
{ entranceExamMinimumScorePct: '230' },
false,
intl,
);
const hasExamMinimumScoreError = Object.keys(errors).includes('entranceExamMinimumScorePct');

expect(hasExamMinimumScoreError).toBeTruthy();
});

it('should return with exam minimum score error with non-numerical value', () => {
const errors = validateScheduleAndDetails(
{ entranceExamMinimumScorePct: 'test' },
false,
intl,
);
const hasExamMinimumScoreError = Object.keys(errors).includes('entranceExamMinimumScorePct');

expect(hasExamMinimumScoreError).toBeTruthy();
});
});
});

0 comments on commit 5738873

Please sign in to comment.