我尝试使用react-day-picker
和enzyme
从https://react-day-picker.js.org/examples/selected-multiple测试jest
,但遗憾的是我收到了错误:TypeError: Cannot read property 'selected' of undefined
。大多数情况下,我想测试handleDayClick()
方法
我的组件
export default class MultiDayPicker extends Component {
state = {
selectedDays: []
};
handleDayClick = (day, {selected}) => {
const {selectedDays} = this.state;
if (selected) {
const selectedIndex = selectedDays.findIndex(selectedDay =>
DateUtils.isSameDay(selectedDay, day)
);
selectedDays.splice(selectedIndex, 1);
} else {
selectedDays.push(day);
}
this.setState({selectedDays});
};
render() {
return (
<DayPicker
selectedDays={this.state.selectedDays}
onDayClick={this.handleDayClick}
/>
);
}
}
我的测试:
it('should call the `handleDayClick` function', () => {
const component = shallow(<MultiDayPicker {...defaultProps} />);
expect(component.instance().handleDayClick()).toHaveBeenCalled();
});
我会感激任何想法或帮助,提前谢谢