69 lines
2.6 KiB
TypeScript
69 lines
2.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { describeTimezone, getLocationContext, utcOffsetLabel, weekdayName } from "../lib/location";
|
|
|
|
describe("describeTimezone", () => {
|
|
it("splits a simple zone into region and city", () => {
|
|
expect(describeTimezone("Asia/Manila")).toEqual({ region: "Asia", city: "Manila" });
|
|
});
|
|
|
|
it("handles zones with more than two segments", () => {
|
|
expect(describeTimezone("America/Argentina/Salta")).toEqual({ region: "America", city: "Salta" });
|
|
});
|
|
|
|
it("underscores become spaces", () => {
|
|
expect(describeTimezone("America/New_York")).toEqual({ region: "America", city: "New York" });
|
|
});
|
|
|
|
it("returns nothing for a bare zone like UTC", () => {
|
|
expect(describeTimezone("UTC")).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("utcOffsetLabel", () => {
|
|
it("renders a GMT offset for a fixed zone", () => {
|
|
const date = new Date("2026-01-02T03:04:05Z");
|
|
expect(utcOffsetLabel(date, "Asia/Manila")).toContain("+08:00");
|
|
});
|
|
|
|
it("reflects daylight saving rather than a fixed offset", () => {
|
|
const winter = utcOffsetLabel(new Date("2026-01-15T12:00:00Z"), "America/New_York");
|
|
const summer = utcOffsetLabel(new Date("2026-07-15T12:00:00Z"), "America/New_York");
|
|
|
|
expect(winter).not.toBe(summer);
|
|
expect(winter).toContain("-05:00");
|
|
expect(summer).toContain("-04:00");
|
|
});
|
|
});
|
|
|
|
describe("weekdayName", () => {
|
|
it("names the weekday in the requested zone", () => {
|
|
// 2026-01-02T03:04:05Z is a Friday in UTC.
|
|
expect(weekdayName(new Date("2026-01-02T03:04:05Z"), "UTC")).toBe("Friday");
|
|
});
|
|
|
|
it("respects the zone when the local date differs from UTC", () => {
|
|
// 2026-01-02T23:30Z is still Friday in UTC but already Saturday in Manila.
|
|
const late = new Date("2026-01-02T23:30:00Z");
|
|
expect(weekdayName(late, "UTC")).toBe("Friday");
|
|
expect(weekdayName(late, "Asia/Manila")).toBe("Saturday");
|
|
});
|
|
});
|
|
|
|
describe("getLocationContext", () => {
|
|
it("reports the requested zone with its parts", () => {
|
|
const context = getLocationContext(new Date("2026-01-02T03:04:05Z"), "Asia/Manila");
|
|
|
|
expect(context.timezone).toBe("Asia/Manila");
|
|
expect(context.city).toBe("Manila");
|
|
expect(context.region).toBe("Asia");
|
|
expect(context.utcOffset).toContain("+08:00");
|
|
expect(context.locale.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("still returns a usable context for an unknown zone", () => {
|
|
const context = getLocationContext(new Date("2026-01-02T03:04:05Z"), "Not/AReal_Zone");
|
|
expect(context.timezone).toBe("Not/AReal_Zone");
|
|
expect(context.utcOffset).toBe("UTC");
|
|
});
|
|
});
|