Skip to content

Commit 4cc5dd9

Browse files
committed
fix: resolve TypeScript errors in test files - Add null checks for DOM queries - Replace toHaveClass with classList.contains() - Complete mockPopperProps with required Floating UI properties
1 parent 5197d5b commit 4cc5dd9

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/test/month_dropdown_options_test.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ describe("MonthDropdownOptions", () => {
119119
const monthOptions = container.querySelectorAll(
120120
".react-datepicker__month-option",
121121
);
122-
fireEvent.keyDown(monthOptions[5], { key: "Enter" });
122+
const monthOption = monthOptions[5];
123+
if (monthOption) {
124+
fireEvent.keyDown(monthOption, { key: "Enter" });
125+
}
123126

124127
expect(handleChange).toHaveBeenCalledWith(5);
125128
});

src/test/popper_component_test.test.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ const mockPopperProps = {
5252
left: 20,
5353
},
5454
placement: "bottom" as const,
55+
strategy: "absolute" as const,
56+
middlewareData: {},
57+
x: 0,
58+
y: 0,
59+
isPositioned: true,
60+
update: jest.fn(),
61+
elements: {},
5562
context: {},
5663
arrowRef: { current: null },
5764
};
@@ -96,7 +103,7 @@ describe("PopperComponent", () => {
96103
);
97104

98105
const popper = container.querySelector(".react-datepicker-popper");
99-
expect(popper).toHaveClass("custom-popper-class");
106+
expect(popper?.classList.contains("custom-popper-class")).toBe(true);
100107
});
101108

102109
it("should apply wrapperClassName to wrapper", () => {
@@ -108,7 +115,7 @@ describe("PopperComponent", () => {
108115
);
109116

110117
const wrapper = container.querySelector(".react-datepicker-wrapper");
111-
expect(wrapper).toHaveClass("custom-wrapper-class");
118+
expect(wrapper?.classList.contains("custom-wrapper-class")).toBe(true);
112119
});
113120

114121
it("should render arrow when showArrow is true", () => {
@@ -137,7 +144,7 @@ describe("PopperComponent", () => {
137144
);
138145

139146
const arrow = getByTestId("floating-arrow");
140-
expect(arrow).toHaveClass("react-datepicker__triangle");
147+
expect(arrow.classList.contains("react-datepicker__triangle")).toBe(true);
141148
});
142149

143150
it("should wrap popper in TabLoop", () => {
@@ -273,9 +280,9 @@ describe("PopperComponent", () => {
273280
);
274281

275282
const wrapper = container.querySelector(".react-datepicker-wrapper");
276-
expect(wrapper).toHaveClass("react-datepicker-wrapper");
277-
expect(wrapper).toHaveClass("custom-wrapper");
278-
expect(wrapper).toHaveClass("extra-class");
283+
expect(wrapper?.classList.contains("react-datepicker-wrapper")).toBe(true);
284+
expect(wrapper?.classList.contains("custom-wrapper")).toBe(true);
285+
expect(wrapper?.classList.contains("extra-class")).toBe(true);
279286
});
280287

281288
it("should combine popper classes correctly", () => {
@@ -288,9 +295,9 @@ describe("PopperComponent", () => {
288295
);
289296

290297
const popper = container.querySelector(".react-datepicker-popper");
291-
expect(popper).toHaveClass("react-datepicker-popper");
292-
expect(popper).toHaveClass("custom-class");
293-
expect(popper).toHaveClass("extra-class");
298+
expect(popper?.classList.contains("react-datepicker-popper")).toBe(true);
299+
expect(popper?.classList.contains("custom-class")).toBe(true);
300+
expect(popper?.classList.contains("extra-class")).toBe(true);
294301
});
295302

296303
it("should not render popper content when hidePopper is undefined (defaults to true)", () => {

src/test/year_test.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ describe("Year", () => {
3030
const yearItems = container.querySelectorAll(
3131
".react-datepicker__year-text",
3232
);
33-
const firstYear = yearItems[0].textContent;
34-
const lastYear = yearItems[11].textContent;
33+
const firstYear = yearItems[0]?.textContent;
34+
const lastYear = yearItems[11]?.textContent;
3535

3636
expect(firstYear).toBe("2017");
3737
expect(lastYear).toBe("2028");
@@ -397,7 +397,7 @@ describe("Year", () => {
397397
);
398398

399399
const year2020 = container.querySelector(".react-datepicker__year-2020");
400-
expect(year2020).toHaveClass("react-datepicker__year-text--disabled");
400+
expect(year2020?.classList.contains("react-datepicker__year-text--disabled")).toBe(true);
401401
});
402402

403403
it("should handle included dates", () => {
@@ -421,7 +421,7 @@ describe("Year", () => {
421421
);
422422

423423
const year2020 = container.querySelector(".react-datepicker__year-2020");
424-
expect(year2020).toHaveClass("react-datepicker__year-text--disabled");
424+
expect(year2020?.classList.contains("react-datepicker__year-text--disabled")).toBe(true);
425425
});
426426

427427
it("should call handleOnKeyDown when provided", () => {

0 commit comments

Comments
 (0)