brackt/app/lib/__tests__/date-utils.test.ts

56 lines
2.1 KiB
TypeScript
Raw Normal View History

import { describe, it, expect } from "vitest";
import { localDateTimeToUtcIso } from "../date-utils";
describe("localDateTimeToUtcIso", () => {
it("returns null for empty string", () => {
expect(localDateTimeToUtcIso("")).toBeNull();
});
it("returns null for null", () => {
expect(localDateTimeToUtcIso(null)).toBeNull();
});
it("returns null for undefined", () => {
expect(localDateTimeToUtcIso(undefined)).toBeNull();
});
it("returns null for an invalid date string", () => {
expect(localDateTimeToUtcIso("not-a-date")).toBeNull();
});
it("converts a valid datetime-local value and round-trips correctly", () => {
// The input matches the datetime-local format "YYYY-MM-DDTHH:MM".
// Regardless of timezone, the resulting ISO string should parse back
// to the same moment in time.
const input = "2026-03-11T17:00";
const result = localDateTimeToUtcIso(input);
expect(result).not.toBeNull();
expect(new Date(result!).getTime()).toBe(new Date(input).getTime());
});
it("returns a string ending with 'Z' (UTC designator)", () => {
const result = localDateTimeToUtcIso("2026-03-11T10:00");
expect(result).not.toBeNull();
expect(result!.endsWith("Z")).toBe(true);
});
it("returns a full ISO-8601 string with milliseconds", () => {
const result = localDateTimeToUtcIso("2026-06-15T08:30");
expect(result).not.toBeNull();
// toISOString() always produces "YYYY-MM-DDTHH:MM:SS.mmmZ"
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
});
it("the returned ISO string is incompatible with datetime-local input format", () => {
// This documents WHY we need a hidden input instead of setting the
// datetime-local input's value directly. datetime-local only accepts
// "YYYY-MM-DDTHH:MM" — a 'Z' suffix makes the value invalid and the
// browser silently clears the field.
const result = localDateTimeToUtcIso("2026-03-11T10:00");
expect(result).not.toBeNull();
expect(result).toContain("Z");
// The value does NOT match the datetime-local format (no Z allowed)
expect(result).not.toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/);
});
});