refactor: simplify calendar and form state
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { isValidPeriod, moscowDate } from "./calendar";
|
||||
import { calendarDate } from "./calendar.schema";
|
||||
|
||||
describe("calendar dates", () => {
|
||||
it.each([
|
||||
["0000-02-29", true],
|
||||
["0001-01-01", true],
|
||||
["0099-12-31", true],
|
||||
["1900-02-29", false],
|
||||
["2000-02-29", true],
|
||||
["2024-02-29", true],
|
||||
["2026-02-29", false],
|
||||
["2026-99-99", false],
|
||||
["2026-1-01", false],
|
||||
["", false],
|
||||
])("validates %s without Date normalization", (value, valid) => {
|
||||
expect(calendarDate.safeParse(value).success).toBe(valid);
|
||||
});
|
||||
|
||||
it("accepts open or single-day periods and rejects reversed ranges", () => {
|
||||
expect(isValidPeriod(null, null)).toBe(true);
|
||||
expect(isValidPeriod(null, "2026-09-01")).toBe(true);
|
||||
expect(isValidPeriod("2026-09-01", null)).toBe(true);
|
||||
expect(isValidPeriod("2026-09-01", "2026-09-01")).toBe(true);
|
||||
expect(isValidPeriod("2026-09-02", "2026-09-01")).toBe(false);
|
||||
expect(isValidPeriod("", "2026-09-01")).toBe(false);
|
||||
});
|
||||
|
||||
it("changes the calendar day at Moscow midnight across a year boundary", () => {
|
||||
expect(moscowDate(new Date("2026-12-31T20:59:59Z"))).toBe("2026-12-31");
|
||||
expect(moscowDate(new Date("2026-12-31T21:00:00Z"))).toBe("2027-01-01");
|
||||
});
|
||||
});
|
||||
@@ -9,4 +9,18 @@ function isValidPeriod(from: null | string, to: null | string) {
|
||||
);
|
||||
}
|
||||
|
||||
export { isValidPeriod };
|
||||
function moscowDate(value: Date): string {
|
||||
const parts = new Intl.DateTimeFormat("en-CA", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
timeZone: "Europe/Moscow",
|
||||
year: "numeric",
|
||||
}).formatToParts(value);
|
||||
|
||||
const part = (type: string) =>
|
||||
parts.find(item => item.type === type)?.value;
|
||||
|
||||
return `${part("year")}-${part("month")}-${part("day")}`;
|
||||
}
|
||||
|
||||
export { isValidPeriod, moscowDate };
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export { isValidPeriod } from "./calendar";
|
||||
export { isValidPeriod, moscowDate } from "./calendar";
|
||||
export { calendarDate } from "./calendar.schema";
|
||||
export {
|
||||
formatChartValue,
|
||||
|
||||
Reference in New Issue
Block a user