admin管理员组文章数量:1399737
I have the following parent ponent:
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 ponent, onClick method will be fired in Parent ponent. 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?
I have the following parent ponent:
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 ponent, onClick method will be fired in Parent ponent. 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?
Share Improve this question edited May 10, 2019 at 7:24 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked May 9, 2019 at 20:31 rick1rick1 9464 gold badges18 silver badges33 bronze badges3 Answers
Reset to default 2Your onClick function is on the object itself, not the prototype. 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.
You should test its effect, for example, part of the DOM is not showing.
You could also test that they ponent's state updates correctly, that is {show: false}
the problem es from the declaration of the onClick
method, if you declare your function like this
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.
If you need to use arrow function (to get the correct value of this
), you could modify your test :
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 :
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() {
return <Child handleClick={this.onClick} />;
}
I suggest doing this with two tests:
- A unit test for the
Child
ponent that asserts itsonClick
prop is called when appropriate. - A unit test for the
Parent
that it properly sets theonClick
prop of itsChild
when rendered.
With these two tests, you will be alerted if either of these pieces fails.
版权声明:本文标题:javascript - How test callback click in child component that was passed from parent component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744188656a2594413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论