|
1 | 1 | import React from "react"; |
2 | 2 | import { render, fireEvent } from "@testing-library/react"; |
3 | 3 | import DatePicker from "../index"; |
4 | | -import { |
| 4 | +import * as dateUtils from "../date_utils"; |
| 5 | + |
| 6 | +const { |
5 | 7 | toZonedTime, |
6 | 8 | fromZonedTime, |
7 | 9 | formatInTimeZone, |
8 | 10 | nowInTimeZone, |
9 | | -} from "../date_utils"; |
| 11 | + __resetDateFnsTzCache, |
| 12 | + __setDateFnsTzNull, |
| 13 | +} = dateUtils; |
10 | 14 |
|
11 | 15 | describe("Timezone utility functions", () => { |
12 | 16 | // Use a fixed UTC date for consistent testing |
@@ -467,3 +471,75 @@ describe("DatePicker with timeZone prop", () => { |
467 | 471 | expect(onChangeMock).toHaveBeenCalled(); |
468 | 472 | }); |
469 | 473 | }); |
| 474 | + |
| 475 | +describe("Timezone fallback behavior (when date-fns-tz is not installed)", () => { |
| 476 | + const originalNodeEnv = process.env.NODE_ENV; |
| 477 | + |
| 478 | + beforeEach(() => { |
| 479 | + // Set to development to trigger console.warn |
| 480 | + process.env.NODE_ENV = "development"; |
| 481 | + // Simulate date-fns-tz not being installed |
| 482 | + __setDateFnsTzNull(); |
| 483 | + }); |
| 484 | + |
| 485 | + afterEach(() => { |
| 486 | + // Reset the cache after each test |
| 487 | + __resetDateFnsTzCache(); |
| 488 | + process.env.NODE_ENV = originalNodeEnv; |
| 489 | + }); |
| 490 | + |
| 491 | + it("toZonedTime should return original date and warn when date-fns-tz is not installed", () => { |
| 492 | + const consoleSpy = jest.spyOn(console, "warn").mockImplementation(); |
| 493 | + const testDate = new Date("2024-06-15T12:00:00Z"); |
| 494 | + |
| 495 | + const result = toZonedTime(testDate, "America/New_York"); |
| 496 | + |
| 497 | + expect(result).toBe(testDate); |
| 498 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 499 | + expect.stringContaining("date-fns-tz"), |
| 500 | + ); |
| 501 | + |
| 502 | + consoleSpy.mockRestore(); |
| 503 | + }); |
| 504 | + |
| 505 | + it("fromZonedTime should return original date and warn when date-fns-tz is not installed", () => { |
| 506 | + const consoleSpy = jest.spyOn(console, "warn").mockImplementation(); |
| 507 | + const testDate = new Date("2024-06-15T12:00:00Z"); |
| 508 | + |
| 509 | + const result = fromZonedTime(testDate, "America/New_York"); |
| 510 | + |
| 511 | + expect(result).toBe(testDate); |
| 512 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 513 | + expect.stringContaining("date-fns-tz"), |
| 514 | + ); |
| 515 | + |
| 516 | + consoleSpy.mockRestore(); |
| 517 | + }); |
| 518 | + |
| 519 | + it("formatInTimeZone should use standard format and warn when date-fns-tz is not installed", () => { |
| 520 | + const consoleSpy = jest.spyOn(console, "warn").mockImplementation(); |
| 521 | + const testDate = new Date("2024-06-15T12:00:00Z"); |
| 522 | + |
| 523 | + const result = formatInTimeZone(testDate, "yyyy-MM-dd", "America/New_York"); |
| 524 | + |
| 525 | + // Should return formatted date using standard format |
| 526 | + expect(result).toBe("2024-06-15"); |
| 527 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 528 | + expect.stringContaining("date-fns-tz"), |
| 529 | + ); |
| 530 | + |
| 531 | + consoleSpy.mockRestore(); |
| 532 | + }); |
| 533 | + |
| 534 | + it("should not warn in production mode", () => { |
| 535 | + process.env.NODE_ENV = "production"; |
| 536 | + const consoleSpy = jest.spyOn(console, "warn").mockImplementation(); |
| 537 | + const testDate = new Date("2024-06-15T12:00:00Z"); |
| 538 | + |
| 539 | + toZonedTime(testDate, "America/New_York"); |
| 540 | + |
| 541 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 542 | + |
| 543 | + consoleSpy.mockRestore(); |
| 544 | + }); |
| 545 | +}); |
0 commit comments