Permalink
Browse files

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
  • Loading branch information...
spoike committed Jul 18, 2015
1 parent 2d4725b commit 5a5e6d81a88cabc5e65bce58fded77883ac27bcf
Showing with 50 additions and 3 deletions.
  1. +3 −3 src/connectFilter.js
  2. +47 −0 test/usingConnectFilterMixin.spec.js
View
@@ -13,10 +13,10 @@ module.exports = function(listenable, key, filterFunc) {
} else {
// Filter initial payload from store.
var result = filterFunc.call(this, listenable.getInitialState());
- if (result) {
- return _.object([key], [result]);
+ if (typeof(result) !== "undefined") {
+ return _.object([key], [result]);
} else {
- return {};
+ return {};
}
}
},
@@ -24,6 +24,53 @@ describe('using the connectFilter(...) mixin',function(){
});
});
+ describe("when filter func returns", function() {
+ var listenable, context, expectedResult;
+ function withFilterReturning(val) {
+ var key = "KEY";
+ listenable = {
+ listen: sinon.spy(),
+ getInitialState: sinon.stub().returns("DOES NOT MATTER")
+ };
+ context = {setState: sinon.spy()};
+ expectedResult = {};
+ expectedResult[key] = val;
+ _.extend(context,connectFilter(listenable, key, function() {return val;}));
+ }
+ describe("0", function() {
+ beforeEach(function () {
+ withFilterReturning(0);
+ });
+ it("should return that value", function() {
+ assert.deepEqual(expectedResult, context.getInitialState());
+ });
+ });
+ describe("empty string", function() {
+ beforeEach(function () {
+ withFilterReturning("");
+ });
+ it("should return that value", function() {
+ assert.deepEqual(expectedResult, context.getInitialState());
+ });
+ });
+ describe("false", function() {
+ beforeEach(function () {
+ withFilterReturning(false);
+ });
+ it("should return that value", function() {
+ assert.deepEqual(expectedResult, context.getInitialState());
+ });
+ });
+ describe("undefined", function() {
+ beforeEach(function () {
+ withFilterReturning();
+ });
+ it("should return empty object", function() {
+ assert.deepEqual({}, context.getInitialState());
+ });
+ });
+ });
+
describe("when calling without key",function(){
var initialstate = "DEFAULTDATA",
listenable = {

0 comments on commit 5a5e6d8

Please sign in to comment.