Fix Schedules Form date validation same day different time scenario (#13062)

* Format datetime and convert to ms to compare which date is larger.

* Add supporting unit test.
This commit is contained in:
kialam
2022-10-28 15:59:54 -07:00
committed by GitHub
parent ed318ea784
commit e40824bded
2 changed files with 38 additions and 2 deletions

View File

@@ -416,8 +416,14 @@ function ScheduleForm({
if (options.end === 'onDate') { if (options.end === 'onDate') {
if ( if (
DateTime.fromISO(values.startDate) >= DateTime.fromFormat(
DateTime.fromISO(options.endDate) `${values.startDate} ${values.startTime}`,
'yyyy-LL-dd h:mm a'
).toMillis() >=
DateTime.fromFormat(
`${options.endDate} ${options.endTime}`,
'yyyy-LL-dd h:mm a'
).toMillis()
) { ) {
freqErrors.endDate = t`Please select an end date/time that comes after the start date/time.`; freqErrors.endDate = t`Please select an end date/time that comes after the start date/time.`;
} }

View File

@@ -900,6 +900,36 @@ describe('<ScheduleForm />', () => {
); );
}); });
test('should create schedule with the same start and end date provided that the end date is at a later time', async () => {
const today = DateTime.now().toFormat('yyyy-LL-dd');
const laterTime = DateTime.now().plus({ hours: 1 }).toFormat('h:mm a');
await act(async () => {
wrapper.find('DatePicker[aria-label="End date"]').prop('onChange')(
today,
new Date(today)
);
});
wrapper.update();
expect(
wrapper
.find('FormGroup[data-cy="schedule-End date/time"]')
.prop('helperTextInvalid')
).toBe(
'Please select an end date/time that comes after the start date/time.'
);
await act(async () => {
wrapper.find('TimePicker[aria-label="End time"]').prop('onChange')(
laterTime
);
});
wrapper.update();
expect(
wrapper
.find('FormGroup[data-cy="schedule-End date/time"]')
.prop('helperTextInvalid')
).toBe(undefined);
});
test('error shown when on day number is not between 1 and 31', async () => { test('error shown when on day number is not between 1 and 31', async () => {
await act(async () => { await act(async () => {
wrapper.find('FrequencySelect#schedule-frequency').invoke('onChange')([ wrapper.find('FrequencySelect#schedule-frequency').invoke('onChange')([