Reputation: 91
I am trying to wrap my head around testing react components that use a redux store.
I have seen an examples use a Mock Store from redux-mock-store.
Link:
I would think that I want to test with my real store. Is there a reason why this is the case? Is it only testing the functionality of the component that doesn't need to know about the real store? But then why use any store at all?
Upvotes: 0
Views: 285
Reputation: 27
You also could test your redux (store / selectors) using the tool Check-State-Management.
It requires a bit of configuration. According to the documentation you:
checkState.config.js
file in /src
folder of your project (you need to export all your selectors from this file). Look the exampleimport * as selectors from "./checkState.config.js";
const checkStateMiddleware = (options = {}) => {
return window && window["__checkStoreExtension__"] ? window["__checkStoreExtension__"](options) :
store => next => action => next(action);
};
const store = createStore(
reducers,
compose(applyMiddleware(
checkStateMiddleware(selectors),
)),
);
Once these tasks are done you can run your application, visit it with the Chrome extension turned on. It will then prepare test-cases based on the real state of your application as you navigate and interact with it.
Upvotes: 0
Reputation: 3551
Mocking the store with tools coming from redux-mock-store
is useful for testing async actions.
Writing tests for async actions
Upvotes: 1
Reputation: 1279
Ideally your Components should be testable even without a store being present. You can just proxy the props passed and test the expected functionality. In most cases you just want to test the end contract of the component and not the actual implementation details ( actions, reducers, etc.).
This is very a very opinionated and pragmatic view point, but you can get away with having the following type of tests for your project -
Hope this helps! 😇
Upvotes: 2