Skip to content

Commit d045955

Browse files
test: add coverage for timezone with selectsRange in handleTimeChange
1 parent 1ededb4 commit d045955

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

src/test/timezone_test.test.tsx

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,148 @@ describe("DatePicker with timeZone prop", () => {
470470
// onChange should have been called with timezone conversion
471471
expect(onChangeMock).toHaveBeenCalled();
472472
});
473+
474+
it("should handle time change with timezone and selectsRange using showTimeInput (start date)", () => {
475+
const startDate = new Date("2024-06-15T12:00:00Z");
476+
const endDate = new Date("2024-06-20T14:00:00Z");
477+
const onChangeMock = jest.fn();
478+
479+
const { container } = render(
480+
<DatePicker
481+
selectsRange
482+
startDate={startDate}
483+
endDate={endDate}
484+
onChange={onChangeMock}
485+
timeZone="America/New_York"
486+
showTimeInput
487+
inline
488+
/>,
489+
);
490+
491+
// Find the start time input and change it
492+
const timeInputs = container.querySelectorAll(
493+
".react-datepicker-time__input input",
494+
);
495+
expect(timeInputs.length).toBe(2);
496+
497+
// Change the start time input
498+
fireEvent.change(timeInputs[0]!, { target: { value: "10:30" } });
499+
500+
// onChange should have been called with timezone conversion
501+
expect(onChangeMock).toHaveBeenCalled();
502+
const [changedStartDate, changedEndDate] = onChangeMock.mock.calls[0][0];
503+
504+
// Both dates should be Date objects
505+
expect(changedStartDate).toBeInstanceOf(Date);
506+
expect(changedEndDate).toBeInstanceOf(Date);
507+
});
508+
509+
it("should handle time change with timezone and selectsRange using showTimeInput (end date)", () => {
510+
const startDate = new Date("2024-06-15T12:00:00Z");
511+
const endDate = new Date("2024-06-20T14:00:00Z");
512+
const onChangeMock = jest.fn();
513+
514+
const { container } = render(
515+
<DatePicker
516+
selectsRange
517+
startDate={startDate}
518+
endDate={endDate}
519+
onChange={onChangeMock}
520+
timeZone="America/New_York"
521+
showTimeInput
522+
inline
523+
/>,
524+
);
525+
526+
// Find the end time input and change it
527+
const timeInputs = container.querySelectorAll(
528+
".react-datepicker-time__input input",
529+
);
530+
expect(timeInputs.length).toBe(2);
531+
532+
// Change the end time input
533+
fireEvent.change(timeInputs[1]!, { target: { value: "16:45" } });
534+
535+
// onChange should have been called with timezone conversion
536+
expect(onChangeMock).toHaveBeenCalled();
537+
const [changedStartDate, changedEndDate] = onChangeMock.mock.calls[0][0];
538+
539+
// Both dates should be Date objects
540+
expect(changedStartDate).toBeInstanceOf(Date);
541+
expect(changedEndDate).toBeInstanceOf(Date);
542+
});
543+
544+
it("should handle time change with timezone and selectsRange with only start date", () => {
545+
const startDate = new Date("2024-06-15T12:00:00Z");
546+
const onChangeMock = jest.fn();
547+
548+
const { container } = render(
549+
<DatePicker
550+
selectsRange
551+
startDate={startDate}
552+
endDate={null}
553+
onChange={onChangeMock}
554+
timeZone="America/New_York"
555+
showTimeInput
556+
inline
557+
/>,
558+
);
559+
560+
// Find the start time input and change it
561+
const timeInputs = container.querySelectorAll(
562+
".react-datepicker-time__input input",
563+
);
564+
expect(timeInputs.length).toBe(2);
565+
566+
// Change the start time input
567+
fireEvent.change(timeInputs[0]!, { target: { value: "10:30" } });
568+
569+
// onChange should have been called with timezone conversion
570+
expect(onChangeMock).toHaveBeenCalled();
571+
const [changedStartDate, changedEndDate] = onChangeMock.mock.calls[0][0];
572+
573+
// Start date should be converted, end date should be null
574+
expect(changedStartDate).toBeInstanceOf(Date);
575+
expect(changedEndDate).toBeNull();
576+
});
577+
578+
it("should handle time change with timezone and selectsRange using legacy showTimeSelect", () => {
579+
// Mock ResizeObserver
580+
const mockResizeObserver = jest.fn().mockImplementation(() => ({
581+
observe: jest.fn(),
582+
unobserve: jest.fn(),
583+
disconnect: jest.fn(),
584+
}));
585+
window.ResizeObserver = mockResizeObserver;
586+
587+
const startDate = new Date("2024-06-15T12:00:00Z");
588+
const endDate = new Date("2024-06-20T14:00:00Z");
589+
const onChangeMock = jest.fn();
590+
591+
const { container } = render(
592+
<DatePicker
593+
selectsRange
594+
startDate={startDate}
595+
endDate={endDate}
596+
onChange={onChangeMock}
597+
timeZone="America/New_York"
598+
showTimeSelect
599+
dateFormat="yyyy-MM-dd HH:mm"
600+
inline
601+
/>,
602+
);
603+
604+
// Find and click a time option (legacy single time picker behavior)
605+
const timeOptions = container.querySelectorAll(
606+
".react-datepicker__time-list-item",
607+
);
608+
if (timeOptions.length > 0) {
609+
fireEvent.click(timeOptions[0]!);
610+
}
611+
612+
// onChange should have been called with timezone conversion
613+
expect(onChangeMock).toHaveBeenCalled();
614+
});
473615
});
474616

475617
describe("Timezone fallback behavior (when date-fns-tz is not installed)", () => {

0 commit comments

Comments
 (0)