Skip to content

Commit

Permalink
♻️🧪 Refactor week_picker_test's querySelector/querySelectorAll with s…
Browse files Browse the repository at this point in the history
…afeQuerySelector/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 1ed38f3 commit 2dd05db
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/test/week_picker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { render, fireEvent } from "@testing-library/react";
import React from "react";

import DatePicker from "../index";
import { safeQuerySelector } from "./test_utils";

describe("WeekPicker", () => {
it("should change the week when clicked on any option in the picker", () => {
Expand All @@ -10,10 +11,12 @@ describe("WeekPicker", () => {
<DatePicker onChange={onChangeSpy} showWeekPicker />,
);
expect(onChangeSpy).not.toHaveBeenCalled();
fireEvent.focus(container.querySelector("input") ?? new HTMLElement());
fireEvent.click(
container.querySelector(".react-datepicker__day") ?? new HTMLElement(),
);

const input = safeQuerySelector(container, "input");
fireEvent.focus(input);

const dayElement = safeQuerySelector(container, ".react-datepicker__day");
fireEvent.click(dayElement);
expect(onChangeSpy).toHaveBeenCalled();
});

Expand All @@ -23,14 +26,15 @@ describe("WeekPicker", () => {
<DatePicker onChange={onChangeSpy} showWeekPicker showWeekNumbers />,
);
expect(onChangeSpy).not.toHaveBeenCalled();
const input = container.querySelector("input");
const input = safeQuerySelector(container, "input");
expect(input).not.toBeNull();
fireEvent.focus(input ?? new HTMLElement());
const weekNumber = container.querySelector(
fireEvent.focus(input);
const weekNumber = safeQuerySelector(
container,
".react-datepicker__week-number",
);
expect(weekNumber).not.toBeNull();
fireEvent.click(weekNumber ?? new HTMLElement());
fireEvent.click(weekNumber);
expect(onChangeSpy).toHaveBeenCalled();
});

Expand Down

0 comments on commit 2dd05db

Please sign in to comment.