|
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent } from "@testing-library/react"; |
| 3 | +import DatePicker from "../index"; |
1 | 4 | import { |
2 | 5 | toZonedTime, |
3 | 6 | fromZonedTime, |
@@ -261,3 +264,206 @@ describe("Timezone utility functions - integration", () => { |
261 | 264 | expect(tokyoFormatted).toBe("2024-06-15 09:00"); // Same day, later in Tokyo |
262 | 265 | }); |
263 | 266 | }); |
| 267 | + |
| 268 | +describe("DatePicker with timeZone prop", () => { |
| 269 | + it("should render DatePicker with timeZone prop", () => { |
| 270 | + const { container } = render( |
| 271 | + <DatePicker |
| 272 | + selected={new Date("2024-06-15T12:00:00Z")} |
| 273 | + onChange={() => {}} |
| 274 | + timeZone="America/New_York" |
| 275 | + />, |
| 276 | + ); |
| 277 | + expect(container.querySelector("input")).not.toBeNull(); |
| 278 | + }); |
| 279 | + |
| 280 | + it("should convert initial date to timezone", () => { |
| 281 | + const utcDate = new Date("2024-06-15T12:00:00Z"); |
| 282 | + const { container } = render( |
| 283 | + <DatePicker |
| 284 | + selected={utcDate} |
| 285 | + onChange={() => {}} |
| 286 | + timeZone="America/New_York" |
| 287 | + dateFormat="yyyy-MM-dd" |
| 288 | + />, |
| 289 | + ); |
| 290 | + const input = container.querySelector("input"); |
| 291 | + // The date should be displayed (time formatting depends on local timezone) |
| 292 | + expect(input?.value).toContain("2024-06-15"); |
| 293 | + }); |
| 294 | + |
| 295 | + it("should handle date selection with timezone", () => { |
| 296 | + const utcDate = new Date("2024-06-15T12:00:00Z"); |
| 297 | + let selectedDate: Date | null = null; |
| 298 | + |
| 299 | + const { container } = render( |
| 300 | + <DatePicker |
| 301 | + selected={utcDate} |
| 302 | + onChange={(date) => { |
| 303 | + selectedDate = date; |
| 304 | + }} |
| 305 | + timeZone="America/New_York" |
| 306 | + />, |
| 307 | + ); |
| 308 | + |
| 309 | + // Open the calendar |
| 310 | + const input = container.querySelector("input"); |
| 311 | + if (input) { |
| 312 | + fireEvent.focus(input); |
| 313 | + } |
| 314 | + |
| 315 | + // Find and click a day |
| 316 | + const days = container.querySelectorAll(".react-datepicker__day"); |
| 317 | + const dayToClick = Array.from(days).find( |
| 318 | + (day) => |
| 319 | + !day.classList.contains("react-datepicker__day--outside-month") && |
| 320 | + day.textContent === "20", |
| 321 | + ); |
| 322 | + |
| 323 | + if (dayToClick) { |
| 324 | + fireEvent.click(dayToClick); |
| 325 | + } |
| 326 | + |
| 327 | + // The selected date should be converted back to UTC |
| 328 | + expect(selectedDate).not.toBeNull(); |
| 329 | + }); |
| 330 | + |
| 331 | + it("should handle preSelection with timezone", () => { |
| 332 | + const { container } = render( |
| 333 | + <DatePicker onChange={() => {}} timeZone="Europe/London" />, |
| 334 | + ); |
| 335 | + |
| 336 | + // Open the calendar |
| 337 | + const input = container.querySelector("input"); |
| 338 | + if (input) { |
| 339 | + fireEvent.focus(input); |
| 340 | + } |
| 341 | + |
| 342 | + // Calendar should be open |
| 343 | + expect(container.querySelector(".react-datepicker__month")).not.toBeNull(); |
| 344 | + }); |
| 345 | + |
| 346 | + it("should work with inline mode and timezone", () => { |
| 347 | + const utcDate = new Date("2024-06-15T12:00:00Z"); |
| 348 | + const { container } = render( |
| 349 | + <DatePicker |
| 350 | + selected={utcDate} |
| 351 | + onChange={() => {}} |
| 352 | + timeZone="Asia/Tokyo" |
| 353 | + inline |
| 354 | + />, |
| 355 | + ); |
| 356 | + |
| 357 | + // Calendar should be visible inline |
| 358 | + expect(container.querySelector(".react-datepicker__month")).not.toBeNull(); |
| 359 | + }); |
| 360 | + |
| 361 | + it("should handle onChange with timezone conversion", () => { |
| 362 | + const utcDate = new Date("2024-06-15T12:00:00Z"); |
| 363 | + const onChangeMock = jest.fn(); |
| 364 | + |
| 365 | + const { container } = render( |
| 366 | + <DatePicker |
| 367 | + selected={utcDate} |
| 368 | + onChange={onChangeMock} |
| 369 | + timeZone="America/Los_Angeles" |
| 370 | + />, |
| 371 | + ); |
| 372 | + |
| 373 | + // Open the calendar |
| 374 | + const input = container.querySelector("input"); |
| 375 | + if (input) { |
| 376 | + fireEvent.focus(input); |
| 377 | + } |
| 378 | + |
| 379 | + // Find and click a day |
| 380 | + const days = container.querySelectorAll(".react-datepicker__day"); |
| 381 | + const dayToClick = Array.from(days).find( |
| 382 | + (day) => |
| 383 | + !day.classList.contains("react-datepicker__day--outside-month") && |
| 384 | + day.textContent === "15", |
| 385 | + ); |
| 386 | + |
| 387 | + if (dayToClick) { |
| 388 | + fireEvent.click(dayToClick); |
| 389 | + } |
| 390 | + |
| 391 | + // onChange should have been called |
| 392 | + expect(onChangeMock).toHaveBeenCalled(); |
| 393 | + }); |
| 394 | + |
| 395 | + it("should handle onSelect with timezone", () => { |
| 396 | + const utcDate = new Date("2024-06-15T12:00:00Z"); |
| 397 | + const onSelectMock = jest.fn(); |
| 398 | + |
| 399 | + const { container } = render( |
| 400 | + <DatePicker |
| 401 | + selected={utcDate} |
| 402 | + onChange={() => {}} |
| 403 | + onSelect={onSelectMock} |
| 404 | + timeZone="UTC" |
| 405 | + />, |
| 406 | + ); |
| 407 | + |
| 408 | + // Open the calendar |
| 409 | + const input = container.querySelector("input"); |
| 410 | + if (input) { |
| 411 | + fireEvent.focus(input); |
| 412 | + } |
| 413 | + |
| 414 | + // Find and click a day |
| 415 | + const days = container.querySelectorAll(".react-datepicker__day"); |
| 416 | + const dayToClick = Array.from(days).find( |
| 417 | + (day) => |
| 418 | + !day.classList.contains("react-datepicker__day--outside-month") && |
| 419 | + day.textContent === "15", |
| 420 | + ); |
| 421 | + |
| 422 | + if (dayToClick) { |
| 423 | + fireEvent.click(dayToClick); |
| 424 | + } |
| 425 | + |
| 426 | + // onSelect should have been called |
| 427 | + expect(onSelectMock).toHaveBeenCalled(); |
| 428 | + }); |
| 429 | + |
| 430 | + it("should handle time change with timezone", () => { |
| 431 | + // Mock ResizeObserver |
| 432 | + const mockResizeObserver = jest.fn().mockImplementation(() => ({ |
| 433 | + observe: jest.fn(), |
| 434 | + unobserve: jest.fn(), |
| 435 | + disconnect: jest.fn(), |
| 436 | + })); |
| 437 | + window.ResizeObserver = mockResizeObserver; |
| 438 | + |
| 439 | + const utcDate = new Date("2024-06-15T12:00:00Z"); |
| 440 | + const onChangeMock = jest.fn(); |
| 441 | + |
| 442 | + const { container } = render( |
| 443 | + <DatePicker |
| 444 | + selected={utcDate} |
| 445 | + onChange={onChangeMock} |
| 446 | + timeZone="America/New_York" |
| 447 | + showTimeSelect |
| 448 | + dateFormat="yyyy-MM-dd HH:mm" |
| 449 | + />, |
| 450 | + ); |
| 451 | + |
| 452 | + // Open the calendar |
| 453 | + const input = container.querySelector("input"); |
| 454 | + if (input) { |
| 455 | + fireEvent.focus(input); |
| 456 | + } |
| 457 | + |
| 458 | + // Find and click a time option |
| 459 | + const timeOptions = container.querySelectorAll( |
| 460 | + ".react-datepicker__time-list-item", |
| 461 | + ); |
| 462 | + if (timeOptions.length > 0) { |
| 463 | + fireEvent.click(timeOptions[0]!); |
| 464 | + } |
| 465 | + |
| 466 | + // onChange should have been called with timezone conversion |
| 467 | + expect(onChangeMock).toHaveBeenCalled(); |
| 468 | + }); |
| 469 | +}); |
0 commit comments