jambit ToiletPaper 111

Testing a saga made easy

Problem

You are currently working on a single page application with React? You use Redux for state management? You even managed to encapsulate your asynchronous work steps and side effects (API calls, access to browser caches etc.) with redux-saga? In this case, it might look like that:

Testing a saga made easy

Now, all you need is one further step to achieve mastery: how do you test the paths/cases to make sure that the thing does what it's supposed to do?

Solution

Uncle Jeremy Fairbank offers something nice – and it's called a redux-saga-test-plan. Using this toy, you can test your saga integration. I.e. within a test run, it is possible to control the saga via mock Redux actions and special providers so that it takes a certain course (and hopefully triggers an expected Redux action). Using the above saga, a special success test might look like this:

Example

  • Testing a saga made easy
  • .dispatch controls which of the "observed" actions (FETCH_USER_CONFIRMATION_START) is being reacted to.
  • Within .provide, everything that affects the saga is mocked – i.e. any selections from the Redux Application State or calls of other functions.
  • .put specifies which Redux action is expected. The test only turns green if it is actually triggered.

Further Aspects

---

Author: Robert Gruner / Software Engineer / Office Leipzig

Download Toilet Paper #111: Testing a saga made easy (pdf)

Testing a saga made easy

Wir verwenden Cookies, um unsere Webseite für Sie zu optimieren. Mit dem Besuch unserer Webseite erklären Sie sich damit einverstanden. // Our website is using cookies to improve your experience. By continuing to browse the site, you are agreeing to our use of cookies.

Weitere Informationen finden Sie in unserer Datenschutzerklärung. // For more information, please refer to our privacy policy.