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

feat: Enhance date validation logic and add tests for timePrecision in DatePickerWidget2 #37218

Merged
merged 7 commits into from
Nov 7, 2024
65 changes: 42 additions & 23 deletions app/client/src/widgets/DatePickerWidget2/widget/derived.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
/* eslint-disable @typescript-eslint/no-unused-vars*/
export default {
isValidDate: (props, moment, _) => {
const minDate = new Date(props.minDate);
const maxDate = new Date(props.maxDate);
const selectedDate =
props.selectedDate !== ""
? moment(new Date(props.selectedDate))
: props.selectedDate;
let dateValid = true;

if (!!props.minDate && !!props.maxDate) {
dateValid = !!selectedDate
? selectedDate.isBetween(minDate, maxDate)
: !props.isRequired;
} else if (!!props.minDate) {
dateValid = !!selectedDate
? selectedDate.isAfter(minDate)
: !props.isRequired;
} else if (!!props.maxDate) {
dateValid = !!selectedDate
? selectedDate.isBefore(maxDate)
: !props.isRequired;
} else {
dateValid = props.isRequired ? !!selectedDate : true;
if (!props.selectedDate && !props.isRequired) {
return true;
}

return dateValid;
const minDate = props.minDate ? new Date(props.minDate) : null;
const maxDate = props.maxDate ? new Date(props.maxDate) : null;
const selectedDate = props.selectedDate
? moment(new Date(props.selectedDate))
: props.selectedDate;

if (!selectedDate) {
return !props.isRequired;
}

let granularity,
inclusivity = "[]";

switch (props.timePrecision) {
case "None":
granularity = "day";
break;
case "second":
case "minute":
case "millisecond":
granularity = props.timePrecision;
break;
default:
granularity = undefined;
inclusivity = undefined;
}

if (minDate && maxDate) {
return selectedDate.isBetween(minDate, maxDate, granularity, inclusivity);
}

if (minDate) {
return selectedDate.isSameOrAfter(minDate, granularity);
}

if (maxDate) {
return selectedDate.isSameOrBefore(maxDate, granularity);
}

return true;
},
//
};
120 changes: 120 additions & 0 deletions app/client/src/widgets/DatePickerWidget2/widget/derived.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,124 @@ describe("Validates Derived Properties", () => {

expect(result).toStrictEqual(false);
});
describe("timePrecision", () => {
it("should fail when selectedDate minute is outside bounds", () => {
const { isValidDate } = derivedProperty;
const input = {
isRequired: true,
maxDate: "2021-12-01T05:49:00.000Z",
minDate: "2021-12-01T05:48:00.000Z",
selectedDate: "2021-12-01T05:50:00.000Z",
timePrecision: "minute",
};

let result = isValidDate(input, moment, _);

expect(result).toStrictEqual(false);
});

it("timePrecision: minute -> date is valid even if selectedDate is over by seconds", () => {
const { isValidDate } = derivedProperty;
const input = {
isRequired: true,
maxDate: "2121-12-31T18:29:00.000Z",
minDate: "2021-12-01T05:49:28.753Z",
selectedDate: "2021-12-01T05:49:24.753Z",
timePrecision: "minute",
};

let result = isValidDate(input, moment, _);

expect(result).toStrictEqual(true);
});

it("timePrecision: second -> date is valid even if selectedDate is over by milliseconds", () => {
const { isValidDate } = derivedProperty;
const input = {
isRequired: true,
maxDate: "2121-12-31T18:29:00.000Z",
minDate: "2021-12-01T05:49:24.753Z",
selectedDate: "2021-12-01T05:49:24.751Z",
timePrecision: "second",
};

let result = isValidDate(input, moment, _);

expect(result).toStrictEqual(true);
});

it("timePrecision: millisecond", () => {
const { isValidDate } = derivedProperty;
const input = {
isRequired: true,
maxDate: "2121-12-31T18:29:00.000Z",
minDate: "2021-12-01T05:49:24.752Z",
selectedDate: "2021-12-01T05:49:24.753Z",
timePrecision: "millisecond",
};

let result = isValidDate(input, moment, _);

expect(result).toStrictEqual(true);
});

describe("timePrecision: None", () => {
it("date is same as minDate", () => {
const { isValidDate } = derivedProperty;
const input = {
isRequired: true,
maxDate: "2121-12-31T18:29:00.000Z",
minDate: "2021-12-01T00:00:00.000Z",
selectedDate: "2021-12-01T00:00:00.000Z",
timePrecision: "None",
};

let result = isValidDate(input, moment, _);

expect(result).toStrictEqual(true);
});
it("date is same as maxDate", () => {
const { isValidDate } = derivedProperty;
const input = {
isRequired: true,
maxDate: "2021-12-01T00:00:00.000Z",
minDate: "1991-12-31T18:29:00.000Z",
selectedDate: "2021-12-01T00:00:00.000Z",
timePrecision: "None",
};

let result = isValidDate(input, moment, _);

expect(result).toStrictEqual(true);
});
it("date is between minDate and maxDate", () => {
const { isValidDate } = derivedProperty;
const input = {
isRequired: true,
maxDate: "2121-12-31T18:29:00.000Z",
minDate: "1920-12-31T18:30:00.000Z",
selectedDate: "2021-12-01T05:49:24.753Z",
timePrecision: "None",
};

let result = isValidDate(input, moment, _);

expect(result).toStrictEqual(true);
});
it("date is out of bounds", () => {
const { isValidDate } = derivedProperty;
const input = {
isRequired: true,
maxDate: "2021-12-31T18:29:00.000Z",
minDate: "1920-12-31T18:30:00.000Z",
selectedDate: "2022-12-01T05:49:24.753Z",
timePrecision: "None",
};

let result = isValidDate(input, moment, _);

expect(result).toStrictEqual(false);
});
});
});
});
Loading