Skip to content

Commit

Permalink
♻️🧪 Refactor calendar_icon's querySelector/querySelectorAll with safe…
Browse files Browse the repository at this point in the history
…QuerySelector/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 90e033e commit c9c39f1
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/test/calendar_icon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from "react";
import CalendarIcon from "../calendar_icon";

import { IconParkSolidApplication } from "./helper_components/calendar_icon";
import { safeQuerySelector } from "./test_utils";

describe("CalendarIcon", () => {
let onClickMock: jest.Mock;
Expand Down Expand Up @@ -38,9 +39,12 @@ describe("CalendarIcon", () => {
it("should fire onClick event when the icon is clicked", () => {
const { container } = render(<CalendarIcon onClick={onClickMock} />);

const icon = container.querySelector("svg.react-datepicker__calendar-icon");
const icon = safeQuerySelector(
container,
"svg.react-datepicker__calendar-icon",
);
expect(icon).not.toBeNull();
fireEvent.click(icon ?? new Element());
fireEvent.click(icon);

expect(onClickMock).toHaveBeenCalledTimes(1);
});
Expand All @@ -50,9 +54,9 @@ describe("CalendarIcon", () => {
<CalendarIcon icon="fa-example-icon" onClick={onClickMock} />,
);

const icon = container.querySelector("i.fa-example-icon");
const icon = safeQuerySelector(container, "i.fa-example-icon");
expect(icon).not.toBeNull();
fireEvent.click(icon ?? new Element());
fireEvent.click(icon);

expect(onClickMock).toHaveBeenCalledTimes(1);
});
Expand All @@ -75,8 +79,11 @@ describe("CalendarIcon", () => {
/>,
);

const icon = container.querySelector("svg.react-datepicker__calendar-icon");
fireEvent.click(icon ?? new Element());
const icon = safeQuerySelector(
container,
"svg.react-datepicker__calendar-icon",
);
fireEvent.click(icon);

expect(onClickMock).toHaveBeenCalledTimes(1);
expect(onClickCustomIcon).toHaveBeenCalledTimes(1);
Expand Down

0 comments on commit c9c39f1

Please sign in to comment.