How to mock React component methods with jest and enzyme

后端 未结 3 1515
渐次进展
渐次进展 2020-12-01 01:32

I have a react component(this is simplified in order to demonstrate the issue):

class MyComponent extends Component {
    handleNameInput = (value) => {
          


        
3条回答
  •  鱼传尺愫
    2020-12-01 01:49

    @Miha's answer worked with a small change:

    it('handleNameInput', () => {
      let wrapper = shallow();
      const searchDishMock = jest.fn();
      wrapper.instance().searchDish = searchDishMock;
      wrapper.update();
      wrapper.instance().handleNameInput('BoB');
      expect(searchDishMock).toBeCalledWith('BoB');
    })
    

提交回复
热议问题