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. Change the test case of finding the disabled time units count to the finding the enabled time units as the module involves on the existence of the includeTimes
2. Moved the test case to find the enabled time units through aria-disabled attribute to the separate it block
3. Replaced the magic value 45 with the actual computation which resolves to the magic value, to make it more maintainable and readable

Closes #4334
  • Loading branch information
Balaji Sridharan committed Oct 21, 2023
1 parent 7d07168 commit 7ada98d
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions test/include_times_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,54 @@ import * as utils from "../src/date_utils";
import TimeComponent from "../src/time";

describe("TimeComponent", () => {
let today, includeTimes;

beforeEach(() => {
today = utils.getStartOfDay(utils.newDate());
includeTimes = [
utils.addMinutes(today, 60),
utils.addMinutes(today, 120),
utils.addMinutes(today, 150),
];
});

it("should only enable times specified in includeTimes props", () => {
const today = utils.getStartOfDay(utils.newDate());
const { container: timeComponent } = render(
<TimeComponent
includeTimes={[
utils.addMinutes(today, 60),
utils.addMinutes(today, 120),
utils.addMinutes(today, 150),
]}
/>,
<TimeComponent includeTimes={includeTimes} />,
);

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

const allDisabledTimeItemsHaveAriaDisabled = Array.from(
disabledTimeItems,
).every((time) => time.getAttribute("aria-disabled") === "true");
expect(allDisabledTimeItemsHaveAriaDisabled).toBe(true);
const expectedDisabledTimeItems = allTimeItems.length - includeTimes.length;
expect(disabledTimeItems.length).toBe(expectedDisabledTimeItems);
});

it("should not add aria-disabled attribute on all the enabled times", () => {
const { container: timeComponent } = render(
<TimeComponent includeTimes={includeTimes} />,
);

const allTimeItems = timeComponent.querySelectorAll(
".react-datepicker__time-list-item",
);
const enabledTimeItems = Array.from(allTimeItems).filter(
(timeItem) =>
!timeItem.classList.contains(
"react-datepicker__time-list-item--disabled",
),
);

const enabledTimeItemsHasNoAriaDisabled = Array.from(
enabledTimeItems,
).every((timeItem) => {
const ariaDisabledValue = timeItem.getAttribute("aria-disabled");
return !ariaDisabledValue || ariaDisabledValue.toLowerCase() === "false";
});
expect(enabledTimeItemsHasNoAriaDisabled).toBe(true);
});
});

0 comments on commit 7ada98d

Please sign in to comment.