我有完整的源代码,但是在编写单元测试时却遇到了问题,我该如何创建引用:
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);
}
}
和问题,从测试范围报告:
我在哪里叫裁判:
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.current
是null
,但如何对其进行测试或适当重构以具有完整的测试范围?