redux-mock-store
A mock store for testing Redux async action creators and middleware. The mock store will create an array of dispatched actions which serve as an action log for tests.
Please note that this library is designed to test the action-related logic, not the reducer-related one. In other words, it does not update the Redux store. If you want a complex test combining actions and reducers together, take a look at other libraries (e.g., redux-actions-assertions). Refer to issue #71 for more details.
Install
npm install redux-mock-store --save-dev
Or
yarn add redux-mock-store --dev
Usage
Synchronous actions
The simplest usecase is for synchronous actions. In this example, we will test if the addTodo
action returns the right payload. redux-mock-store
saves all the dispatched actions inside the store instance. You can get all the actions by calling store.getActions()
. Finally, you can use any assertion library to test the payload.
const configureStore = //CommonJS const middlewares = const mockStore = // You would import the action from your codebase in a real scenarioconst addTodo = type: 'ADD_TODO'
Asynchronous actions
A common usecase for an asynchronous action is a HTTP request to a server. In order to test those types of actions, you will need to call store.getActions()
at the end of the request.
const middlewares = thunk // add your middlewares like `redux-thunk`const mockStore = // You would import the action from your codebase in a real scenario { return type: 'FETCH_DATA_SUCCESS' } { return { return // Some async action with promise };}
API
mockStore: Function
Configure mock store by applying the middlewares.
store: Function
Returns an instance of the configured mock store. If you want to reset your store after every test, you should call this function.
store action
Dispatches an action through the mock store. The action will be stored in an array inside the instance and executed.
store state: Object
Returns the state of the mock store.
store actions: Array
Returns the actions of the mock store.
store
Clears the stored actions.
store unsubscribe: Function
Subscribe to the store.
store
Follows the Redux API.
< 1.x.x
)
Old version (https://github.com/arnaudbenard/redux-mock-store/blob/v0.0.6/README.md
License
The MIT License