我正在尝试测试组件内部的按钮组件,但是我不知道如何将模拟Fn传递给childComponent
我可以使用浅层的组件来获取ParentComponent,然后找到子组件,但是如何传递模拟函数呢?
<ParentComponent>
<ChildComponent onPress={this.myFunction}/>
</ParentComponent>
const mockFunction = jest.fn();
describe("<ParentComponent> behaviour", () => {
it("should call onSubmit", () => {
const wrapper = shallow(<ParentComponent />);
//How pass the mockFn here
wrapper.find(ChildComponent).simulate('click');
expect(mockFunction).toHaveBeenCalled();
我该怎么做?
答案 0 :(得分:0)
我想您应该直接在子组件中对此进行测试,如下所示:
const mockFunction = jest.fn();
describe("<ParentComponent> behaviour", () => {
it("should call onSubmit", () => {
const wrapper = shallow(<ChildComponent onPress={mockFunction} />);
wrapper.simulate('click');
expect(mockFunction).toHaveBeenCalled();
}
答案 1 :(得分:0)
<ParentComponent
text = {'hey There'}>
<ChildComponent
alert(this.props.text)
onPress={this.myFunction}/>
</ParentComponent>