How to unit test a method of react component? How to unit test a method of react component? reactjs reactjs

How to unit test a method of react component?


You are almost there. Just change your expect to this:

expect(wrapper.instance().checkBoxChecked()).equals(true);

You can go through this link to know more about testing component methods using enzyme


For those who find the accepted answer as not working, try using .dive() on your shallow wrapper before using .instance():

expect(wrapper.dive().instance().somePrivateMethod()).toEqual(true);

Reference: Testing component methods with enzyme


Extend of previous answer.If you have connected component (Redux) , try next code :

 const store=configureStore();  const context = { store };   const wrapper = shallow(      <MyComponent,      { context },    );   const inst = wrapper.dive().instance();   inst.myCustomMethod('hello');