Skip to content

Commit

Permalink
Test: Ensure InputTime component handles specified date prop correctly
Browse files Browse the repository at this point in the history
This commit includes two test cases for the InputTime component

1. It verifies that the component correctly triggers the onChange event when a date prop is provided. The test simulates a change in time input and checks if the expected date, based on the provided date prop and new time, is passed to the onChange function.

2. It ensures that the component gracefully handles the scenario when the date prop is missing. The test simulates a change in time input and checks if the expected date, based on the current date and new time, is passed to the onChange function when no date prop is provided.

Closes Hacker0x01#4268
  • Loading branch information
Balaji Sridharan committed Oct 1, 2023
1 parent 6c9a350 commit 4121ff6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/time_input_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,49 @@ describe("timeInput", () => {
new Date(new Date().setHours(14, 0, 0, 0)),
);
});

it("should trigger onChange event with the specified date prop if available", () => {
const mockOnChange = jest.fn();
const mockDate = new Date("2023-09-30");

const timeComponent = shallow(
<InputTimeComponent date={mockDate} onChange={mockOnChange} />,
);

const newTime = "13:00";
const input = timeComponent.find("input");
input.simulate("change", { target: { value: newTime } });

const expectedDate = new Date(mockDate);
const [expectedHours, expectedMinutes] = newTime.split(":");
expectedDate.setHours(expectedHours);
expectedDate.setMinutes(expectedMinutes);

expect(mockOnChange).toHaveBeenCalledWith(expectedDate);
});

it("should trigger onChange event with the default date when date prop is missing", () => {
const mockOnChange = jest.fn();
const mockCurrentDate = new Date("2023-09-30");
const dateSpy = jest
.spyOn(global, "Date")
.mockImplementation(() => mockCurrentDate);

const timeComponent = shallow(
<InputTimeComponent onChange={mockOnChange} />,
);

const newTime = "13:00";
const input = timeComponent.find("input");
input.simulate("change", { target: { value: newTime } });

const expectedDate = new Date(mockCurrentDate);
const [expectedHours, expectedMinutes] = newTime.split(":");
expectedDate.setHours(expectedHours);
expectedDate.setMinutes(expectedMinutes);

expect(mockOnChange).toHaveBeenCalledWith(expectedDate);

dateSpy.mockRestore();
});
});

0 comments on commit 4121ff6

Please sign in to comment.