我有以下父组件:
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>
);
}
}
我需要检查回调是否在子组件中触发,onClick 方法将在父组件中触发。我写了这样一个测试:
test("check callback fire", () => {
let mockFn = jest.fn();
Parent.prototype.onClick = mockFn;
let wrapper = shallow(<Parent />);
wrapper.find('Child').props().handleClick();
expect(mockFn).toHaveBeenCalled();
});
我有一个错误
Expected mock function to have been called, but it was not called.
我怎样才能用笑话和酶正确地做到这一点?是否可以?