- Notifications
You must be signed in to change notification settings - Fork11
Simplify testing of redux action and async action creators
License
redux-things/redux-actions-assertions
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Assertions for redux actions testing.
This library adds assertions forredux actions testing.
It useredux-mock-store to mock redux store.
If you have not found assertion framework/library that you are using - please add comment intothis issue.
- Allows to avoid retesting nested action creators;
- Reduces repetitive code of test methods;
- Simplifies initial setup;
It allows to test only actions that need to be tested.
Example:
We have two actions (A, B). Each one makes async http requests.
Action A makes a request and if the result is successful it triggers Action B.
Action B is also used as an independent action.
Action B can be tested separately.
Therefore, we don't need to test it again in Action A.
Actions:
functionactionA(){returndispatch=>{dispatch(actionAStart());returnapi.getA().then(response=>{dispatch(actionAFinish(response));dispatch(actionB());}).catch(err=>{dispatch(actionAFailure(err));});};}functionactionB(){returndispatch=>{dispatch(actionBStart());returnapi.getB().then(response=>{dispatch(actionBFinish(response));}).catch(err=>{dispatch(actionBFailure(err));});};}
Without:
constexpectedActions=[{type:action_a_start},{type:action_a_success},{type:action_b_start},// retesting of action B{type:action_b_success}// retesting of action B];conststore=mockStore({todos:[]});store.dispatch(actionA()).then(()=>{expect(store.getActions()).toEqual(expectedActions);}).then(done).catch(done);
With:
expect(actionA()).withState({todos:[]}).toDispatch([{type:action_a_start},{type:action_a_success},actionB()// just executing tested action],done);
It reduces boilerplate of test methods and makes testing fluent.
Without:
conststore=mockStore(/* initial state */);constexpectedActions=[{type:types.FETCH_TODOS_REQUEST},/* All expected triggered action objects */];store.dispatch(fetchData()).then(()=>{constactions=store.getActions();expect(actions).toEqual(expectedActions);}).then(done).catch(done);
With:
constexpectedActions=[/*All expected triggered action objects or action creator functions*/];expect(fetchData()).toDispatchActions(expectedActions,done);
With using customised store state:
expect(fetchData()).withState({/*custom state*/}).toDispatchActions(expectedActions,done);
It provides singe-time global configuration for middlewares and initial store state.
Without:
constmiddlewares=[thunk];constmockStore=configureStore(middlewares);conststore=mockStore({/*initialstoreobject*});
With:
registerMiddlewares([thunk]);// to set custom initial stateregisterInitialStoreState(/*object of function*/);// to generate initial state of your applicationregisterInitialStoreState(buildInitialStoreState(/*your root reducer*/));
Usingnpm:
$ npm install --save-dev redux-actions-assertions// using ES6 modulesimport{registerMiddlewares}from'redux-actions-assertions';// using CommonJS modulesvarregisterMiddlewares=require('redux-actions-assertions').registerMiddlewares;// registrationregisterMiddlewares([/* Here you need to list your middlewares */]);
By using state object or function:
// using ES6 modulesimport{registerInitialStoreState}from'redux-actions-assertions';// using CommonJS modulesvarregisterInitialStoreState=require('redux-actions-assertions').registerInitialStoreState;// registrationregisterInitialStoreState(/* default initial state object or function */);
By using your root reducer:
// using ES6 modulesimport{buildInitialStoreState,registerInitialStoreState}from'redux-actions-assertions';// using CommonJS modulesvarreduxActionsAssertions=require('redux-actions-assertions');varregisterInitialStoreState=reduxActionsAssertions.registerInitialStoreState;// registrationregisterInitialStoreState(buildInitialStoreState(/* root reducer function */));
About
Simplify testing of redux action and async action creators
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.