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 df0a41f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
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
4 changes: 2 additions & 2 deletions src/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
this.setState(nextState, () => {
const args = {
action: nextState.action,
activeStartDate: nextState.activeStartDate || this.activeStartDate,
activeStartDate: nextState.activeStartDate || prevArgs.activeStartDate,
value: ('value' in nextState && nextState.value) || this.value,
view: ('view' in nextState && nextState.view) || this.view,
};
Expand Down 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 df0a41f

Please sign in to comment.