Skip to content

Commit

Permalink
fix(NgTableParams): incorrect default sort direction applied to group…
Browse files Browse the repository at this point in the history
…ing function
  • Loading branch information
ccrowhurstram committed Aug 28, 2015
1 parent a7e5025 commit 7b30995
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/scripts/ngTableParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,23 @@
};

function parseGroup(group){
var defaultSort = settings.groupOptions && settings.groupOptions.defaultSort;
if (angular.isFunction(group)) {
if (group.sortDirection == null){
group.sortDirection = defaultSort;
}
return group;
} else if (angular.isString(group)) {
var grp = {};
grp[group] = settings.groupOptions && settings.groupOptions.defaultSort;
grp[group] = defaultSort;
return grp;
} else if (angular.isObject(group)) {
for (var key in group) {
if (group[key] == null){
group[key] = defaultSort;
}
}
return group;
} else {
return group;
}
Expand Down
22 changes: 14 additions & 8 deletions test/tableParamsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe('NgTableParams', function () {

expect(params.hasGroup()).toBe(true);
expect(params.hasGroup(angular.identity)).toBe(true);
expect(params.hasGroup(angular.identity, params.settings().groupOptions.defaultSort)).toBe(true);
expect(params.group()).toEqual(angular.identity);


Expand Down Expand Up @@ -247,19 +248,22 @@ describe('NgTableParams', function () {
expect(params.group()).toEqual({ age: 'asc' });
});

it('should not apply defaultSort from groupOptions when explicitly set to undefined/null', function(){
it('should not apply defaultSort from groupOptions when explicitly set to empty string', function(){
var params = new NgTableParams({
group: 'role'
}, {
groupOptions: { defaultSort: 'desc' }
});
expect(params.group()).toEqual({ role: 'desc' }); // checking assumptions

params.group({ role: undefined});
expect(params.group()).toEqual({ role: undefined });
params.group({ role: ''});
expect(params.group()).toEqual({ role: '' });

params.group({ age: null});
expect(params.group()).toEqual({ age: null });
params.group({ age: undefined});
expect(params.group()).toEqual({ age: 'desc' });

params.group({ role: null});
expect(params.group()).toEqual({ role: 'desc' });
});
});

Expand Down Expand Up @@ -445,12 +449,14 @@ describe('NgTableParams', function () {
});

it('should use group function to group data', function () {
var grouper = function (item) {
return item.name[0];
};
grouper.sortDirection = '';
var tp = createNgTableParams({
count: 2,
sorting: {name: 'desc'},
group: function (item) {
return item.name[0];
}
group: grouper
}, {data: data});

var actualRoleGroups;
Expand Down

0 comments on commit 7b30995

Please sign in to comment.