Skip to content

Commit 01857a3

Browse files
committed
- added key unset check
- easified unittest check - unittest corrections - corrected unit test array check - fixed getting not existing object - fixed selection unit test error get fix selection test getter fix selection test fix getter fix selection fixed selection fix lint error selection test void 0 check selection test
1 parent 15ce114 commit 01857a3

File tree

4 files changed

+53
-46
lines changed

4 files changed

+53
-46
lines changed

packages/core/src/js/factories/Grid.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,11 +1105,15 @@ angular.module('ui.grid')
11051105
* looks in grid.rows
11061106
*/
11071107
Grid.prototype.getRowsByKey = function getRowsByKey(isInEntity, key, comparator, lookInRows) {
1108+
if ( key == void 0 ) {
1109+
return null;
1110+
}
1111+
11081112
lookInRows = lookInRows == void 0 ? this.rows : lookInRows;
11091113
var func = isInEntity ? function (row) {
1110-
return row.entity != void 0 && row.entity[key] === comparator;
1114+
return row.entity != void 0 && row.entity.hasOwnProperty(key) && row.entity[key] === comparator;
11111115
} : function (row) {
1112-
return row[key] === comparator;
1116+
return row.hasOwnProperty(key) && row[key] === comparator;
11131117
}
11141118

11151119
return lookInRows.filter(func);
@@ -1127,23 +1131,25 @@ angular.module('ui.grid')
11271131
* looks in grid.rows
11281132
*/
11291133
Grid.prototype.findRowByKey = function findRowByKey(isInEntity, key, comparator, lookInRows) {
1130-
lookInRows = lookInRows == void 0 ? this.rows : lookInRows;
11311134
var result = null;
1132-
var func = isInEntity ? function (row) {
1133-
if ( row.entity != void 0 && row.entity[key] === comparator ) {
1134-
result = row;
1135-
return false;
1136-
}
1137-
return true;
1138-
} : function (row) {
1139-
if ( row[key] === comparator ) {
1140-
result = row;
1141-
return false;
1135+
if ( key != void 0 ) {
1136+
lookInRows = lookInRows == void 0 ? this.rows : lookInRows;
1137+
var func = isInEntity ? function (row) {
1138+
if ( row.entity != void 0 && row.entity.hasOwnProperty(key) && row.entity[key] === comparator ) {
1139+
result = row;
1140+
return false;
1141+
}
1142+
return true;
1143+
} : function (row) {
1144+
if ( row.hasOwnProperty(key) && row[key] === comparator ) {
1145+
result = row;
1146+
return false;
1147+
}
1148+
return true;
11421149
}
1143-
return true;
1144-
}
11451150

1146-
lookInRows.every(func);
1151+
lookInRows.every(func);
1152+
}
11471153
return result
11481154
};
11491155

packages/core/test/core/factories/Grid.spec.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ describe('Grid factory', function() {
327327
describe('getting Rows', function() {
328328
it('should get Rows', function() {
329329
expect(grid.getRow()).toBe(null);
330-
expect(grid.getRows(grid.rows[0].entity)).toBe(grid.rows[0]);
331-
expect(grid.getRows(grid.rows[0].entity), [grid.rows[0]]).toBe(grid.rows[0]);
330+
expect(grid.getRow(grid.rows[0].entity)).toBe(grid.rows[0]);
331+
expect(grid.getRow(grid.rows[0].entity), [grid.rows[0]]).toBe(grid.rows[0]);
332332
});
333333

334334
it('should get Rows by key', function() {
@@ -341,19 +341,19 @@ describe('Grid factory', function() {
341341
grid.rows[1].innerMulti = false;
342342

343343
expect(grid.getRowsByKey()).toBe(null);
344-
expect(grid.getRowsByKey(true, "test")).toBe(null);
345-
expect(grid.getRowsByKey(true, "str", "abc")).toBe([grid.rows[0]]);
346-
expect(grid.getRowsByKey(true, "str", "def")).toBe(null);
347-
expect(grid.getRowsByKey(true, "num", 123)).toBe([grid.rows[0]]);
348-
expect(grid.getRowsByKey(true, "nll", null)).toBe([grid.rows[0]]);
344+
expect(grid.getRowsByKey(true, "test")).toEqual([]);
345+
expect(grid.getRowsByKey(true, "str", "abc")[0].entity).toBe(grid.rows[0].entity);
346+
expect(grid.getRowsByKey(true, "str", "def")).toEqual([]);
347+
expect(grid.getRowsByKey(true, "num", 123)[0].entity).toBe(grid.rows[0].entity);
348+
expect(grid.getRowsByKey(true, "nll", null)[0].entity).toBe(grid.rows[0].entity);
349349
expect(grid.getRowsByKey(true, "multi", true).length).toBe(2);
350350

351-
expect(grid.getRowsByKey(false, "test")).toBe(null);
352-
expect(grid.getRowsByKey(false, "str", "abc")).toBe([grid.rows[0]]);
353-
expect(grid.getRowsByKey(false, "str", "def")).toBe(null);
354-
expect(grid.getRowsByKey(false, "num", 123)).toBe([grid.rows[0]]);
355-
expect(grid.getRowsByKey(false, "nll", null)).toBe([grid.rows[0]]);
356-
expect(grid.getRowsByKey(true, "innerMulti", false).length).toBe(2);
351+
expect(grid.getRowsByKey(false, "test")).toEqual([]);
352+
expect(grid.getRowsByKey(false, "str", "abc")).toEqual([grid.rows[0]]);
353+
expect(grid.getRowsByKey(false, "str", "def")).toEqual([]);
354+
expect(grid.getRowsByKey(false, "num", 123)).toEqual([grid.rows[0]]);
355+
expect(grid.getRowsByKey(false, "nll", null)).toEqual([grid.rows[0]]);
356+
expect(grid.getRowsByKey(false, "innerMulti", false).length).toBe(2);
357357
});
358358

359359
it('should find first Row by key', function() {
@@ -367,18 +367,18 @@ describe('Grid factory', function() {
367367

368368
expect(grid.findRowByKey()).toBe(null);
369369
expect(grid.findRowByKey(true, "test")).toBe(null);
370-
expect(grid.findRowByKey(true, "str", "abc")).toBe([grid.rows[0]]);
370+
expect(grid.findRowByKey(true, "str", "abc").entity).toBe(grid.rows[0].entity);
371371
expect(grid.findRowByKey(true, "str", "def")).toBe(null);
372-
expect(grid.findRowByKey(true, "num", 123)).toBe([grid.rows[0]]);
373-
expect(grid.findRowByKey(true, "nll", null)).toBe([grid.rows[0]]);
374-
expect(grid.findRowByKey(true, "multi", true).length).toBe(1);
372+
expect(grid.findRowByKey(true, "num", 123).entity).toBe(grid.rows[0].entity);
373+
expect(grid.findRowByKey(true, "nll", null).entity).toBe(grid.rows[0].entity);
374+
expect(grid.findRowByKey(true, "multi", true).entity).toBe(grid.rows[0].entity);
375375

376376
expect(grid.findRowByKey(false, "test")).toBe(null);
377-
expect(grid.findRowByKey(false, "str", "abc")).toBe([grid.rows[0]]);
377+
expect(grid.findRowByKey(false, "str", "abc")).toBe(grid.rows[0]);
378378
expect(grid.findRowByKey(false, "str", "def")).toBe(null);
379-
expect(grid.findRowByKey(false, "num", 123)).toBe([grid.rows[0]]);
380-
expect(grid.findRowByKey(false, "nll", null)).toBe([grid.rows[0]]);
381-
expect(grid.findRowByKey(true, "innerMulti", false).length).toBe(1);
379+
expect(grid.findRowByKey(false, "num", 123)).toBe(grid.rows[0]);
380+
expect(grid.findRowByKey(false, "nll", null)).toBe(grid.rows[0]);
381+
expect(grid.findRowByKey(false, "innerMulti", false).entity).toBe(grid.rows[0].entity);
382382
});
383383
})
384384

packages/selection/src/js/selection.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
*/
202202
toggleRowSelection: function (rowEntity, evt) {
203203
var row = grid.getRow(rowEntity);
204-
if (row !== null) {
204+
if (row != void 0) {
205205
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
206206
}
207207
},
@@ -215,7 +215,7 @@
215215
*/
216216
selectRow: function (rowEntity, evt) {
217217
var row = grid.getRow(rowEntity);
218-
if (row !== null && !row.isSelected) {
218+
if (row != void 0 && !row.isSelected) {
219219
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
220220
}
221221
},
@@ -232,7 +232,7 @@
232232
*/
233233
selectRowByVisibleIndex: function (rowNum, evt) {
234234
var row = grid.renderContainers.body.visibleRowCache[rowNum];
235-
if (row !== null && typeof (row) !== 'undefined' && !row.isSelected) {
235+
if (row != void 0 && !row.isSelected) {
236236
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
237237
}
238238
},
@@ -249,7 +249,7 @@
249249
*/
250250
selectRowByKey: function (isInEntity, key, comparator, evt, lookInRows) {
251251
var row = grid.findRowByKey(isInEntity, key, comparator, lookInRows);
252-
if (row !== null && !row.isSelected) {
252+
if (row != void 0 && !row.isSelected) {
253253
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
254254
}
255255
},
@@ -263,7 +263,7 @@
263263
*/
264264
unSelectRow: function (rowEntity, evt) {
265265
var row = grid.getRow(rowEntity);
266-
if (row !== null && row.isSelected) {
266+
if (row != void 0 && row.isSelected) {
267267
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
268268
}
269269
},
@@ -280,7 +280,7 @@
280280
*/
281281
unSelectRowByVisibleIndex: function (rowNum, evt) {
282282
var row = grid.renderContainers.body.visibleRowCache[rowNum];
283-
if (row !== null && typeof (row) !== 'undefined' && row.isSelected) {
283+
if (row != void 0 && row.isSelected) {
284284
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
285285
}
286286
},
@@ -297,7 +297,7 @@
297297
*/
298298
unSelectRowByKey: function (isInEntity, key, comparator, evt, lookInRows) {
299299
var row = grid.findRowByKey(isInEntity, key, comparator, lookInRows);
300-
if (row !== null && !row.isSelected) {
300+
if (row != void 0 && row.isSelected) {
301301
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
302302
}
303303
},

packages/selection/test/uiGridSelectionService.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,9 @@ describe('ui.grid.selection uiGridSelectionService', function() {
230230
});
231231

232232
it('select by key then unselect rows by key outside entity, including selecting rows already selected and unselecting rows not selected', function() {
233-
grid.rows[4] = {str: 'abc'};
234-
grid.rows[6] = {str: 'def'};
233+
grid.rows[4].str = 'abc';
234+
grid.rows[6].str = 'def';
235+
235236
grid.api.selection.selectRowByKey(false, "str", "abc");
236237
expect(grid.rows[4].isSelected).toBe(true);
237238

0 commit comments

Comments
 (0)