Skip to content

Commit

Permalink
Merge branch 'main' into issue-4268/fix/retain-selected-date-on-time-…
Browse files Browse the repository at this point in the history
…change
  • Loading branch information
Balaji Sridharan committed Oct 1, 2023
2 parents 4121ff6 + b10a14a commit 2831b94
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions test/datepicker_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ function getSelectedDayNode(datePicker) {
);
}

function findSelectedDay(datePicker, targetDate) {
const days = TestUtils.scryRenderedComponentsWithType(datePicker, Day);
return days.find(
(d) =>
utils.formatDate(d.props.day, "yyyy-MM-dd") ===
utils.formatDate(targetDate, "yyyy-MM-dd"),
);
}

function goToLastMonth(datePicker) {
const lastMonthButton = TestUtils.scryRenderedDOMComponentsWithClass(
datePicker,
"react-datepicker__navigation-icon--previous",
)[0];

TestUtils.Simulate.click(ReactDOM.findDOMNode(lastMonthButton));
}

describe("DatePicker", () => {
afterEach(() => {
jest.resetAllMocks();
Expand Down Expand Up @@ -1688,7 +1706,8 @@ describe("DatePicker", () => {
const onChange = (dates = []) => {
[startDate, endDate] = dates;
};
let datePicker = TestUtils.renderIntoDocument(

const datePicker = TestUtils.renderIntoDocument(
<DatePicker
selected={selected}
onChange={onChange}
Expand All @@ -1698,12 +1717,15 @@ describe("DatePicker", () => {
inline
/>,
);
let days = TestUtils.scryRenderedComponentsWithType(datePicker, Day);
const selectedDay = days.find(
(d) =>
utils.formatDate(d.props.day, "yyyy-MM-dd") ===
utils.formatDate(selectedPrevious, "yyyy-MM-dd"),
);

let selectedDay = findSelectedDay(datePicker, selectedPrevious);
// Ensure that we're dealing with a date at the beginning of the month
if (!selectedDay) {
// If it's the beginning of the month & if the selectedPrevious is not being displayed, navigate to the previous month and reselect the selectedPrevious
goToLastMonth(datePicker);
selectedDay = findSelectedDay(datePicker, selectedPrevious);
}

TestUtils.Simulate.click(ReactDOM.findDOMNode(selectedDay));
expect(utils.formatDate(startDate, "yyyy-MM-dd")).toBe(
utils.formatDate(selectedPrevious, "yyyy-MM-dd"),
Expand Down

0 comments on commit 2831b94

Please sign in to comment.