项目作者: redux-things

项目描述 :
Simplify testing of redux action and async action creators
高级语言: JavaScript
项目地址: git://github.com/redux-things/redux-actions-assertions.git
创建时间: 2016-04-05T12:48:44Z
项目社区:https://github.com/redux-things/redux-actions-assertions

开源协议:MIT License

下载


redux-actions-assertions

Assertions for redux actions testing.

This library adds assertions for redux actions testing.
It use redux-mock-store to mock redux store.

build status
npm version

Supported Assertion Frameworks/Libraries:

If you have not found assertion framework/library that you are using - please add comment into this issue.

What it does:

Allows to avoid retesting nested action creators

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:

  1. function actionA() {
  2. return dispatch => {
  3. dispatch(actionAStart());
  4. return api.getA().then(response => {
  5. dispatch(actionAFinish(response));
  6. dispatch(actionB());
  7. }).catch(err => {
  8. dispatch(actionAFailure(err));
  9. });
  10. };
  11. }
  12. function actionB() {
  13. return dispatch => {
  14. dispatch(actionBStart());
  15. return api.getB().then(response => {
  16. dispatch(actionBFinish(response));
  17. }).catch(err => {
  18. dispatch(actionBFailure(err));
  19. });
  20. };
  21. }

Without:

  1. const expectedActions = [
  2. { type: action_a_start },
  3. { type: action_a_success },
  4. { type: action_b_start }, // retesting of action B
  5. { type: action_b_success } // retesting of action B];
  6. const store = mockStore({ todos: [] });
  7. store.dispatch(actionA()).then(() => {
  8. expect(store.getActions()).toEqual(expectedActions);
  9. }).then(done).catch(done);

With:

  1. expect(actionA()).withState({ todos: [] }).toDispatch([
  2. { type: action_a_start },
  3. { type: action_a_success },
  4. actionB() // just executing tested action
  5. ], done);

Reduces repetitive code of test methods

It reduces boilerplate of test methods and makes testing fluent.

Without:

  1. const store = mockStore(/* initial state */);
  2. const expectedActions = [
  3. { type: types.FETCH_TODOS_REQUEST },
  4. /* All expected triggered action objects */
  5. ];
  6. store.dispatch(fetchData()).then(() => {
  7. const actions = store.getActions();
  8. expect(actions).toEqual(expectedActions);
  9. }).then(done).catch(done);

With:

  1. const expectedActions = [
  2. /*All expected triggered action objects or action creator functions*/
  3. ];
  4. expect(fetchData()).toDispatchActions(expectedActions, done);

With using customised store state:

  1. expect(fetchData()).withState({/*custom state*/}).toDispatchActions(expectedActions, done);

Simplifies initial setup

It provides singe-time global configuration for middlewares and initial store state.

Without:

  1. const middlewares = [thunk];
  2. const mockStore = configureStore(middlewares);
  3. const store = mockStore({ /*initial store object*});

With:

  1. registerMiddlewares([ thunk ]);
  2. // to set custom initial state
  3. registerInitialStoreState(/*object of function*/);
  4. // to generate initial state of your application
  5. registerInitialStoreState(buildInitialStoreState(/*your root reducer*/));

Installation

Using npm:

  1. $ npm install --save-dev redux-actions-assertions

Redux middlewares registration

  1. // using ES6 modules
  2. import { registerMiddlewares } from 'redux-actions-assertions';
  3. // using CommonJS modules
  4. var registerMiddlewares = require('redux-actions-assertions').registerMiddlewares;
  5. // registration
  6. registerMiddlewares([
  7. /* Here you need to list your middlewares */
  8. ]);

Default initial store state registration

By using state object or function:

  1. // using ES6 modules
  2. import { registerInitialStoreState } from 'redux-actions-assertions';
  3. // using CommonJS modules
  4. var registerInitialStoreState = require('redux-actions-assertions').registerInitialStoreState;
  5. // registration
  6. registerInitialStoreState(/* default initial state object or function */);

By using your root reducer:

  1. // using ES6 modules
  2. import { buildInitialStoreState, registerInitialStoreState } from 'redux-actions-assertions';
  3. // using CommonJS modules
  4. var reduxActionsAssertions = require('redux-actions-assertions');
  5. var registerInitialStoreState = reduxActionsAssertions.registerInitialStoreState;
  6. // registration
  7. registerInitialStoreState(buildInitialStoreState(/* root reducer function */));