Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integration tests for react redux redux-saga

I have a project structure like this:

app/
  global/
    styles/
    components/
  scenes/
    Home/
      actions.js
      constants.js
      index.jsx
      reducer.js
      sagas.js
      styles.styl
      index.spec.jsx
    some-other-scene/
      actions.js
      constants.js
      index.jsx
      reducer.js
      sagas.js
      styles.styl
      index.spec.jsx

so I have no problem with unit test with this structure, but I'm pretty confused as to how to structure integration test. For my unit tests I am exporting each scene component as a class

export class SomeComponent extends Component {}

and as a redux connected component

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(SomeComponent)

So for the fist style of export (the class) I am unit testing it, but for the second way (the connected component way) I am unsure how to approach this specifically how to do integration testing in react/redux. I've searched the internet about this, but nothing that is close to this structure.

So:

  1. is integration testing in react/redux/middleware (in this case redux saga) how one component integrates with redux and middleware.
  2. Or is it about how the whole app works with all the components mounted?
  3. if it's #1 does that mean each component should have a single integration test file that tests how the component integrates with redux and middleware or if it's #2 then is it one test file that tests all components as one app?

also, if it's #1 then how should I test routes via react router?

like image 561
zero Avatar asked Jul 12 '18 00:07

zero


People also ask

What is redux saga testing?

redux-saga-testing library provides a method sagaHelper that takes your generator and returns a value that works a lot like Jest's it() function, but also advances the generator being tested. The result parameter passed into the callback is the value yielded by the generater.

How do you use expectSaga?

To use expectSaga , pass in your generator function as the first argument. Pass in additional arguments which will be the arguments passed on to the generator function. expectSaga runs your saga with Redux Saga's runSaga function, so it will run just like it would in your application.

Is redux saga deprecated?

Such a powerful & elegant tool as Redux-Saga, a Redux side effect manager, is said to be deprecated, and no longer being maintained, starting from Jan 27, 2021.

What is related to unit testing in saga?

Redux saga is a very useful middleware which handles our application side effects like api calls in your react redux data flow. In simpler terms they sit between your actions and reducers and handle all asynchronous logic for you as plain redux action isn't capable of that.


Video Answer


1 Answers

In the case of your example, what we do is export mapStateToProps and mapDispatchToProps and we write unit tests for those (when they are not trivial).

I suppose you already unit test your action creators, sagas and reducers on their own.

What's left as far as integration goes? You might want to mount the components in the context of a real store, to see if they react correctly to mutations of the redux store and whether they dispatch the correct actions or not. In my project, we use end-to-end browser automation tests to verify things like this.

like image 188
Sylvain Avatar answered Oct 05 '22 20:10

Sylvain