Skip to content

Commit

Permalink
feat: check for valid term date format (#327)
Browse files Browse the repository at this point in the history
In the Support Tools: New Learner Credit Plan, a validity check was added that
    the term start and end date is in the format "YYYY-MM-DD".

    ENT-6940
  • Loading branch information
emrosarioa authored Apr 20, 2023
1 parent 138fcc1 commit abec034
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Form } from '@edx/paragon';
import PROVISIONING_PAGE_TEXT from '../data/constants';
import useProvisioningContext from '../data/hooks';
import { selectProvisioningContext } from '../data/utils';
import { isValidDateString } from '../../../utils';

const ProvisioningFormTerm = () => {
const { TERM } = PROVISIONING_PAGE_TEXT.FORM;
Expand All @@ -12,10 +13,17 @@ const ProvisioningFormTerm = () => {

const handleDateChange = (e) => {
const eventTarget = e.target;
if (eventTarget.dataset.testid.includes('start')) {
return setStartDate(eventTarget.value);
const isStartDate = eventTarget.dataset.testid.includes('start');
if (isValidDateString(eventTarget.value)) {
if (isStartDate) {
return setStartDate(eventTarget.value);
}
return setEndDate(eventTarget.value);
}
return setEndDate(eventTarget.value);
if (isStartDate) {
return setStartDate('');
}
return setEndDate('');
};

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,14 @@ describe('ProvisioningFormTerm', () => {
expect(endDateInputValue).toEqual('2020-01-01');

expect(screen.getByText(TERM.VALIDITY)).toBeTruthy();

fireEvent.change(endDateInput, { target: { value: 'foo' } });
const invalidEndDateInputValue = endDateInput.getAttribute('value');
expect(invalidEndDateInputValue).toEqual('');
expect(startDateInput.getAttribute('value')).toEqual('2021-01-01');

fireEvent.change(startDateInput, { target: { value: '2020/09/08' } });
const invalidStartDateInputValue = startDateInput.getAttribute('value');
expect(invalidStartDateInputValue).toEqual('');
});
});
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@ export function extractParams(searchParams) {
.map(queryParams => queryParams.split('=')),
);
}

export const isValidDateString = (dateString) => moment(dateString, 'YYYY-MM-DD', true).isValid();
19 changes: 19 additions & 0 deletions src/utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isValidCourseID,
extractMessageTuple,
extractParams,
isValidDateString,
isWholeDollarAmount,
} from './index';

Expand Down Expand Up @@ -210,6 +211,24 @@ describe('Test Utils', () => {
expect(params.get('')).toEqual(undefined);
});
});

describe('isValidDate', () => {
it('returns true for valid date string in YYYY-MM-DD format', () => {
expect(isValidDateString('2022-03-24')).toBe(true);
expect(isValidDateString('1997-11-04')).toBe(true);
expect(isValidDateString('2022-01-01')).toBe(true);
});
it('returns false for invalid date string', () => {
expect(isValidDateString('2022-3-24')).toBe(false);
expect(isValidDateString('2022-04-3oT12:00:00Z')).toBe(false);
expect(isValidDateString('2022/01/01')).toBe(false);
expect(isValidDateString('foo')).toBe(false);
});
it('returns false for null or undefined input', () => {
expect(isValidDateString(null)).toBe(false);
expect(isValidDateString(undefined)).toBe(false);
});
});
});

describe('isWholeDollarAmount', () => {
Expand Down

0 comments on commit abec034

Please sign in to comment.