[英]How to test function with another function inside with Jest?
我只是开始学习测试以进行反应并在这一刻卡住了。
这是我的功能:
private handleCountyItemClick = (
e: React.FormEvent<HTMLButtonElement>
): void => {
const { countries } = this.props.countriesRed;
const selectedId: number = parseFloat(e.currentTarget.dataset.value);
const foundCountry = countries.find(
(country: Country, i: number) => i === selectedId
);
if (foundCountry) {
this.props.CountyItemClick(foundCountry);
this.props.clearInput();
this.setState({
dropdownOpen: false,
fullWidthFullHeightElHeight: 54
});
this.countriesListItems.current.style.height = "";
scrollIt(this.props.countriesRootEl.current, 300, "easeOutQuad", () =>
enableBodyScroll(this.countriesListItems.current)
);
}
};
然后,我写了这个测试,并且得到了错误:
it("should update the count by 1 when invoked by default", () => {
const wrapper = shallow(component);
const instance = wrapper.instance();
instance.handleCountyItemClick({
currentTarget: { dataset: { value: 1 } }
});
});
错误:
TypeError: _this.props.CountyItemClick is not a function
如您所见,在我的函数中,我使用Redux中的另一个函数,此错误是指Redux中的函数。 我不知道下一步该怎么做! 我需要做什么? 如何进行?
似乎您需要模拟道具。
let props;
let wrapper;
beforeEach(() => {
props = {
CountyItemClick: jest.fn(),
};
wrapper = shallow(<Component {...props} />);
});
it("should update the count by 1 when invoked by default", () => {
const wrapper = shallow(component);
const instance = wrapper.instance();
instance.handleCountyItemClick({
currentTarget: { dataset: { value: 1 } }
});
expect(props.CountyItemClick).toHaveBeenCalled();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.