用酶进行反应测试:内部组分的设定值

时间:2020-08-21 10:48:38

标签: reactjs jestjs enzyme

我正在使用React组件的酶和玩笑来编写测试用例。我的react组件的定义就像

class MyComponent extends React.Component {
    
    
  // some code here
    
  showModalandSetValues () {
      //some code here
    this.confirmModal.setFormValues (values);
    
  }
    
  render (){
    const {val1 , val2} = this.props
    
    return <div>
      // some more markup
      <MyForm>
        <MyModal ref = {ref => this.confirmModal = ref}} />
      </MyForm>
    </div>
  }  
}

export default MyComponent

我正在使用浅酶法。如何在测试中设置this.confirmModal的值?它只能通过render方法设置。请指教。

1 个答案:

答案 0 :(得分:0)

they says引用未在shallow()中设置。

您可以使用mount()但可以模拟子组件来实现半浅渲染:

import MyModal from '../some/path/MyModal.js'; // to be able to .find(MyModal)

// modal is rendered as a span
jest.mock('../some/path/MyModal.js', () => (props) => <span {...props} />);

...
    const wrapper = mount(<MyComponent />);