Javascript 如何使用Jest中的另一个函数测试函数?
我刚开始学习反应测试,就停留在这一刻 以下是我的功能:Javascript 如何使用Jest中的另一个函数测试函数?,javascript,reactjs,jestjs,enzyme,Javascript,Reactjs,Jestjs,Enzyme,我刚开始学习反应测试,就停留在这一刻 以下是我的功能: private handleCountyItemClick = ( e: React.FormEvent<HTMLButtonElement> ): void => { const { countries } = this.props.countriesRed; const selectedId: number = parseFloat(e.currentTarget.dataset.value
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)
);
}
};
错误:
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();
});
let道具;
让包装纸;
在每个之前(()=>{
道具={
CountyItemClick:jest.fn(),
};
包装器=浅();
});
它(“在默认情况下调用时应将计数更新1”,()=>{
常量包装=浅层(组件);
const instance=wrapper.instance();
instance.handleCountyItemClick({
当前目标:{dataset:{value:1}}
});
期望(props.CountyItemClick).tohavebeincall();
});
看起来你需要模仿你的道具
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();
});
let道具;
让包装纸;
在每个之前(()=>{
道具={
CountyItemClick:jest.fn(),
};
包装器=浅();
});
它(“在默认情况下调用时应将计数更新1”,()=>{
常量包装=浅层(组件);
const instance=wrapper.instance();
instance.handleCountyItemClick({
当前目标:{dataset:{value:1}}
});
期望(props.CountyItemClick).tohavebeincall();
});
还有这个.props.clearInput()代码>这需要修改mocked@RobertMennell我刚刚展示了这个想法,要模仿的是authorther,还有this.props.clearInput()代码>这需要修改mocked@RobertMennell我只是展示了这个想法,要模仿的是作者这个标题需要更多。。。与这个问题有关。它实际上应该更像是在使用Jest测试React类时调用props传递的函数,这样人们才知道真正的问题是什么。这不是一个函数中的函数,它是一个实例上的函数,需要测试实例数据这篇文章的标题需要更多。。。与这个问题有关。它实际上应该更像是在使用Jest测试React类时调用props传递的函数,这样人们才知道真正的问题是什么。这不是函数中的函数,而是实例上的函数,需要测试实例数据