Simulate a button click in Jest

前端 未结 6 515
太阳男子
太阳男子 2020-11-30 22:49

Simulating a button click seems like a very easy/standard operation. Yet, I can\'t get it to work in Jest.js tests.

This is what I tried (and also doing it using jQue

6条回答
  •  旧时难觅i
    2020-11-30 23:08

    Solutions in accepted answer are being deprecated

    #4 Calling prop directly

    Enzyme simulate is supposed to be removed in version 4. The main maintainer is suggesting directly invoking prop functions, which is what simulate does internally. One solution is to directly test that invoking those props does the right thing; or you can mock out instance methods, test that the prop functions call them, and unit test the instance methods.

    You could call click, for example:

    wrapper.find('Button').prop('onClick')() 
    

    Or

    wrapper.find('Button').props().onClick() 
    

    Information about deprecation: Deprecation of .simulate() #2173

提交回复
热议问题