UNPKG

@blueprintjs/datetime

Version:

Components for interacting with dates and times

60 lines (44 loc) 2.38 kB
/* ! * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. */ import { format } from "date-fns"; import { describe, expect, it } from "@blueprintjs/test-commons/vitest"; import { OUT_OF_RANGE_MESSAGE } from "../components/dateConstants"; import { getFormattedDateString } from "./dateFormatProps"; import { Months } from "./months"; const formatDate = (date: Date) => format(date, "yyyy-MM-dd"); describe("DateFormatProps", () => { describe("getFormattedDateString", () => { it("should return an empty string for null date", () => { const testDate: Date | null = null; expect(getFormattedDateString(testDate, { formatDate })).toBe(""); }); it("should return an invalid date message for invalid date", () => { const invalidDate = new Date("invalid-date"); const invalidDateMessage = "INVALID"; expect(getFormattedDateString(invalidDate, { formatDate, invalidDateMessage })).toBe(invalidDateMessage); }); it("should format an in range date with default format", () => { const testDate = new Date(2025, Months.DECEMBER, 15); const minDate = new Date(2025, Months.DECEMBER, 1); const maxDate = new Date(2025, Months.DECEMBER, 31); expect(getFormattedDateString(testDate, { formatDate, maxDate, minDate })).toBe("2025-12-15"); }); it("should return out of range message for out of range date", () => { const testDate = new Date(2025, Months.DECEMBER, 31); const minDate = new Date(2025, Months.DECEMBER, 1); const maxDate = new Date(2025, Months.DECEMBER, 30); const outOfRangeMessage = OUT_OF_RANGE_MESSAGE; expect(getFormattedDateString(testDate, { formatDate, maxDate, minDate, outOfRangeMessage })).toBe( outOfRangeMessage, ); }); it("should format an in range date with ignoreRange set to true", () => { const testDate = new Date(2025, Months.DECEMBER, 31); const minDate = new Date(2025, Months.DECEMBER, 1); const maxDate = new Date(2025, Months.DECEMBER, 30); const ignoreRange = true; expect(getFormattedDateString(testDate, { formatDate, maxDate, minDate }, ignoreRange)).toBe("2025-12-31"); }); }); });