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
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit6f9ac00

Browse files
committed
Reducer: Throw when subscription handler returns undefined. Provide tests.
1 parent688791f commit6f9ac00

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

‎src/Reducer.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ export default class Reducer {
3838
return(state,action)=>{
3939
this.subscriptions.forEach((subscription)=>{
4040
const{action:subscribedAction}=subscription;
41+
const{type:dispatchedType}=action;
4142

4243
constshouldResolve=(subscribedActioninstanceofRegExp)
43-
?subscribedAction.test(action.type)
44-
:(subscribedAction===action.type);
44+
?subscribedAction.test(dispatchedType)
45+
:(subscribedAction===dispatchedType);
4546

4647
if(shouldResolve){
47-
this.state=subscription.resolver(this.state,fromJS(action),this.context);
48+
constnextState=subscription.resolver(this.state,fromJS(action),this.context);
49+
50+
if(!nextState){
51+
thrownewError(`Expected reducer to return the next state, but got:${nextState}. Check the return statement for the "${dispatchedType}" subscription.`);
52+
}
53+
54+
this.state=nextState;
4855
}
4956
});
5057

‎test/Reducer.spec.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ reducer.subscribe('ADD_COMMENT', (state, action, context) => {
1515
/* Create a new Redux store */
1616
conststore=createStore(reducer.toFunction());
1717

18-
describe('Reducer',()=>{
18+
describe('General',()=>{
1919
it('Library exports are fine',()=>{
2020
returnexpect(Reducer).to.not.be.undefined;
2121
});
22+
});
2223

24+
describe('Reducer',()=>{
2325
/**
2426
* Initial state passed to the method should propagate to Redux state.
2527
*/
@@ -59,6 +61,15 @@ describe('Reducer', () => {
5961
expect(state.get('comments').get(1)).to.equal('abc');
6062
});
6163

64+
it('Throw on subscription not returning the next state',()=>{
65+
constreducer=newReducer({prop:true});
66+
reducer.subscribe('ACTION_ONE',()=>{});
67+
68+
conststore=createStore(reducer.toFunction());
69+
70+
expect(store.dispatch.bind(this,{type:'ACTION_ONE'})).to.throw;
71+
});
72+
6273
it('Context is shared between different subscriptions',()=>{
6374
constreducer=newReducer({someProp:true});
6475

@@ -77,7 +88,7 @@ describe('Reducer', () => {
7788
reducer.subscribe('ACTION_THREE',(state,action,context)=>{
7889
expect(context).to.not.have.property('firstProp');
7990
expect(context).to.have.property('secondProp','poo');
80-
returnstate;
91+
//return state;
8192
});
8293

8394
conststore=createStore(reducer.toFunction());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp