Skip to content

Commit

Permalink
feat(NgTableFilterParams): add hasFilter function
Browse files Browse the repository at this point in the history
Useful for testing whether the NgTableParams.filter has at least one significant field value
  • Loading branch information
ccrowhurstram committed Aug 2, 2015
1 parent 1ed4216 commit 1163e22
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
21 changes: 21 additions & 0 deletions src/scripts/03-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,27 @@ app.factory('NgTableParams', ['$q', '$log', 'ngTableDefaults', function($q, $log
return pages;
};

/**
* @ngdoc method
* @name NgTableParams#hasFilter
* @description Determines if NgTableParams#filter has significant filter value(s)
* (any value except null, undefined, or empty string)
* @returns {Boolean} true when NgTableParams#filter has at least one significant field value
*/
this.hasFilter = function(){
var currentFilter = this.filter();
var keys = Object.keys(currentFilter);
var result = false;
for (var i=0; i < keys.length; i++){
var filterValue = currentFilter[keys[i]];
if (filterValue != null && filterValue !== '') {
result = true;
break;
}
}
return result;
};

/**
* @ngdoc method
* @name NgTableParams#url
Expand Down
51 changes: 38 additions & 13 deletions test/tableParamsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ describe('NgTableParams', function () {
page: 3
});
expect(params.page()).toBe(3);

var callCount = 0;
scope.tableParams = params;
scope.$watch('tableParams', function (innerParams) {
callCount++;
expect(innerParams.page()).toBe(4);
});
params.page(4);
scope.$apply();
expect(callCount).toBe(1);
// repeat call
scope.$apply();
expect(callCount).toBe(1);
}));

it('NgTableParams parse url parameters', inject(function (NgTableParams) {
Expand Down Expand Up @@ -293,4 +280,42 @@ describe('NgTableParams', function () {
expect(settings.counts.length).toEqual(0);
expect(settings.filterDelay).toEqual(750);
}));

describe('hasFilter', function(){
var tableParams;

beforeEach(inject(function(NgTableParams){
tableParams = new NgTableParams({}, {});
}));

it('should return false for an empty filter object', function(){
tableParams.filter({});
expect(tableParams.hasFilter()).toBeFalsy();
});

it('should return true for when filter has a field with a significant value', function(){
tableParams.filter({ a: 'b' });
expect(tableParams.hasFilter()).toBeTruthy();

tableParams.filter({ a: 0 });
expect(tableParams.hasFilter()).toBeTruthy();
});

it('should return false when filter only has insignificant field values', function(){
tableParams.filter({ a: '' });
expect(tableParams.hasFilter()).toBeFalsy();

tableParams.filter({ a: null });
expect(tableParams.hasFilter()).toBeFalsy();

tableParams.filter({ a: undefined });
expect(tableParams.hasFilter()).toBeFalsy();

tableParams.filter({ a: undefined, b: '', c: undefined });
expect(tableParams.hasFilter()).toBeFalsy();

//tableParams.filter({ a: NaN });
//expect(tableParams.hasFilter()).toBeFalsy();
});
});
});

0 comments on commit 1163e22

Please sign in to comment.