import { dayjs } from "../index"; describe("dayjs", () => { describe("basic functionality", () => { it("should be defined and be a function", () => { expect(dayjs).toBeDefined(); expect(typeof dayjs).toBe("function"); }); it("should create dayjs object from date string", () => { const date = dayjs("2023-01-01"); expect(date.isValid()).toBe(true); expect(date.year()).toBe(2023); expect(date.month()).toBe(0); // January is 0 expect(date.date()).toBe(1); }); it("should create dayjs object from timestamp", () => { const timestamp = 1672531200000; // 2023-01-01 00:00:00 UTC const date = dayjs(timestamp); expect(date.isValid()).toBe(true); expect(date.valueOf()).toBe(timestamp); }); it("should get current date when called without arguments", () => { const now = dayjs(); expect(now.isValid()).toBe(true); expect(now.year()).toBeGreaterThan(2020); }); }); describe("utc plugin", () => { it("should support UTC conversion", () => { const date = dayjs("2023-01-01T12:00:00"); const utcDate = date.utc(); expect(utcDate).toBeDefined(); expect(utcDate.isValid()).toBe(true); }); it("should create UTC date", () => { const utcDate = dayjs.utc("2023-01-01"); expect(utcDate.isValid()).toBe(true); expect(utcDate.utcOffset()).toBe(0); }); it("should handle UTC offset correctly", () => { const date = dayjs.utc(); expect(date.utcOffset()).toBe(0); }); }); describe("timezone plugin", () => { it("should support timezone conversion", () => { const date = dayjs("2023-01-01T12:00:00"); const tzDate = date.tz("America/New_York"); expect(tzDate).toBeDefined(); expect(tzDate.isValid()).toBe(true); }); it("should create date in specific timezone", () => { const date = dayjs.tz("2023-01-01", "America/New_York"); expect(date.isValid()).toBe(true); }); it("should handle timezone method", () => { const date = dayjs(); // Just verify the method exists and doesn't throw expect(() => date.tz("UTC")).not.toThrow(); }); }); describe("isoWeek plugin", () => { it("should get ISO week of year", () => { const date = dayjs("2023-01-01"); const isoWeek = date.isoWeek(); expect(typeof isoWeek).toBe("number"); expect(isoWeek).toBeGreaterThan(0); expect(isoWeek).toBeLessThanOrEqual(53); }); it("should get ISO weekday", () => { const date = dayjs("2023-01-02"); // Monday const isoWeekday = date.isoWeekday(); expect(isoWeekday).toBe(1); // Monday is 1 in ISO weekday }); it("should set ISO week", () => { const date = dayjs("2023-01-01"); const newDate = date.isoWeek(10); expect(newDate.isoWeek()).toBe(10); }); }); describe("duration plugin", () => { it("should create duration from milliseconds", () => { const duration = dayjs.duration(1000); expect(duration.asSeconds()).toBe(1); }); it("should create duration with object", () => { const duration = dayjs.duration({ hours: 1, minutes: 30, }); expect(duration.asMinutes()).toBe(90); }); it("should format duration", () => { const duration = dayjs.duration(3661, "seconds"); expect(duration.hours()).toBe(1); expect(duration.minutes()).toBe(1); expect(duration.seconds()).toBe(1); }); it("should add duration to date", () => { const date = dayjs("2023-01-01"); const duration = dayjs.duration(1, "day"); const newDate = date.add(duration); expect(newDate.date()).toBe(2); }); it("should subtract duration from date", () => { const date = dayjs("2023-01-02"); const duration = dayjs.duration(1, "day"); const newDate = date.subtract(duration); expect(newDate.date()).toBe(1); }); }); describe("relativeTime plugin", () => { it("should get time from now", () => { const pastDate = dayjs().subtract(1, "hour"); const relative = pastDate.fromNow(); expect(typeof relative).toBe("string"); expect(relative).toContain("hour"); }); it("should get time to now", () => { const futureDate = dayjs().add(1, "hour"); const relative = futureDate.toNow(); expect(typeof relative).toBe("string"); expect(relative).toContain("hour"); }); it("should get time from specific date", () => { const date1 = dayjs("2023-01-01"); const date2 = dayjs("2023-01-02"); const relative = date1.from(date2); expect(typeof relative).toBe("string"); expect(relative).toContain("day"); }); it("should get time to specific date", () => { const date1 = dayjs("2023-01-01"); const date2 = dayjs("2023-01-02"); const relative = date2.to(date1); expect(typeof relative).toBe("string"); expect(relative).toContain("day"); }); it("should handle relative time for minutes", () => { const pastDate = dayjs().subtract(30, "minutes"); const relative = pastDate.fromNow(); expect(relative).toContain("minute"); }); it("should handle relative time for days", () => { const pastDate = dayjs().subtract(5, "days"); const relative = pastDate.fromNow(); expect(relative).toContain("day"); }); it("should handle relative time for months", () => { const pastDate = dayjs().subtract(2, "months"); const relative = pastDate.fromNow(); expect(relative).toContain("month"); }); it("should handle relative time for years", () => { const pastDate = dayjs().subtract(2, "years"); const relative = pastDate.fromNow(); expect(relative).toContain("year"); }); }); describe("custom relativeTime thresholds", () => { it("should respect custom threshold configuration", () => { // Our custom config has 59 minutes threshold for 'mm' const date = dayjs().subtract(45, "minutes"); const relative = date.fromNow(); // Should show minutes, not hours expect(relative).toContain("minute"); }); it("should handle edge case at day threshold", () => { const date = dayjs().subtract(29, "days"); const relative = date.fromNow(); // With custom threshold of 29 days, should still show days expect(relative).toContain("day"); }); }); describe("date manipulation", () => { it("should add time units", () => { const date = dayjs("2023-01-01"); expect(date.add(1, "day").date()).toBe(2); expect(date.add(1, "month").month()).toBe(1); expect(date.add(1, "year").year()).toBe(2024); }); it("should subtract time units", () => { const date = dayjs("2023-01-02"); expect(date.subtract(1, "day").date()).toBe(1); expect(date.subtract(1, "month").month()).toBe(11); // December of previous year expect(date.subtract(1, "year").year()).toBe(2022); }); it("should start of time unit", () => { const date = dayjs("2023-06-15T14:30:45"); expect(date.startOf("day").hour()).toBe(0); expect(date.startOf("month").date()).toBe(1); expect(date.startOf("year").month()).toBe(0); }); it("should end of time unit", () => { const date = dayjs("2023-06-15T14:30:45"); expect(date.endOf("day").hour()).toBe(23); expect(date.endOf("month").date()).toBe(30); // June has 30 days expect(date.endOf("year").month()).toBe(11); // December }); }); describe("formatting", () => { it("should format date", () => { const date = dayjs("2023-01-01T12:30:45"); expect(date.format("YYYY-MM-DD")).toBe("2023-01-01"); expect(date.format("YYYY/MM/DD HH:mm:ss")).toContain("2023/01/01"); }); it("should convert to ISO string", () => { const date = dayjs("2023-01-01"); const iso = date.toISOString(); expect(iso).toContain("2023-01-01"); expect(iso).toContain("T"); expect(iso).toContain("Z"); }); it("should convert to Date object", () => { const dayjsDate = dayjs("2023-01-01"); const jsDate = dayjsDate.toDate(); expect(jsDate).toBeInstanceOf(Date); expect(jsDate.getFullYear()).toBe(2023); }); }); describe("comparison", () => { it("should check if date is before another", () => { const date1 = dayjs("2023-01-01"); const date2 = dayjs("2023-01-02"); expect(date1.isBefore(date2)).toBe(true); expect(date2.isBefore(date1)).toBe(false); }); it("should check if date is after another", () => { const date1 = dayjs("2023-01-01"); const date2 = dayjs("2023-01-02"); expect(date2.isAfter(date1)).toBe(true); expect(date1.isAfter(date2)).toBe(false); }); it("should check if date is same as another", () => { const date1 = dayjs("2023-01-01"); const date2 = dayjs("2023-01-01"); expect(date1.isSame(date2)).toBe(true); }); }); describe("validation", () => { it("should validate valid dates", () => { expect(dayjs("2023-01-01").isValid()).toBe(true); expect(dayjs(new Date()).isValid()).toBe(true); expect(dayjs(Date.now()).isValid()).toBe(true); }); it("should detect invalid dates", () => { expect(dayjs("invalid").isValid()).toBe(false); expect(dayjs("2023-13-01", "YYYY-MM-DD", true).isValid()).toBe(false); }); }); });