首页 > 解决方案 > 在 Enzyme 中查找 ref 进行测试

问题描述

有测试代码:

it('componentDidUpdate should mount and change props', () => {
    const onChange = jest.fn();
    const wrapper = enzyme
      .mount(
        withTestTheme(
          <JsonInput
            onChange={onChange}
            onValueChange={mockOnValueChange}
            value={exampleJsonStringValidated}
          />),
      );

    expect(wrapper.instance().JsonInputRef).toBeTruthy;
});

我有:

“组件<{}、{}、任何>”类型上不存在属性“JsonInputRef”。ts(2339)

我想找到 ref (文本区域),然后检查有效性属性以编写单元测试,这就是我需要它的原因。

渲染功能:

render() {
const { height = '', onValueChange, ...restProps } = this.props;
return (
  <StyledTextArea
    ref={this.JsonInputRef}
    {...restProps}
    onChange={this.handleValueChange}
    height={height}
  />
);
}

类似的问题,但不起作用:React/JestJS/Enzyme: How to test for reference function?

标签: reactjstypescriptjestjsenzyme

解决方案


推荐阅读