|
| 1 | +import { render } from "@testing-library/react"; |
| 2 | +import React from "react"; |
| 3 | + |
| 4 | +import DatePicker from "../index"; |
| 5 | +import { ReactDatePickerCustomDayNameProps } from "../calendar"; |
| 6 | + |
| 7 | +describe("renderCustomDayName", () => { |
| 8 | + it("should call renderCustomDayName function with correct parameters", () => { |
| 9 | + const renderCustomDayName = jest.fn( |
| 10 | + ({ shortName }: ReactDatePickerCustomDayNameProps) => ( |
| 11 | + <span>{shortName}</span> |
| 12 | + ), |
| 13 | + ); |
| 14 | + |
| 15 | + render(<DatePicker renderCustomDayName={renderCustomDayName} inline />); |
| 16 | + |
| 17 | + // Should be called 7 times (one for each day of the week) |
| 18 | + expect(renderCustomDayName).toHaveBeenCalledTimes(7); |
| 19 | + |
| 20 | + // Check that it's called with correct parameters |
| 21 | + const firstCall = renderCustomDayName.mock.calls[0]?.[0]; |
| 22 | + expect(firstCall).toBeDefined(); |
| 23 | + expect(firstCall).toHaveProperty("day"); |
| 24 | + expect(firstCall).toHaveProperty("shortName"); |
| 25 | + expect(firstCall).toHaveProperty("fullName"); |
| 26 | + expect(firstCall).toHaveProperty("locale"); |
| 27 | + expect(firstCall).toHaveProperty("customDayNameCount"); |
| 28 | + expect(firstCall?.day).toBeInstanceOf(Date); |
| 29 | + expect(typeof firstCall?.shortName).toBe("string"); |
| 30 | + expect(typeof firstCall?.fullName).toBe("string"); |
| 31 | + expect(typeof firstCall?.customDayNameCount).toBe("number"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should render custom day names", () => { |
| 35 | + const renderCustomDayName = ({ |
| 36 | + shortName, |
| 37 | + }: ReactDatePickerCustomDayNameProps) => ( |
| 38 | + <span className="custom-day-name">Custom-{shortName}</span> |
| 39 | + ); |
| 40 | + |
| 41 | + const { container } = render( |
| 42 | + <DatePicker renderCustomDayName={renderCustomDayName} inline />, |
| 43 | + ); |
| 44 | + |
| 45 | + const customDayNames = container.querySelectorAll(".custom-day-name"); |
| 46 | + expect(customDayNames).toHaveLength(7); |
| 47 | + expect(customDayNames[0]?.textContent).toContain("Custom-"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should render default day names when renderCustomDayName is not provided", () => { |
| 51 | + const { container } = render(<DatePicker inline />); |
| 52 | + |
| 53 | + const dayNames = container.querySelectorAll(".react-datepicker__day-name"); |
| 54 | + expect(dayNames).toHaveLength(7); |
| 55 | + |
| 56 | + // Check that default structure is present (sr-only + aria-hidden) |
| 57 | + const firstDayName = dayNames[0]; |
| 58 | + expect( |
| 59 | + firstDayName?.querySelector(".react-datepicker__sr-only"), |
| 60 | + ).not.toBeNull(); |
| 61 | + expect(firstDayName?.querySelector('[aria-hidden="true"]')).not.toBeNull(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should use custom day names with accessibility", () => { |
| 65 | + const renderCustomDayName = ({ |
| 66 | + shortName, |
| 67 | + fullName, |
| 68 | + }: ReactDatePickerCustomDayNameProps) => ( |
| 69 | + <> |
| 70 | + <span className="react-datepicker__sr-only">{fullName}</span> |
| 71 | + <span aria-hidden="true">{shortName}</span> |
| 72 | + </> |
| 73 | + ); |
| 74 | + |
| 75 | + const { container } = render( |
| 76 | + <DatePicker renderCustomDayName={renderCustomDayName} inline />, |
| 77 | + ); |
| 78 | + |
| 79 | + const dayNames = container.querySelectorAll(".react-datepicker__day-name"); |
| 80 | + expect(dayNames).toHaveLength(7); |
| 81 | + |
| 82 | + // Check that accessibility structure is maintained |
| 83 | + dayNames.forEach((dayName) => { |
| 84 | + expect( |
| 85 | + dayName.querySelector(".react-datepicker__sr-only"), |
| 86 | + ).not.toBeNull(); |
| 87 | + expect(dayName.querySelector('[aria-hidden="true"]')).not.toBeNull(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should apply weekDayClassName along with custom day names", () => { |
| 92 | + const weekDayClassName = (date: Date) => { |
| 93 | + return date.getDay() === 0 || date.getDay() === 6 ? "weekend" : ""; |
| 94 | + }; |
| 95 | + |
| 96 | + const renderCustomDayName = ({ |
| 97 | + shortName, |
| 98 | + }: ReactDatePickerCustomDayNameProps) => <span>{shortName}</span>; |
| 99 | + |
| 100 | + const { container } = render( |
| 101 | + <DatePicker |
| 102 | + renderCustomDayName={renderCustomDayName} |
| 103 | + weekDayClassName={weekDayClassName} |
| 104 | + inline |
| 105 | + />, |
| 106 | + ); |
| 107 | + |
| 108 | + const weekendDays = container.querySelectorAll( |
| 109 | + ".react-datepicker__day-name.weekend", |
| 110 | + ); |
| 111 | + // Should have 2 weekend days (Saturday and Sunday) |
| 112 | + expect(weekendDays.length).toBeGreaterThanOrEqual(2); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should pass locale to renderCustomDayName", () => { |
| 116 | + const renderCustomDayName = jest.fn( |
| 117 | + ({ shortName }: ReactDatePickerCustomDayNameProps) => ( |
| 118 | + <span>{shortName}</span> |
| 119 | + ), |
| 120 | + ); |
| 121 | + |
| 122 | + render( |
| 123 | + <DatePicker |
| 124 | + renderCustomDayName={renderCustomDayName} |
| 125 | + locale="en-US" |
| 126 | + inline |
| 127 | + />, |
| 128 | + ); |
| 129 | + |
| 130 | + const firstCall = renderCustomDayName.mock.calls[0]?.[0]; |
| 131 | + expect(firstCall?.locale).toBe("en-US"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should pass customDayNameCount when displaying multiple months", () => { |
| 135 | + const renderCustomDayName = jest.fn( |
| 136 | + ({ shortName }: ReactDatePickerCustomDayNameProps) => ( |
| 137 | + <span>{shortName}</span> |
| 138 | + ), |
| 139 | + ); |
| 140 | + |
| 141 | + render( |
| 142 | + <DatePicker |
| 143 | + renderCustomDayName={renderCustomDayName} |
| 144 | + monthsShown={3} |
| 145 | + inline |
| 146 | + />, |
| 147 | + ); |
| 148 | + |
| 149 | + // Should be called 7 times per month, so 21 times for 3 months |
| 150 | + expect(renderCustomDayName).toHaveBeenCalledTimes(21); |
| 151 | + |
| 152 | + // Check that customDayNameCount is different for each month |
| 153 | + const firstMonthCall = renderCustomDayName.mock.calls[0]?.[0]; |
| 154 | + const secondMonthCall = renderCustomDayName.mock.calls[7]?.[0]; |
| 155 | + const thirdMonthCall = renderCustomDayName.mock.calls[14]?.[0]; |
| 156 | + |
| 157 | + expect(firstMonthCall?.customDayNameCount).toBe(0); |
| 158 | + expect(secondMonthCall?.customDayNameCount).toBe(1); |
| 159 | + expect(thirdMonthCall?.customDayNameCount).toBe(2); |
| 160 | + }); |
| 161 | +}); |
0 commit comments