[英]How to test callback function in function component in Jest
If I remove callback () => test completed success, but me need this callback for functionality如果我删除回调() =>测试完成成功,但我需要这个回调函数
Component成分
const Slider = ({slideSelected, slides}) => {
const slideClicked = slide => () => { // can't test this
slideSelected(slide);
};
const renderSlide = item => (
<Slide
slideClicked={slideClicked}
item={item}
/>
);
return (
<Carousel
data={slides}
renderItem={renderSlide}
/>
);
};
test测试
const slideSelectedFunc = jest.fn();
wrapper.setProps({
slides: [{ title: 'hello' }],
slideSelected: slideSelectedFunc,
})
const Item = sliderShallowWrapper.find('Carousel').prop('renderItem');
const itemShallowWrapper = shallow(<Item item={[{ title: 'hello' }]} />);
itemShallowWrapper.props().slideClicked();
expect(slideSelectedFunc).toHaveBeenCalled(); // error
Version Jest 24.9版本 Jest 24.9
slideClicked is not prop of your item you need to find Slide first and then you will get a correct prop. slideClicked 不是您的项目的道具,您需要先找到 Slide,然后才能获得正确的道具。
itemShallowWrapper.find("Slide").props().slideClicked();
expect(slideSelectedFunc).toHaveBeenCalled();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.