|
| 1 | +import { |
| 2 | + toZonedTime, |
| 3 | + fromZonedTime, |
| 4 | + formatInTimeZone, |
| 5 | + nowInTimeZone, |
| 6 | +} from "../date_utils"; |
| 7 | + |
| 8 | +describe("Timezone utility functions", () => { |
| 9 | + const testDate = new Date("2024-06-15T12:00:00Z"); |
| 10 | + |
| 11 | + describe("toZonedTime", () => { |
| 12 | + it("should return the original date when no timezone is provided", () => { |
| 13 | + const result = toZonedTime(testDate); |
| 14 | + expect(result).toBe(testDate); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should return the original date when timezone is undefined", () => { |
| 18 | + const result = toZonedTime(testDate, undefined); |
| 19 | + expect(result).toBe(testDate); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should return the original date when date-fns-tz is not installed", () => { |
| 23 | + // Suppress console.warn for this test |
| 24 | + const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 25 | + |
| 26 | + // Since date-fns-tz is not installed, it should return the original date |
| 27 | + const result = toZonedTime(testDate, "America/New_York"); |
| 28 | + expect(result).toBe(testDate); |
| 29 | + |
| 30 | + // Should have warned about missing date-fns-tz |
| 31 | + expect(warnSpy).toHaveBeenCalledWith( |
| 32 | + expect.stringContaining("date-fns-tz"), |
| 33 | + ); |
| 34 | + |
| 35 | + warnSpy.mockRestore(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("fromZonedTime", () => { |
| 40 | + it("should return the original date when no timezone is provided", () => { |
| 41 | + const result = fromZonedTime(testDate); |
| 42 | + expect(result).toBe(testDate); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should return the original date when timezone is undefined", () => { |
| 46 | + const result = fromZonedTime(testDate, undefined); |
| 47 | + expect(result).toBe(testDate); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return the original date when date-fns-tz is not installed", () => { |
| 51 | + // Suppress console.warn for this test |
| 52 | + const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 53 | + |
| 54 | + // Since date-fns-tz is not installed, it should return the original date |
| 55 | + const result = fromZonedTime(testDate, "America/New_York"); |
| 56 | + expect(result).toBe(testDate); |
| 57 | + |
| 58 | + warnSpy.mockRestore(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("formatInTimeZone", () => { |
| 63 | + it("should use standard format when no timezone is provided", () => { |
| 64 | + const result = formatInTimeZone(testDate, "yyyy-MM-dd"); |
| 65 | + // Without timezone, it should use the standard format function |
| 66 | + expect(result).toBe("2024-06-15"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should use standard format when date-fns-tz is not installed", () => { |
| 70 | + // Suppress console.warn for this test |
| 71 | + const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 72 | + |
| 73 | + // Since date-fns-tz is not installed, it should fall back to standard format |
| 74 | + const result = formatInTimeZone( |
| 75 | + testDate, |
| 76 | + "yyyy-MM-dd", |
| 77 | + "America/New_York", |
| 78 | + ); |
| 79 | + expect(result).toBe("2024-06-15"); |
| 80 | + |
| 81 | + warnSpy.mockRestore(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("nowInTimeZone", () => { |
| 86 | + it("should return current date when no timezone is provided", () => { |
| 87 | + const before = new Date(); |
| 88 | + const result = nowInTimeZone(); |
| 89 | + const after = new Date(); |
| 90 | + |
| 91 | + expect(result.getTime()).toBeGreaterThanOrEqual(before.getTime()); |
| 92 | + expect(result.getTime()).toBeLessThanOrEqual(after.getTime()); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should return current date when date-fns-tz is not installed", () => { |
| 96 | + // Suppress console.warn for this test |
| 97 | + const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 98 | + |
| 99 | + const before = new Date(); |
| 100 | + const result = nowInTimeZone("America/New_York"); |
| 101 | + const after = new Date(); |
| 102 | + |
| 103 | + // The result should be a Date object within the expected range |
| 104 | + expect(result).toBeInstanceOf(Date); |
| 105 | + expect(result.getTime()).toBeGreaterThanOrEqual(before.getTime()); |
| 106 | + expect(result.getTime()).toBeLessThanOrEqual(after.getTime()); |
| 107 | + |
| 108 | + warnSpy.mockRestore(); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe("Timezone utility functions - edge cases", () => { |
| 114 | + it("should handle empty string timezone", () => { |
| 115 | + const testDate = new Date("2024-06-15T12:00:00Z"); |
| 116 | + const result = toZonedTime(testDate, ""); |
| 117 | + // Empty string is falsy, so should return original date |
| 118 | + expect(result).toBe(testDate); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should handle various date inputs", () => { |
| 122 | + const dates = [ |
| 123 | + new Date("2024-01-01T00:00:00Z"), |
| 124 | + new Date("2024-06-15T12:30:45Z"), |
| 125 | + new Date("2024-12-31T23:59:59Z"), |
| 126 | + ]; |
| 127 | + |
| 128 | + dates.forEach((date) => { |
| 129 | + const result = toZonedTime(date); |
| 130 | + expect(result).toBe(date); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments