Skip to content

Commit

Permalink
refactor: Improve Code Readability and Maintainability
Browse files Browse the repository at this point in the history
In this commit, I've made the following enhancements to the code:
1. Replaced the magic value 17 with meaning constant for clarity.
2. Instead of directly checking the disabledTimeItems.length to be 2, make sure whether it's really the value we assigned (17 or 5pm).
3. Split the test-cases and move the test-case for checking the aria-disabled attribute to a separate it block.

Closes Hacker0x01#4334
  • Loading branch information
Balaji Sridharan committed Oct 21, 2023
1 parent 55ef1a7 commit 7d07168
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions test/filter_times_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,43 @@ import { getHours } from "../src/date_utils";
import TimeComponent from "../src/time";

describe("TimeComponent", () => {
const HOUR_TO_DISABLE_IN_12_HR = 5;
const HOUR_TO_DISABLE_IN_24_HR = 17;

it("should disable times matched by filterTime prop", () => {
const { container: timeComponent } = render(
<TimeComponent filterTime={(time) => getHours(time) !== 17} />,
<TimeComponent
filterTime={(time) => getHours(time) !== HOUR_TO_DISABLE_IN_24_HR}
/>,
);

const disabledTimeItems = timeComponent.querySelectorAll(
".react-datepicker__time-list-item--disabled",
);

const disabledAllFilterTimes = Array.from(disabledTimeItems).every(
(disabledTimeItem) => {
const disabledTimeItemValue = disabledTimeItem.textContent.trim();
return (
disabledTimeItemValue.startsWith(`${HOUR_TO_DISABLE_IN_12_HR}:`) ||
disabledTimeItemValue.startsWith(`${HOUR_TO_DISABLE_IN_24_HR}:`)
);
},
);

expect(disabledAllFilterTimes).toBe(true);
});

it("should add aria-disabled to the disabled times matched by filterTime prop", () => {
const { container: timeComponent } = render(
<TimeComponent
filterTime={(time) => getHours(time) !== HOUR_TO_DISABLE_IN_24_HR}
/>,
);

const disabledTimeItems = timeComponent.querySelectorAll(
".react-datepicker__time-list-item--disabled",
);
expect(disabledTimeItems.length).toBe(2);

const allDisabledTimeItemsHaveAriaDisabled = Array.from(
disabledTimeItems,
Expand Down

0 comments on commit 7d07168

Please sign in to comment.