Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simplify testing of redux action and async action creators

License

NotificationsYou must be signed in to change notification settings

redux-things/redux-actions-assertions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Assertions for redux actions testing.

This library adds assertions forredux actions testing.
It useredux-mock-store to mock redux store.

build statusnpm version

Supported Assertion Frameworks/Libraries:

If you have not found assertion framework/library that you are using - please add comment intothis 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:

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);

Reduces repetitive code of test methods

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);

Simplifies initial setup

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*/));

Installation

Usingnpm:

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

Redux middlewares registration

// using ES6 modulesimport{registerMiddlewares}from'redux-actions-assertions';// using CommonJS modulesvarregisterMiddlewares=require('redux-actions-assertions').registerMiddlewares;// registrationregisterMiddlewares([/* Here you need to list your middlewares */]);

Default initial store state registration

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 */));

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp