在Jest中编写测试时Ref为null

时间:2019-09-27 08:54:00

标签: javascript reactjs jestjs enzyme

我有完整的源代码,但是在编写单元测试时却遇到了问题,我该如何创建引用:

private JsonInputRef: React.RefObject<HTMLTextAreaElement> = React.createRef();

我如何使用它:

if (errorMessage == null) {
  if (this.JsonInputRef.current) {
    this.JsonInputRef.current.setCustomValidity('');
  }
  onValueChange && onValueChange(value, e);
} else {
  if (this.JsonInputRef.current) {
    this.JsonInputRef.current.setCustomValidity(errorMessage);
  }
}

和问题,从测试范围报告:

enter image description here

我在哪里叫裁判:

render() {
// to exclude unknown property 'onValueChange' for JsonInput for DevTools
const { height = '', onValueChange, ...restProps } = this.props;
  return (
    <StyledTextArea
      ref={this.JsonInputRef}
      {...restProps}
      onChange={this.handleValueChange}
      height={height}
    />
  );
}

我得出结论,this.JsonRef.currentnull,但如何对其进行测试或适当重构以具有完整的测试范围?

0 个答案:

没有答案