I have a function that check whether the param is the same with or later than today, in my function I used new Date() like so
import moment from "moment";
const validateDate = ({ date }) => {
return moment(date, "DD-MM-YYYY").isSameOrAfter(
moment(new Date(), "DD-MM-YYYY", "days")
);
};
export default validateDate;
My test will be like so:
import validateDate from "./index";
it("is same or after today", () => {
expect(validateDate({ date: "16-05-2019" })).toBeTruthy();
});
The problem is that the test will fail on 17-05-2019. How to solve this issue?
I tried this idea but not sure whether it's fine or not.
const validateDate = ({ date, today = new Date() }) => {
return moment(date, "DD-MM-YYYY").isSameOrAfter(
moment(today, "DD-MM-YYYY", "days")
);
};
My test:
expect(validateDate({ date: "16-05-2019" }, today: new Date())).toBeTruthy();