简体   繁体   English

如何测试回调单击从父组件传递的子组件?

[英]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: 我建议通过两个测试来做到这一点:

  1. A unit test for the Child component that asserts its onClick prop is called when appropriate. 适当时将调用断言其onClick道具的Child组件的单元测试。
  2. A unit test for the Parent that it properly sets the onClick prop of its Child when rendered. Parent级进行单元测试,以使其在渲染时正确设置其ChildonClick道具。

With these two tests, you will be alerted if either of these pieces fails. 通过这两个测试,如果其中任何一个失败,您都会收到警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当单击从父组件传递时,为什么子组件未定义“this”? - Why 'this' is undefined for child component, when click is passed from a parent component? React中的回调参数如何从子组件传递到父组件? - How is an argument for a callback in React being passed from the child component to the parent component? 如何在React Native中从子组件回调到父组件 - How to get callback from child component to parent component in React Native React中从父组件传递给子组件的“未定义”? - 'Undefined' passed from parent to child component in React? 未定义的数据从子组件传递到父组件 - Data passed as undefined from child to parent component 在父组件测试文件中模拟子组件元素点击 - simulate child component element click in parent component test file 虽然子组件道具是从父组件传递的,但它们是空的 - Child component props are empty though they're passed from a parent component vue 3访问从父组件传递的未在子组件中定义的道具 - vue 3 access props passed from parent component that are not defined in child component React 子组件不会更新从父组件传递的属性 - React Child Component does not update properties passed from Parent Component 父组件的实例如何传递到子组件的构造函数中? - How is an instance of a parent component passed into a child component constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM