Skip to content

Commit 5a5e6d8

Browse files
committed
Fixing connectFilter to handle falsy values properly
Initial implementation by @sairion https://github.com/sairion. Added tests and the more cross-browser friendly undefined check. Fixes #371
1 parent 2d4725b commit 5a5e6d8

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/connectFilter.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ module.exports = function(listenable, key, filterFunc) {
1313
} else {
1414
// Filter initial payload from store.
1515
var result = filterFunc.call(this, listenable.getInitialState());
16-
if (result) {
17-
return _.object([key], [result]);
16+
if (typeof(result) !== "undefined") {
17+
return _.object([key], [result]);
1818
} else {
19-
return {};
19+
return {};
2020
}
2121
}
2222
},

test/usingConnectFilterMixin.spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,53 @@ describe('using the connectFilter(...) mixin',function(){
2424
});
2525
});
2626

27+
describe("when filter func returns", function() {
28+
var listenable, context, expectedResult;
29+
function withFilterReturning(val) {
30+
var key = "KEY";
31+
listenable = {
32+
listen: sinon.spy(),
33+
getInitialState: sinon.stub().returns("DOES NOT MATTER")
34+
};
35+
context = {setState: sinon.spy()};
36+
expectedResult = {};
37+
expectedResult[key] = val;
38+
_.extend(context,connectFilter(listenable, key, function() {return val;}));
39+
}
40+
describe("0", function() {
41+
beforeEach(function () {
42+
withFilterReturning(0);
43+
});
44+
it("should return that value", function() {
45+
assert.deepEqual(expectedResult, context.getInitialState());
46+
});
47+
});
48+
describe("empty string", function() {
49+
beforeEach(function () {
50+
withFilterReturning("");
51+
});
52+
it("should return that value", function() {
53+
assert.deepEqual(expectedResult, context.getInitialState());
54+
});
55+
});
56+
describe("false", function() {
57+
beforeEach(function () {
58+
withFilterReturning(false);
59+
});
60+
it("should return that value", function() {
61+
assert.deepEqual(expectedResult, context.getInitialState());
62+
});
63+
});
64+
describe("undefined", function() {
65+
beforeEach(function () {
66+
withFilterReturning();
67+
});
68+
it("should return empty object", function() {
69+
assert.deepEqual({}, context.getInitialState());
70+
});
71+
});
72+
});
73+
2774
describe("when calling without key",function(){
2875
var initialstate = "DEFAULTDATA",
2976
listenable = {

0 commit comments

Comments
 (0)