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:
also, if it's #1 then how should I test routes via react router?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With