From c9c39f17d8159e2777255b0e1b1a675003b15e61 Mon Sep 17 00:00:00 2001 From: Balaji Sridharan Date: Mon, 19 Aug 2024 15:00:33 +0530 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=F0=9F=A7=AA=20Refactor=20cal?= =?UTF-8?q?endar=5Ficon's=20querySelector/querySelectorAll=20with=20safeQu?= =?UTF-8?q?erySelector/safeQuerySelectorAll=20to=20ensure=20element=20exis?= =?UTF-8?q?tence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - This change prevents tests from passing with unexpected null elements, enhancing test reliability and catching potential issues earlier. Closes #5038 --- src/test/calendar_icon.test.tsx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/test/calendar_icon.test.tsx b/src/test/calendar_icon.test.tsx index 27f5c241ec..6dd4b93e08 100644 --- a/src/test/calendar_icon.test.tsx +++ b/src/test/calendar_icon.test.tsx @@ -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; @@ -38,9 +39,12 @@ describe("CalendarIcon", () => { it("should fire onClick event when the icon is clicked", () => { const { container } = render(); - 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); }); @@ -50,9 +54,9 @@ describe("CalendarIcon", () => { , ); - 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); }); @@ -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);