Skip to content

Commit 6f732a7

Browse files
Add fallback tests for timezone functions when date-fns-tz is not installed
1 parent 7755ac4 commit 6f732a7

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/date_utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ interface DateFnsTz {
8181
let dateFnsTz: DateFnsTz | null = null;
8282
let dateFnsTzLoadAttempted = false;
8383

84+
/**
85+
* Resets the date-fns-tz module cache. Used for testing.
86+
* @internal
87+
*/
88+
export function __resetDateFnsTzCache(): void {
89+
dateFnsTz = null;
90+
dateFnsTzLoadAttempted = false;
91+
}
92+
93+
/**
94+
* Sets the date-fns-tz module to null to simulate it not being installed. Used for testing.
95+
* @internal
96+
*/
97+
export function __setDateFnsTzNull(): void {
98+
dateFnsTz = null;
99+
dateFnsTzLoadAttempted = true;
100+
}
101+
84102
/**
85103
* Attempts to load date-fns-tz module.
86104
* Returns null if the module is not installed.

src/test/timezone_test.test.tsx

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import React from "react";
22
import { render, fireEvent } from "@testing-library/react";
33
import DatePicker from "../index";
4-
import {
4+
import * as dateUtils from "../date_utils";
5+
6+
const {
57
toZonedTime,
68
fromZonedTime,
79
formatInTimeZone,
810
nowInTimeZone,
9-
} from "../date_utils";
11+
__resetDateFnsTzCache,
12+
__setDateFnsTzNull,
13+
} = dateUtils;
1014

1115
describe("Timezone utility functions", () => {
1216
// Use a fixed UTC date for consistent testing
@@ -467,3 +471,75 @@ describe("DatePicker with timeZone prop", () => {
467471
expect(onChangeMock).toHaveBeenCalled();
468472
});
469473
});
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

Comments
 (0)