jeudi 12 juillet 2018

Testing React component method with jest && enzyme

I've encounterd a question during testing React component with jest & enzyme. I'm used to mocking and testing a function which pass to Child component as props with simulate() of enzyme.

When I tested method in Component (which is not derived from parent) with spy, an interesting thing happened. I used jest.spyOn with enzyme's instance to test Component class method. something likes this. // jest.spyOn(instance, 'Clicker') and then I tried simulate event on element for invoking the method..

Case1 below failed.

<button
      onClick={this.Clicker}
    >Click
 </button>

Case2 with Arrow function succeed.

 <button
onClick={() => this.Clicker()}
>Click</button>

Both Component method with Arrow function and function with .bind(this) on constructor have same result.

Aucun commentaire:

Enregistrer un commentaire