🩹(react) harmonize date picker components output

By introducing this utility function, we ensure that any output value
is converted back to an ISO 8601 date and time string in UTC timezone.
Overall, it harmonize and factorize the way we output
values from date picker components.
This commit is contained in:
Lebaud Antoine
2023-07-12 22:12:31 +02:00
committed by aleb_the_flash
parent bae7392fe1
commit 8cf8e1eba2
8 changed files with 109 additions and 19 deletions

View File

@@ -1,5 +1,12 @@
import { CalendarDate, DateValue, parseDate } from "@internationalized/date";
import {
CalendarDate,
DateValue,
parseAbsolute,
parseDate,
} from "@internationalized/date";
import { vi } from "vitest";
import {
convertDateValueToString,
parseCalendarDate,
parseRangeCalendarDate,
} from ":/components/Forms/DatePicker/utils";
@@ -17,6 +24,18 @@ const expectDateToBeEqual = (
expect(parsedDate?.day === expectedDay);
};
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"),
};
});
describe("parseCalendarDate", () => {
it.each([
[2023, 4, 12],
@@ -161,3 +180,24 @@ describe("parseRangeCalendarDate", () => {
expectDateToBeEqual(range?.end, 2023, 4, 22);
});
});
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");
});
});