[英]How test callback click in child component that was passed from parent component?
I have the following parent component: 我有以下父组件:
import React, { Fragment } from 'react';
export class Parent extends Component {
constructor(props) {
super(props);
this.state = {
show: false,
};
}
onClick = () => {
// working
}
render() {
return (
<Fragment>
<Child handleClick={this.onClick} />
</Fragment>
);
}
}
I need to check if callback fired in Child component, onClick method will be fired in Parent component. 我需要检查子组件中是否触发了回调,父组件中的onClick方法是否将被触发。 I wrote such a test:
我写了这样的测试:
test("check callback fire", () => {
let mockFn = jest.fn();
Parent.prototype.onClick = mockFn;
let wrapper = shallow(<Parent />);
wrapper.find('Child').props().handleClick();
expect(mockFn).toHaveBeenCalled();
});
and I got an error 我有一个错误
Expected mock function to have been called, but it was not called.
How can I do that properly with jest and enzyme? 我该如何用笑话和酵素正确地做到这一点? Is it possible?
可能吗?
Your onClick function is on the object itself, not the prototype. 您的onClick函数位于对象本身而不是原型上。 You have to modify the object instance.
您必须修改对象实例。
let mockFn = jest.fn();
let wrapper = shallow(<Parent />);
wrapper.find('Child').props().handleClick = mockFn;
wrapper.find('Child').props().handleClick();
expect(mockFn).toHaveBeenCalled();
Having said that, you are calling onClick yourself, so it doesn't make sense to test that it was called. 话虽如此,您是自己调用onClick,因此测试它是否被调用没有意义。
You should test its effect, for example, part of the DOM is not showing. 您应该测试其效果,例如,部分DOM未显示。
You could also test that they component's state updates correctly, that is {show: false} 您还可以测试它们组件的状态是否正确更新,即{show:false}
the problem comes from the declaration of the onClick
method, if you declare your function like this 问题来自
onClick
方法的声明,如果您这样声明函数
onClick() {}
instead of 代替
onClick = () => {}
your test should work because onClick
will be present in the Parent.prototype
object, note that this
keyword in the onClick
method will no longer refers to the class instance. 您的测试应该可以进行,因为
onClick
将出现在Parent.prototype
对象中,请注意, onClick
方法中的this
关键字将不再引用类实例。
If you need to use arrow function (to get the correct value of this
), you could modify your test : 如果您需要使用箭头功能(以获取
this
的正确值),则可以修改测试:
test("check callback fire", () => {
let mockFn = jest.fn();
let wrapper = shallow(<Parent />);
wrapper.instance().onClick = mockFn;
wrapper.find('Child').props().handleClick();
expect(mockFn).toHaveBeenCalled();
});
Using onClick = () => {}
in the Parent
class will not add the property to Parent.prototype
, in fact it will declare an instance variable like this : 在
Parent
类中使用onClick = () => {}
不会将属性添加到Parent.prototype
,实际上,它将声明一个实例变量,如下所示:
class Parent {
constructor() {
super();
this.onClick = () => {}
}
}
By the way, the presence of Fragment
inside your render
method seems to be useless, you could remove it 顺便说一句,您的
render
方法中存在Fragment
似乎没有用,您可以将其删除
render() {
return <Child handleClick={this.onClick} />;
}
I suggest doing this with two tests: 我建议通过两个测试来做到这一点:
Child
component that asserts its onClick
prop is called when appropriate. onClick
道具的Child
组件的单元测试。 Parent
that it properly sets the onClick
prop of its Child
when rendered. Parent
级进行单元测试,以使其在渲染时正确设置其Child
的onClick
道具。 With these two tests, you will be alerted if either of these pieces fails. 通过这两个测试,如果其中任何一个失败,您都会收到警报。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.