score:22

Accepted answer

this is the case scenario you want to test. if the componentdidmount is being called, check that it has been called only once, or as many times you want.

your test. i have the explanation in comments below

    // inside your general `describe`
  let wrapper;
  const props = {
    // your props goes here..
  };
  beforeeach(() => {
    wrapper = shallow(<yourcomponent {...props} />);
  });

    it('should check `componentdidmount()`', () => {
      const instance = wrapper.instance(); // you assign your instance of the wrapper
      jest.spyon(instance, 'randomfunction'); // you spy on the randomfunction
      instance.componentdidmount();
      expect(instance.randomfunction).tohavebeencalledtimes(1); // you check if the condition you want to match is correct.
    });

you can abstract, this case to do more complex things, but the basic gist of it is the above one. if you have a more detailed or better solution, please post it. thanks!!


Related Query

More Query from same tag