2023-07-12 22:51:32 +02:00
|
|
|
import { DateValue, parseAbsolute, parseDate } from "@internationalized/date";
|
2023-07-12 22:12:31 +02:00
|
|
|
import { vi } from "vitest";
|
|
|
|
|
import {
|
|
|
|
|
convertDateValueToString,
|
2023-07-12 22:51:32 +02:00
|
|
|
parseDateValue,
|
|
|
|
|
parseRangeDateValue,
|
2023-06-16 13:49:47 +02:00
|
|
|
} from ":/components/Forms/DatePicker/utils";
|
|
|
|
|
|
|
|
|
|
const expectDateToBeEqual = (
|
2023-07-12 22:51:32 +02:00
|
|
|
parsedDate: DateValue | undefined,
|
2023-06-16 13:49:47 +02:00
|
|
|
expectedYear: number,
|
|
|
|
|
expectedMonth: number,
|
2023-07-18 15:43:56 +02:00
|
|
|
expectedDay: number,
|
2023-06-16 13:49:47 +02:00
|
|
|
) => {
|
|
|
|
|
expect(parsedDate).not.eq(undefined);
|
2023-07-12 22:51:32 +02:00
|
|
|
expect(parsedDate?.year).eq(expectedYear);
|
|
|
|
|
expect(parsedDate?.month).eq(expectedMonth);
|
|
|
|
|
expect(parsedDate?.day).eq(expectedDay);
|
2023-06-16 13:49:47 +02:00
|
|
|
};
|
|
|
|
|
|
2023-07-12 22:12:31 +02:00
|
|
|
vi.mock("@internationalized/date", async () => {
|
|
|
|
|
const mod = await vi.importActual<typeof import("@internationalized/date")>(
|
|
|
|
|
"@internationalized/date",
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
...mod,
|
|
|
|
|
// Note: Restoring mocks will cause the function to return 'undefined'.
|
|
|
|
|
// Consider providing a default implementation to be restored instead.
|
|
|
|
|
getLocalTimeZone: vi.fn().mockReturnValue("Europe/Paris"),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-12 22:51:32 +02:00
|
|
|
describe("parseDateValue", () => {
|
|
|
|
|
it("parse a 'YYYY-MM-DDThh:mm:ssZ' date", () => {
|
|
|
|
|
const parsedDate = parseDateValue("2023-05-11T00:00:00.000Z");
|
|
|
|
|
expectDateToBeEqual(parsedDate, 2023, 5, 11);
|
|
|
|
|
expect(parsedDate?.hour).eq(0);
|
2023-06-16 13:49:47 +02:00
|
|
|
});
|
|
|
|
|
|
2023-07-12 22:51:32 +02:00
|
|
|
it("parse a 'YYYY-MM-DDThh:mm:ss±hh:mm' date", () => {
|
|
|
|
|
const parsedDate = parseDateValue("2023-05-11T00:00:00.000+00:00");
|
|
|
|
|
expectDateToBeEqual(parsedDate, 2023, 5, 11);
|
|
|
|
|
expect(parsedDate?.hour).eq(0);
|
2023-06-16 13:49:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.each([undefined, ""])("parse an empty or null date", (date) => {
|
2023-07-12 22:51:32 +02:00
|
|
|
const parsedDate = parseDateValue(date);
|
2023-06-16 13:49:47 +02:00
|
|
|
expect(parsedDate).eq(undefined);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
|
"35/04/2024",
|
2023-07-12 22:51:32 +02:00
|
|
|
"2023-05-11",
|
2023-06-16 13:49:47 +02:00
|
|
|
"11 janvier 20O2",
|
|
|
|
|
"22.04.2022",
|
|
|
|
|
"22-4-2022",
|
|
|
|
|
"2022-04-1T00:00:00-00:00",
|
|
|
|
|
"2022-04-01 T00:00:00-00:00",
|
2023-07-12 22:51:32 +02:00
|
|
|
"2022-04-01T00:00:00.000",
|
2023-06-16 13:49:47 +02:00
|
|
|
])("parse a wrong date", (wrongFormattedDate) => {
|
2023-07-12 22:51:32 +02:00
|
|
|
expect(() => parseDateValue(wrongFormattedDate)).toThrow(
|
2023-07-18 15:43:56 +02:00
|
|
|
"Invalid date format when initializing props on DatePicker component",
|
2023-06-16 13:49:47 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-12 22:51:32 +02:00
|
|
|
describe("parseRangeDateValue", () => {
|
|
|
|
|
it("parse a date range", () => {
|
|
|
|
|
const range = parseRangeDateValue([
|
|
|
|
|
"2023-03-22T00:00:00.000Z",
|
|
|
|
|
"2023-04-22T00:00:00.000Z",
|
|
|
|
|
]);
|
2023-06-16 13:49:47 +02:00
|
|
|
expectDateToBeEqual(range?.start, 2023, 3, 22);
|
|
|
|
|
expectDateToBeEqual(range?.end, 2023, 4, 22);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
|
["", "2023-03-22"],
|
|
|
|
|
["2023-03-22", ""],
|
|
|
|
|
])(
|
|
|
|
|
"parse a partially null or empty date range",
|
2023-07-12 22:51:32 +02:00
|
|
|
(start: string, end: string) => {
|
|
|
|
|
expect(parseRangeDateValue([start, end])).eq(undefined);
|
2023-07-18 15:43:56 +02:00
|
|
|
},
|
2023-06-16 13:49:47 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it("parse an undefined date range", () => {
|
2023-07-12 22:51:32 +02:00
|
|
|
expect(parseRangeDateValue(undefined)).eq(undefined);
|
2023-06-16 13:49:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("parse an inverted date range", () => {
|
|
|
|
|
// Utils function accepts start date superior to the end date
|
|
|
|
|
// However, DateRangePicker will trigger an error with the parsed range.
|
2023-07-12 22:51:32 +02:00
|
|
|
const range = parseRangeDateValue([
|
|
|
|
|
"2023-05-22T00:00:00.000Z",
|
|
|
|
|
"2023-04-22T00:00:00.000Z",
|
|
|
|
|
]);
|
2023-06-16 13:49:47 +02:00
|
|
|
expectDateToBeEqual(range?.start, 2023, 5, 22);
|
|
|
|
|
expectDateToBeEqual(range?.end, 2023, 4, 22);
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-07-12 22:12:31 +02:00
|
|
|
|
|
|
|
|
describe("convertDateValueToString", () => {
|
|
|
|
|
it("should return an empty string for null date", () => {
|
|
|
|
|
const date: DateValue | null = null;
|
|
|
|
|
const result = convertDateValueToString(date);
|
|
|
|
|
expect(result).toBe("");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return a UTC ISO 8601 string from a CalendarDate", async () => {
|
|
|
|
|
const date = parseDate("2023-05-25");
|
|
|
|
|
const result = convertDateValueToString(date);
|
|
|
|
|
expect(result).eq("2023-05-24T22:00:00.000Z");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return a UTC ISO 8601 string from a ZonedDateTime", async () => {
|
|
|
|
|
// Europe/Paris is the mocked local timezone.
|
|
|
|
|
const date = parseAbsolute("2023-05-25T00:00:00.000+02:00", "Europe/Paris");
|
|
|
|
|
const result = convertDateValueToString(date);
|
|
|
|
|
expect(result).eq("2023-05-24T22:00:00.000Z");
|
|
|
|
|
});
|
|
|
|
|
});
|