Testing with React's Jest and Enzyme when simulated clicks call a function that calls a promise

前端 未结 2 1594
甜味超标
甜味超标 2020-11-30 11:41
  • React v15.1.0
  • Jest v12.1.1
  • Enzyme v2.3.0

I\'m trying to figure out how to test a component that calls a promise in a function invok

2条回答
  •  囚心锁ツ
    2020-11-30 12:08

    There isn't much around needing to somehow wait for the promise to fulfill before ending the test. There are two main ways of doing it from your code that I can see.

    1. independently test that onClick and your promise methods. So check that onClick calls the correct function, but spying on setTextWithPromise, triggering a click and asserting that setTextWithPromise was called. Then you can also get the component instance and call that method which returns the promise you can attach a handler and assert it did the right thing.

    2. expose a callback prop that you can pass in that is called when the promise resolves.

提交回复
热议问题