Skip to content

Commit

Permalink
fix(selection): remove a logic bug in setSelected(..)
Browse files Browse the repository at this point in the history
The bug was that selectedCount would become incorrect if setSelected(x)
was called twice in sequence for the same row with the same x.
  • Loading branch information
Gareth Smith committed Jul 1, 2016
1 parent 592be63 commit 57cdb31
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/features/selection/js/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,9 @@
* @param {bool} selected value to set
*/
$delegate.prototype.setSelected = function(selected) {
this.isSelected = selected;
if (selected) {
this.grid.selection.selectedCount++;
}
else {
this.grid.selection.selectedCount--;
if (selected !== this.isSelected) {
this.isSelected = selected;
this.grid.selection.selectedCount += selected ? 1 : -1;
}
};

Expand Down
20 changes: 20 additions & 0 deletions src/features/selection/test/uiGridSelectionService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ describe('ui.grid.selection uiGridSelectionService', function () {
expect(grid.rows[4].isSelected).toBe(false);
});
});

describe('setSelected function', function() {
it('select row and check the selected count is correct', function() {

expect(grid.selection.selectedCount).toBe(0);

grid.rows[0].setSelected(true);
expect(grid.rows[0].isSelected).toBe(true);
expect(grid.selection.selectedCount).toBe(1);

// the second setSelected(true) should have no effect
grid.rows[0].setSelected(true);
expect(grid.rows[0].isSelected).toBe(true);
expect(grid.selection.selectedCount).toBe(1);

grid.rows[0].setSelected(false);
expect(grid.rows[0].isSelected).toBe(false);
expect(grid.selection.selectedCount).toBe(0);
});
});

describe('selectAllRows and clearSelectedRows functions', function() {
it('should select all rows, and select all rows when already all selected, then unselect again', function () {
Expand Down

0 comments on commit 57cdb31

Please sign in to comment.