Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4334: Improved Test Cases Readability and Maintainability #4335

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions test/exclude_times_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,37 @@ import { setTime, newDate } from "../src/date_utils";
import DatePicker from "../src/index.jsx";

describe("DatePicker", () => {
let now, excludeTimes;

beforeEach(() => {
now = newDate();
excludeTimes = [
setTime(now, { hour: 17, minute: 0 }),
setTime(now, { hour: 18, minute: 30 }),
setTime(now, { hour: 19, minute: 30 }),
setTime(now, { hour: 17, minute: 30 }),
];
});

it("should disable times specified in excludeTimes props", () => {
const now = newDate();
const { container: datePicker } = render(
<DatePicker open showTimeSelect excludeTimes={excludeTimes} />,
);

const disabledTimeItems = datePicker.querySelectorAll(
".react-datepicker__time-list-item--disabled",
);
expect(disabledTimeItems.length).toBe(excludeTimes.length);
});

it("should add aria-disabled to all the excluded times", () => {
const { container: datePicker } = render(
<DatePicker
open
showTimeSelect
excludeTimes={[
setTime(now, { hour: 17, minute: 0 }),
setTime(now, { hour: 18, minute: 30 }),
setTime(now, { hour: 19, minute: 30 }),
setTime(now, { hour: 17, minute: 30 }),
]}
/>,
<DatePicker open showTimeSelect excludeTimes={excludeTimes} />,
);

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

const allDisabledTimeItemsHaveAriaDisabled = Array.from(
disabledTimeItems,
Expand Down
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
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);
});
});
Loading