Skip to content

Commit

Permalink
♻️🧪 Refactor day_test's querySelector/querySelectorAll with safeQuery…
Browse files Browse the repository at this point in the history
…Selector/safeQuerySelectorAll to ensure element existence

- This change prevents tests from passing with unexpected null elements, enhancing test reliability and catching potential issues earlier.

Closes Hacker0x01#5038
  • Loading branch information
Balaji Sridharan committed Aug 19, 2024
1 parent 3b6d494 commit 7daa8a8
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/test/day_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from "../date_utils";
import Day from "../day";

import { safeQuerySelector } from "./test_utils";

function renderDay(day: Date, props = {}) {
return render(
<Day day={day} month={0} handleOnKeyDown={() => {}} {...props} />,
Expand Down Expand Up @@ -1226,9 +1228,9 @@ describe("Day", () => {
onClick={onClick}
/>,
);
fireEvent.click(
container.querySelector(".react-datepicker__day") ?? new HTMLElement(),
);

const dayElement = safeQuerySelector(container, ".react-datepicker__day");
fireEvent.click(dayElement);
expect(onClickCalled).toBe(true);
});

Expand All @@ -1243,9 +1245,9 @@ describe("Day", () => {
onClick={onClick}
/>,
);
fireEvent.click(
container.querySelector(".react-datepicker__day") ?? new HTMLElement(),
);

const dayElement = safeQuerySelector(container, ".react-datepicker__day");
fireEvent.click(dayElement);
expect(onClickCalled).toBe(false);
});

Expand All @@ -1262,9 +1264,9 @@ describe("Day", () => {
onClick={onClick}
/>,
);
fireEvent.click(
container.querySelector(".react-datepicker__day") ?? new HTMLElement(),
);

const dayElement = safeQuerySelector(container, ".react-datepicker__day");
fireEvent.click(dayElement);
expect(onClickCalled).toBe(false);
});
});
Expand All @@ -1284,9 +1286,8 @@ describe("Day", () => {
/>,
);

fireEvent.mouseEnter(
container.querySelector(".react-datepicker__day") ?? new HTMLElement(),
);
const dayElement = safeQuerySelector(container, ".react-datepicker__day");
fireEvent.mouseEnter(dayElement);
expect(onMouseEnterSpy).toHaveBeenCalled();
});

Expand All @@ -1305,9 +1306,8 @@ describe("Day", () => {
/>,
);

fireEvent.pointerEnter(
container.querySelector(".react-datepicker__day") ?? new HTMLElement(),
);
const dayElement = safeQuerySelector(container, ".react-datepicker__day");
fireEvent.pointerEnter(dayElement);
expect(onMouseEnterSpy).toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 7daa8a8

Please sign in to comment.