Skip to content

Commit 138d149

Browse files
authored
fix(modifyRows): modifyRows uses the new entity when using enableRowHashing
When using enableRowHashing (which is set right now to true as default), modifyRows match the old and the new row but not using the new entity at all. Credit to @idangozlan for the fix. This PR merely adds unit tests to his [work](#4818).
1 parent da942e9 commit 138d149

File tree

2 files changed

+72
-42
lines changed

2 files changed

+72
-42
lines changed

src/js/core/factories/Grid.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ angular.module('ui.grid')
10661066
* @methodOf ui.grid.class:Grid
10671067
* @description returns the GridRow that contains the rowEntity
10681068
* @param {object} rowEntity the gridOptions.data array element instance
1069-
* @param {array} rows [optional] the rows to look in - if not provided then
1069+
* @param {array} lookInRows [optional] the rows to look in - if not provided then
10701070
* looks in grid.rows
10711071
*/
10721072
Grid.prototype.getRow = function getRow(rowEntity, lookInRows) {
@@ -1131,13 +1131,20 @@ angular.module('ui.grid')
11311131
self.rows.length = 0;
11321132

11331133
newRawData.forEach( function( newEntity, i ) {
1134-
var newRow;
1134+
var newRow, oldRow;
1135+
11351136
if ( self.options.enableRowHashing ){
11361137
// if hashing is enabled, then this row will be in the hash if we already know about it
1137-
newRow = oldRowHash.get( newEntity );
1138+
oldRow = oldRowHash.get( newEntity );
11381139
} else {
11391140
// otherwise, manually search the oldRows to see if we can find this row
1140-
newRow = self.getRow(newEntity, oldRows);
1141+
oldRow = self.getRow(newEntity, oldRows);
1142+
}
1143+
1144+
// update newRow to have an entity
1145+
if ( oldRow ) {
1146+
newRow = oldRow;
1147+
newRow.entity = newEntity;
11411148
}
11421149

11431150
// if we didn't find the row, it must be new, so create it

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

+61-38
Original file line numberDiff line numberDiff line change
@@ -414,86 +414,109 @@ describe('Grid factory', function () {
414414
});
415415

416416
describe('follow source array', function() {
417-
it('should insert it on position 0', function() {
418-
var dataRows = [{str:'abc'}];
419-
var grid = new Grid({ id: 1 });
417+
var dataRows, grid;
420418

421-
grid.modifyRows(dataRows);
419+
beforeEach(function() {
420+
dataRows = [{str:'abc'},{str:'cba'},{str:'bac'}];
421+
grid = new Grid({ id: 1 });
422+
grid.options.enableRowHashing = false;
423+
424+
spyOn(grid, 'getRow').and.callThrough();
422425

426+
grid.modifyRows(dataRows);
427+
});
423428

424-
expect(grid.rows.length).toBe(1);
429+
it('should update the grid rows', function() {
430+
expect(grid.rows.length).toBe(3);
425431
expect(grid.rows[0].entity.str).toBe('abc');
432+
expect(grid.rows[1].entity.str).toBe('cba');
433+
expect(grid.rows[2].entity.str).toBe('bac');
434+
});
426435

436+
it('should insert it on position 0', function() {
427437
dataRows.splice(0,0,{str:'cba'});
428438
grid.modifyRows(dataRows);
429439

430-
expect(grid.rows.length).toBe(2);
440+
expect(grid.getRow).toHaveBeenCalled();
441+
expect(grid.rows.length).toBe(4);
431442
expect(grid.rows[0].entity.str).toBe('cba');
432443
});
433444

434445
it('should swap', function() {
435-
var dataRows = [{str:'abc'},{str:'cba'}];
436-
var grid = new Grid({ id: 1 });
437-
438-
grid.modifyRows(dataRows);
439-
440-
expect(grid.rows[0].entity.str).toBe('abc');
441-
expect(grid.rows[1].entity.str).toBe('cba');
442-
443446
var tmpRow = dataRows[0];
447+
444448
dataRows[0] = dataRows[1];
445449
dataRows[1] = tmpRow;
446450
grid.modifyRows(dataRows);
447451

452+
expect(grid.getRow).toHaveBeenCalled();
448453
expect(grid.rows[0].entity.str).toBe('cba');
449454
expect(grid.rows[1].entity.str).toBe('abc');
450455
});
451456

452457
it('should delete and insert new in the middle', function() {
453-
var dataRows = [{str:'abc'},{str:'cba'},{str:'bac'}];
454-
var grid = new Grid({ id: 1 });
455-
456-
grid.modifyRows(dataRows);
457-
458-
expect(grid.rows.length).toBe(3);
459-
expect(grid.rows[0].entity.str).toBe('abc');
460-
expect(grid.rows[1].entity.str).toBe('cba');
461-
expect(grid.rows[2].entity.str).toBe('bac');
462-
463458
dataRows[1] = {str:'xyz'};
464459
grid.modifyRows(dataRows);
465460

461+
expect(grid.getRow).toHaveBeenCalled();
466462
expect(grid.rows.length).toBe(3);
467463
expect(grid.rows[0].entity.str).toBe('abc');
468464
expect(grid.rows[1].entity.str).toBe('xyz');
469465
expect(grid.rows[2].entity.str).toBe('bac');
470466
});
467+
});
468+
469+
describe('when row hashing is enabled', function() {
470+
var dataRows, grid;
471+
472+
beforeEach(function() {
473+
dataRows = [{str:'abc'},{str:'cba'},{str:'bac'}];
474+
grid = new Grid({ id: 1 });
475+
grid.options.enableRowHashing = true;
476+
477+
spyOn(grid, 'getRow').and.callThrough();
471478

472-
/*
473-
* No longer trying to keep order of sort - we run rowsProcessors
474-
* immediately after anyway, which will resort.
475-
*
476-
it('should keep the order of the sort', function() {
477-
var dataRows = [{str:'abc'},{str:'cba'},{str:'bac'}];
478-
var grid = new Grid({ id: 1 });
479-
grid.options.columnDefs = [{name:'1',type:'string'}];
480-
grid.buildColumns();
481479
grid.modifyRows(dataRows);
480+
});
482481

482+
it('should update the grid rows', function() {
483483
expect(grid.rows.length).toBe(3);
484484
expect(grid.rows[0].entity.str).toBe('abc');
485485
expect(grid.rows[1].entity.str).toBe('cba');
486486
expect(grid.rows[2].entity.str).toBe('bac');
487+
});
487488

488-
grid.sortColumn(grid.columns[0]);
489-
490-
dataRows.splice(0,0,{str:'xyz'});
489+
it('should insert it on position 0', function() {
490+
dataRows.splice(0,0,{str:'cba'});
491491
grid.modifyRows(dataRows);
492+
493+
expect(grid.getRow).not.toHaveBeenCalled();
492494
expect(grid.rows.length).toBe(4);
495+
expect(grid.rows[0].entity.str).toBe('cba');
496+
});
497+
498+
it('should swap', function() {
499+
var tmpRow = dataRows[0];
500+
501+
dataRows[0] = dataRows[1];
502+
dataRows[1] = tmpRow;
503+
grid.modifyRows(dataRows);
504+
505+
expect(grid.getRow).not.toHaveBeenCalled();
506+
expect(grid.rows[0].entity.str).toBe('cba');
507+
expect(grid.rows[1].entity.str).toBe('abc');
508+
});
509+
510+
it('should delete and insert new in the middle', function() {
511+
dataRows[1] = {str:'xyz'};
512+
grid.modifyRows(dataRows);
513+
514+
expect(grid.getRow).not.toHaveBeenCalled();
515+
expect(grid.rows.length).toBe(3);
493516
expect(grid.rows[0].entity.str).toBe('abc');
494-
expect(grid.rows[3].entity.str).toBe('xyz');
517+
expect(grid.rows[1].entity.str).toBe('xyz');
518+
expect(grid.rows[2].entity.str).toBe('bac');
495519
});
496-
*/
497520
});
498521

499522
describe('binding', function() {

0 commit comments

Comments
 (0)