Skip to content

Commit

Permalink
✅ Update the test case of WeekNumber component's onKeyDown event to i…
Browse files Browse the repository at this point in the history
…mprove the coverage
  • Loading branch information
Balaji Sridharan committed Jul 18, 2024
1 parent 08cdae9 commit 68a38d2
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions src/test/week_number_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,52 @@ describe("WeekNumber", () => {
});

describe("handleOnKeyDown", () => {
const handleOnKeyDownMock = jest.fn((event) => {
if (event.key === KeyType.Space) {
event.preventDefault();
event.key = KeyType.Enter;
}
it("should not change any other key", () => {
const onKeyDownMock = jest.fn();

const container = renderWeekNumber(1, {
handleOnKeyDown: onKeyDownMock,
});

const weekNumberElement = container.querySelector(
".react-datepicker__week-number",
)!;

fireEvent.keyDown(weekNumberElement, {
key: KeyType.Enter,
preventDefault: jest.fn(),
});

expect(onKeyDownMock).toHaveBeenCalledTimes(1);
expect(onKeyDownMock).toHaveBeenCalledWith(
expect.objectContaining({
key: KeyType.Enter,
}),
);
});

it("should change space key to Enter", () => {
const eventSpace = {
const onKeyDownMock = jest.fn();

const container = renderWeekNumber(1, {
handleOnKeyDown: onKeyDownMock,
});

const weekNumberElement = container.querySelector(
".react-datepicker__week-number",
)!;

fireEvent.keyDown(weekNumberElement, {
key: KeyType.Space,
preventDefault: jest.fn(),
};
handleOnKeyDownMock(eventSpace);
expect(eventSpace.preventDefault).toHaveBeenCalled();
expect(eventSpace.key).toBe(KeyType.Enter);
});
});

it("should not change any other key", () => {
const eventA = {
key: "a",
};
handleOnKeyDownMock(eventA);
expect(eventA.key).toBe("a");
expect(onKeyDownMock).toHaveBeenCalledTimes(1);
expect(onKeyDownMock).toHaveBeenCalledWith(
expect.objectContaining({
key: KeyType.Enter,
}),
);
});
});
});
Expand Down

0 comments on commit 68a38d2

Please sign in to comment.