Skip to content

Commit

Permalink
Fix and test turning goToRangeStartOnSelect off
Browse files Browse the repository at this point in the history
  • Loading branch information
TimFinucane committed May 23, 2023
1 parent f56c7ae commit 64467ee
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
79 changes: 79 additions & 0 deletions src/Calendar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,85 @@ describe('Calendar', () => {

expect(onActiveStartDateChange).not.toHaveBeenCalled();
});

it('returns to start of range when range is completed', () => {
const onActiveStartDateChange = vi.fn();

const initialRange = new Date(2017, 0, 10);

const { container } = render(
<Calendar
view="month"
selectRange
onActiveStartDateChange={onActiveStartDateChange}
defaultValue={initialRange}
/>,
);

const nextMonthButton = container.querySelector(
'button.react-calendar__navigation__next-button',
) as HTMLButtonElement;

act(() => {
nextMonthButton.click();
});
// Disregard the call on the above button click
onActiveStartDateChange.mockClear();

// First day not in the previous month
const firstDayTile = container.querySelector(
'.react-calendar__tile:not([react-calendar__month-view__days__day--weekend])',
) as HTMLButtonElement;

act(() => {
firstDayTile.click();
});

expect(onActiveStartDateChange).toHaveBeenCalledWith(
expect.objectContaining({
action: 'onChange',
activeStartDate: new Date(2017, 0, 1),
view: 'month',
}),
);
});

it('does not change activeStartDate on range completion when goToRangeStartOnSelect is set to false', () => {
const onActiveStartDateChange = vi.fn();

const initialRange = new Date(2017, 0, 10);

const { container } = render(
<Calendar
view="month"
selectRange
onActiveStartDateChange={onActiveStartDateChange}
defaultValue={initialRange}
goToRangeStartOnSelect={false}
/>,
);

const nextMonthButton = container.querySelector(
'button.react-calendar__navigation__next-button',
) as HTMLButtonElement;

act(() => {
nextMonthButton.click();
});
// Disregard the call on the above button click
onActiveStartDateChange.mockClear();

// First day not in the previous month
const firstDayTile = container.querySelector(
'.react-calendar__tile:not([react-calendar__month-view__days__day--weekend])',
) as HTMLButtonElement;

act(() => {
firstDayTile.click();
});

expect(onActiveStartDateChange).not.toHaveBeenCalled();
});
});

describe('handles view change properly', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
...(this.props as CalendarPropsWithDefaults),
value: nextValue,
})
: null;
: this.activeStartDate;

event.persist();

Expand Down

0 comments on commit 64467ee

Please sign in to comment.