Skip to content

Commit 57cdb31

Browse files
author
Gareth Smith
committed
fix(selection): remove a logic bug in setSelected(..)
The bug was that selectedCount would become incorrect if setSelected(x) was called twice in sequence for the same row with the same x.
1 parent 592be63 commit 57cdb31

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/features/selection/js/selection.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,9 @@
6666
* @param {bool} selected value to set
6767
*/
6868
$delegate.prototype.setSelected = function(selected) {
69-
this.isSelected = selected;
70-
if (selected) {
71-
this.grid.selection.selectedCount++;
72-
}
73-
else {
74-
this.grid.selection.selectedCount--;
69+
if (selected !== this.isSelected) {
70+
this.isSelected = selected;
71+
this.grid.selection.selectedCount += selected ? 1 : -1;
7572
}
7673
};
7774

src/features/selection/test/uiGridSelectionService.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ describe('ui.grid.selection uiGridSelectionService', function () {
173173
expect(grid.rows[4].isSelected).toBe(false);
174174
});
175175
});
176+
177+
describe('setSelected function', function() {
178+
it('select row and check the selected count is correct', function() {
179+
180+
expect(grid.selection.selectedCount).toBe(0);
181+
182+
grid.rows[0].setSelected(true);
183+
expect(grid.rows[0].isSelected).toBe(true);
184+
expect(grid.selection.selectedCount).toBe(1);
185+
186+
// the second setSelected(true) should have no effect
187+
grid.rows[0].setSelected(true);
188+
expect(grid.rows[0].isSelected).toBe(true);
189+
expect(grid.selection.selectedCount).toBe(1);
190+
191+
grid.rows[0].setSelected(false);
192+
expect(grid.rows[0].isSelected).toBe(false);
193+
expect(grid.selection.selectedCount).toBe(0);
194+
});
195+
});
176196

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

0 commit comments

Comments
 (0)