Skip to content

Commit a9115aa

Browse files
Merge pull request #6 from amit1911/feature/ignore-invalid-action
Ignore invalid actions
2 parents 081d65a + 0d4acf7 commit a9115aa

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@
3232
"babel-preset-es2015": "^6.1.18",
3333
"sinon": "^1.17.2",
3434
"tape": "^4.2.2"
35+
},
36+
"dependencies": {
37+
"lodash.isplainobject": "^4.0.6"
3538
}
3639
}

src/index.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import defaultErrorHandler from "./default-error-handler";
2+
import isPlainObject from "lodash.isplainobject";
23

34
export default function reduxUnhandledAction(callback = defaultErrorHandler) {
4-
return ({getState}) => (next) => (action) => {
5-
const prevState = getState();
6-
const result = next(action);
7-
const nextState = getState();
8-
if (prevState === nextState) {
9-
callback(action);
10-
}
11-
return result;
12-
};
13-
5+
return ({ getState }) => (next) => (action) => {
6+
if (isPlainObject(action) && typeof action.type !== "undefined") {
7+
const prevState = getState();
8+
const result = next(action);
9+
const nextState = getState();
10+
if (prevState === nextState) {
11+
callback(action);
12+
}
13+
return result;
14+
}
15+
return next(action);
16+
};
1417
}

test/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,21 @@ test("should not error when state is properly updated", function(assert) {
6262
assert.notOk(consoleError.called, "console error is not logged when a new state is returned");
6363
assert.end();
6464
});
65+
66+
test("should not call callback if action is not a plain object", function(assert) {
67+
var action = [];
68+
var callbackSpy = sinon.spy();
69+
var actionHandler = unhandledAction(callbackSpy)({getState: getState})(spy)(action);
70+
assert.equal(callbackSpy.callCount, 0, "callback is not called when action is not a plain object");
71+
assert.ok(spy.calledWith(action), "next called with action when action is not a plain object");
72+
assert.end();
73+
});
74+
75+
test("should not call callback if action doesn't have type property", function(assert) {
76+
var action = {};
77+
var callbackSpy = sinon.spy();
78+
var actionHandler = unhandledAction(callbackSpy)({getState: getState})(spy)(action);
79+
assert.equal(callbackSpy.callCount, 0, "callback is not called when action doesn't have type property");
80+
assert.ok(spy.calledWith(action), "next called with action when action doesn't have type property");
81+
assert.end();
82+
});

0 commit comments

Comments
 (0)