Skip to content

Commit d4617be

Browse files
fix: resolve all eslint and typescript linting errors in tests
Fixed 22 linting errors across test files: TypeScript errors: - Replaced `as any` with `as unknown as Date` for null date casting in date_utils_critical.test.ts (2 errors fixed) Jest conditional expect errors: - Replaced conditional expects inside `if` blocks with assertions that verify element existence before the conditional code - Used `expect(elements.length).toBeGreaterThan(0/1/2)` pattern - Applied fixes across 5 test files: * datepicker_test.test.tsx (8 errors) * portal.test.tsx (1 error) * timepicker_test.test.tsx (5 errors) * year_picker_test.test.tsx (5 errors) All tests still passing with 98.78% statement coverage maintained. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 8805f67 commit d4617be

File tree

5 files changed

+114
-137
lines changed

5 files changed

+114
-137
lines changed

src/test/date_utils_critical.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe("date_utils critical functions coverage", () => {
106106
const endDate = new Date("2024-01-20");
107107

108108
// Should handle null gracefully
109-
const result = isDayInRange(testDate, null as any, endDate);
109+
const result = isDayInRange(testDate, null as unknown as Date, endDate);
110110

111111
// Depending on implementation, this might throw or return false
112112
expect(typeof result).toBe("boolean");
@@ -117,7 +117,7 @@ describe("date_utils critical functions coverage", () => {
117117
const startDate = new Date("2024-01-10");
118118

119119
// Should handle null gracefully
120-
const result = isDayInRange(testDate, startDate, null as any);
120+
const result = isDayInRange(testDate, startDate, null as unknown as Date);
121121

122122
// Depending on implementation, this might throw or return false
123123
expect(typeof result).toBe("boolean");

src/test/datepicker_test.test.tsx

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5250,13 +5250,12 @@ describe("DatePicker", () => {
52505250
".react-datepicker__time-list-item",
52515251
);
52525252

5253-
if (timeElements.length > 0) {
5254-
const firstTimeElement = timeElements[0] as HTMLElement;
5255-
// Line 942: handleTimeChange early return for selectsRange
5256-
fireEvent.click(firstTimeElement);
5257-
// Time change should not affect range selection directly
5258-
expect(container.querySelector(".react-datepicker")).not.toBeNull();
5259-
}
5253+
expect(timeElements.length).toBeGreaterThan(0);
5254+
const firstTimeElement = timeElements[0] as HTMLElement;
5255+
// Line 942: handleTimeChange early return for selectsRange
5256+
fireEvent.click(firstTimeElement);
5257+
// Time change should not affect range selection directly
5258+
expect(container.querySelector(".react-datepicker")).not.toBeNull();
52605259
});
52615260

52625261
it("should handle handleTimeChange with selectsMultiple (line 942)", () => {
@@ -5275,12 +5274,11 @@ describe("DatePicker", () => {
52755274
".react-datepicker__time-list-item",
52765275
);
52775276

5278-
if (timeElements.length > 0) {
5279-
const firstTimeElement = timeElements[0] as HTMLElement;
5280-
// Line 942: handleTimeChange early return for selectsMultiple
5281-
fireEvent.click(firstTimeElement);
5282-
expect(container.querySelector(".react-datepicker")).not.toBeNull();
5283-
}
5277+
expect(timeElements.length).toBeGreaterThan(0);
5278+
const firstTimeElement = timeElements[0] as HTMLElement;
5279+
// Line 942: handleTimeChange early return for selectsMultiple
5280+
fireEvent.click(firstTimeElement);
5281+
expect(container.querySelector(".react-datepicker")).not.toBeNull();
52845282
});
52855283

52865284
it("should handle adjustDateOnChange in setSelected (line 1044)", () => {
@@ -5300,13 +5298,13 @@ describe("DatePicker", () => {
53005298
const calendar = container.querySelector(".react-datepicker");
53015299
const days = calendar?.querySelectorAll(".react-datepicker__day");
53025300

5303-
if (days && days.length > 0) {
5304-
const firstDay = days[0] as HTMLElement;
5305-
fireEvent.click(firstDay);
5301+
expect(days).toBeDefined();
5302+
expect(days!.length).toBeGreaterThan(0);
5303+
const firstDay = days![0] as HTMLElement;
5304+
fireEvent.click(firstDay);
53065305

5307-
// Line 1044: adjustDateOnChange should adjust the date to minDate
5308-
expect(onChange).toHaveBeenCalled();
5309-
}
5306+
// Line 1044: adjustDateOnChange should adjust the date to minDate
5307+
expect(onChange).toHaveBeenCalled();
53105308
});
53115309

53125310
it("should handle onDayMouseEnter with selectsRange and keyboard (lines 1210-1211)", () => {
@@ -5325,18 +5323,17 @@ describe("DatePicker", () => {
53255323
".react-datepicker__day:not(.react-datepicker__day--disabled)",
53265324
);
53275325

5328-
if (days.length > 2) {
5329-
const firstDay = days[0] as HTMLElement;
5330-
const secondDay = days[1] as HTMLElement;
5326+
expect(days.length).toBeGreaterThan(2);
5327+
const firstDay = days[0] as HTMLElement;
5328+
const secondDay = days[1] as HTMLElement;
53315329

5332-
// Simulate keyboard selection start
5333-
fireEvent.keyDown(firstDay, { key: "Enter" });
5330+
// Simulate keyboard selection start
5331+
fireEvent.keyDown(firstDay, { key: "Enter" });
53345332

5335-
// Lines 1210-1211: onDayMouseEnter with keyboard selection
5336-
fireEvent.mouseEnter(secondDay);
5333+
// Lines 1210-1211: onDayMouseEnter with keyboard selection
5334+
fireEvent.mouseEnter(secondDay);
53375335

5338-
expect(secondDay).not.toBeNull();
5339-
}
5336+
expect(secondDay).not.toBeNull();
53405337
});
53415338

53425339
it("should handle onYearMouseEnter with selectsRange (line 1353)", () => {
@@ -5356,18 +5353,17 @@ describe("DatePicker", () => {
53565353
".react-datepicker__year-text",
53575354
);
53585355

5359-
if (yearElements.length > 1) {
5360-
const firstYear = yearElements[0] as HTMLElement;
5361-
const secondYear = yearElements[1] as HTMLElement;
5356+
expect(yearElements.length).toBeGreaterThan(1);
5357+
const firstYear = yearElements[0] as HTMLElement;
5358+
const secondYear = yearElements[1] as HTMLElement;
53625359

5363-
// Start range selection
5364-
fireEvent.click(firstYear);
5360+
// Start range selection
5361+
fireEvent.click(firstYear);
53655362

5366-
// Line 1353: onYearMouseEnter with selectsRange
5367-
fireEvent.mouseEnter(secondYear);
5363+
// Line 1353: onYearMouseEnter with selectsRange
5364+
fireEvent.mouseEnter(secondYear);
53685365

5369-
expect(secondYear).not.toBeNull();
5370-
}
5366+
expect(secondYear).not.toBeNull();
53715367
});
53725368

53735369
it("should handle onMonthMouseLeave with selectsRange (line 1358)", () => {
@@ -5387,15 +5383,14 @@ describe("DatePicker", () => {
53875383
".react-datepicker__month-text",
53885384
);
53895385

5390-
if (monthElements.length > 0) {
5391-
const firstMonth = monthElements[0] as HTMLElement;
5386+
expect(monthElements.length).toBeGreaterThan(0);
5387+
const firstMonth = monthElements[0] as HTMLElement;
53925388

5393-
// Line 1358: onMonthMouseLeave with selectsRange
5394-
fireEvent.mouseEnter(firstMonth);
5395-
fireEvent.mouseLeave(firstMonth);
5389+
// Line 1358: onMonthMouseLeave with selectsRange
5390+
fireEvent.mouseEnter(firstMonth);
5391+
fireEvent.mouseLeave(firstMonth);
53965392

5397-
expect(firstMonth).not.toBeNull();
5398-
}
5393+
expect(firstMonth).not.toBeNull();
53995394
});
54005395

54015396
it("should handle onQuarterMouseLeave with selectsRange (line 1363)", () => {
@@ -5415,15 +5410,14 @@ describe("DatePicker", () => {
54155410
".react-datepicker__quarter-text",
54165411
);
54175412

5418-
if (quarterElements.length > 0) {
5419-
const firstQuarter = quarterElements[0] as HTMLElement;
5413+
expect(quarterElements.length).toBeGreaterThan(0);
5414+
const firstQuarter = quarterElements[0] as HTMLElement;
54205415

5421-
// Line 1363: onQuarterMouseLeave with selectsRange
5422-
fireEvent.mouseEnter(firstQuarter);
5423-
fireEvent.mouseLeave(firstQuarter);
5416+
// Line 1363: onQuarterMouseLeave with selectsRange
5417+
fireEvent.mouseEnter(firstQuarter);
5418+
fireEvent.mouseLeave(firstQuarter);
54245419

5425-
expect(firstQuarter).not.toBeNull();
5426-
}
5420+
expect(firstQuarter).not.toBeNull();
54275421
});
54285422

54295423
it("should handle onYearMouseLeave with selectsRange (line 1368)", () => {
@@ -5443,15 +5437,14 @@ describe("DatePicker", () => {
54435437
".react-datepicker__year-text",
54445438
);
54455439

5446-
if (yearElements.length > 0) {
5447-
const firstYear = yearElements[0] as HTMLElement;
5440+
expect(yearElements.length).toBeGreaterThan(0);
5441+
const firstYear = yearElements[0] as HTMLElement;
54485442

5449-
// Line 1368: onYearMouseLeave with selectsRange
5450-
fireEvent.mouseEnter(firstYear);
5451-
fireEvent.mouseLeave(firstYear);
5443+
// Line 1368: onYearMouseLeave with selectsRange
5444+
fireEvent.mouseEnter(firstYear);
5445+
fireEvent.mouseLeave(firstYear);
54525446

5453-
expect(firstYear).not.toBeNull();
5454-
}
5447+
expect(firstYear).not.toBeNull();
54555448
});
54565449

54575450
it("should handle Tab key in date range mode (line 1191)", () => {

src/test/portal.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ describe("Portal", () => {
7070

7171
// The portal root should still exist but the content should be removed
7272
const stillExists = document.getElementById("test-portal-4");
73-
if (stillExists) {
74-
expect(stillExists.querySelector(".cleanup-test")).toBeNull();
75-
}
73+
expect(stillExists).toBeTruthy();
74+
expect(stillExists!.querySelector(".cleanup-test")).toBeNull();
7675
});
7776

7877
it("should render to portalHost when provided", () => {

src/test/timepicker_test.test.tsx

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,11 @@ describe("TimePicker", () => {
471471
".react-datepicker__time-list-item--disabled",
472472
);
473473

474-
if (timeList.length > 0) {
475-
// Line 133: early return when clicking disabled time
476-
const disabledItem = timeList[0] as HTMLElement;
477-
fireEvent.click(disabledItem);
478-
expect(onChange).not.toHaveBeenCalled();
479-
} else {
480-
// Fallback if no disabled times are found
481-
expect(
482-
container.querySelector(".react-datepicker__time"),
483-
).not.toBeNull();
484-
}
474+
expect(timeList.length).toBeGreaterThan(0);
475+
// Line 133: early return when clicking disabled time
476+
const disabledItem = timeList[0] as HTMLElement;
477+
fireEvent.click(disabledItem);
478+
expect(onChange).not.toHaveBeenCalled();
485479
});
486480

487481
it("should handle keyboard navigation in time list with ArrowUp", () => {
@@ -499,14 +493,13 @@ describe("TimePicker", () => {
499493
".react-datepicker__time-list-item",
500494
);
501495

502-
if (timeItems.length > 1) {
503-
const secondItem = timeItems[1] as HTMLElement;
496+
expect(timeItems.length).toBeGreaterThan(1);
497+
const secondItem = timeItems[1] as HTMLElement;
504498

505-
// Lines 190-191: ArrowUp navigation with previousSibling
506-
fireEvent.keyDown(secondItem, { key: "ArrowUp" });
499+
// Lines 190-191: ArrowUp navigation with previousSibling
500+
fireEvent.keyDown(secondItem, { key: "ArrowUp" });
507501

508-
expect(timeItems[0]).not.toBeNull();
509-
}
502+
expect(timeItems[0]).not.toBeNull();
510503
});
511504

512505
it("should handle keyboard navigation in time list with ArrowDown", () => {
@@ -524,14 +517,13 @@ describe("TimePicker", () => {
524517
".react-datepicker__time-list-item",
525518
);
526519

527-
if (timeItems.length > 1) {
528-
const firstItem = timeItems[0] as HTMLElement;
520+
expect(timeItems.length).toBeGreaterThan(1);
521+
const firstItem = timeItems[0] as HTMLElement;
529522

530-
// Lines 199-200: ArrowDown navigation with nextSibling
531-
fireEvent.keyDown(firstItem, { key: "ArrowDown" });
523+
// Lines 199-200: ArrowDown navigation with nextSibling
524+
fireEvent.keyDown(firstItem, { key: "ArrowDown" });
532525

533-
expect(timeItems[1]).not.toBeNull();
534-
}
526+
expect(timeItems[1]).not.toBeNull();
535527
});
536528

537529
it("should handle keyboard navigation with ArrowLeft", () => {
@@ -549,14 +541,13 @@ describe("TimePicker", () => {
549541
".react-datepicker__time-list-item",
550542
);
551543

552-
if (timeItems.length > 1) {
553-
const secondItem = timeItems[1] as HTMLElement;
544+
expect(timeItems.length).toBeGreaterThan(1);
545+
const secondItem = timeItems[1] as HTMLElement;
554546

555-
// ArrowLeft should behave like ArrowUp
556-
fireEvent.keyDown(secondItem, { key: "ArrowLeft" });
547+
// ArrowLeft should behave like ArrowUp
548+
fireEvent.keyDown(secondItem, { key: "ArrowLeft" });
557549

558-
expect(timeItems[0]).not.toBeNull();
559-
}
550+
expect(timeItems[0]).not.toBeNull();
560551
});
561552

562553
it("should handle keyboard navigation with ArrowRight", () => {
@@ -574,14 +565,13 @@ describe("TimePicker", () => {
574565
".react-datepicker__time-list-item",
575566
);
576567

577-
if (timeItems.length > 1) {
578-
const firstItem = timeItems[0] as HTMLElement;
568+
expect(timeItems.length).toBeGreaterThan(1);
569+
const firstItem = timeItems[0] as HTMLElement;
579570

580-
// ArrowRight should behave like ArrowDown
581-
fireEvent.keyDown(firstItem, { key: "ArrowRight" });
571+
// ArrowRight should behave like ArrowDown
572+
fireEvent.keyDown(firstItem, { key: "ArrowRight" });
582573

583-
expect(timeItems[1]).not.toBeNull();
584-
}
574+
expect(timeItems[1]).not.toBeNull();
585575
});
586576
});
587577
});

0 commit comments

Comments
 (0)