Skip to content

Commit

Permalink
fix(expandable.js): Update expandedAll attribute when toggling rows.
Browse files Browse the repository at this point in the history
fix #6077
  • Loading branch information
Portugal, Marcelo authored and mportuga committed Mar 31, 2018
1 parent 214f6cc commit 1b080b9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
22 changes: 17 additions & 5 deletions src/features/expandable/js/expandable.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@
var publicApi = {
events: {
expandable: {
/**
* @ngdoc event
* @name rowExpandedBeforeStateChanged
* @eventOf ui.grid.expandable.api:PublicApi
* @description raised when row is expanding or collapsing
* <pre>
* gridApi.expandable.on.rowExpandedBeforeStateChanged(scope,function(row){})
* </pre>
* @param {scope} scope the application scope
* @param {GridRow} row the row that was expanded
*/
rowExpandedBeforeStateChanged: function(scope, row){
},
/**
* @ngdoc event
* @name rowExpandedStateChanged
Expand All @@ -136,10 +149,9 @@
* <pre>
* gridApi.expandable.on.rowExpandedStateChanged(scope,function(row){})
* </pre>
* @param {scope} scope the application scope
* @param {GridRow} row the row that was expanded
*/
rowExpandedBeforeStateChanged: function(scope,row){
},
rowExpandedStateChanged: function (scope, row) {
}
}
Expand Down Expand Up @@ -270,15 +282,15 @@

if (row.isExpanded) {
row.height = row.grid.options.rowHeight + row.expandedRowHeight;
}
else {
grid.expandable.expandedAll = service.getExpandedRows(grid).length === grid.rows.length;
} else {
row.height = row.grid.options.rowHeight;
grid.expandable.expandedAll = false;
}
grid.api.expandable.raise.rowExpandedStateChanged(row);
},

expandAllRows: function(grid, $scope) {
expandAllRows: function(grid) {
grid.renderContainers.body.visibleRowCache.forEach( function(row) {
if (!row.isExpanded && !(row.entity.subGridOptions && row.entity.subGridOptions.disableRowExpandable)) {
service.toggleRowExpansion(grid, row);
Expand Down
74 changes: 66 additions & 8 deletions src/features/expandable/test/expandable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ describe('ui.grid.expandable', function() {
grid.api.registerMethodsFromObject.and.callFake(function(publicMethods) {
methods = publicMethods.expandable;
});
grid.api.registerEventsFromObject.and.callFake(function(events) {
grid.api.expandable = {raise: events.expandable};
});
grid.rows = [
{entity: {id: 1, name: 'John Doe'}, isExpanded: true},
{entity: {id: 2, name: 'Joe Doe'}, isExpanded: false},
Expand Down Expand Up @@ -233,20 +236,64 @@ describe('ui.grid.expandable', function() {
});
describe('expandRow', function() {
beforeEach(function() {
grid.getRow.and.returnValue(null);
methods.expandRow(grid.rows[1].entity);
grid.getRow.and.callFake(function(rowEntity) {
return rowEntity;
});

grid.rows = [
{isExpanded: false, grid: grid},
{isExpanded: true, grid: grid},
{isExpanded: true, grid: grid},
{isExpanded: true, grid: grid},
{isExpanded: true, grid: grid},
{isExpanded: true, grid: grid}
];
});
it('should call getRow on the grid with the row entity passed in', function() {
expect(grid.getRow).toHaveBeenCalledWith(grid.rows[1].entity);
it('should do nothing when the current row is expanded', function() {
methods.expandRow(grid.rows[1]);

expect(grid.rows[1].isExpanded).toEqual(true);
});
it('should expand the current row if it is collapsed', function() {
methods.expandRow(grid.rows[0]);

expect(grid.rows[0].isExpanded).toEqual(true);
});
it('should update the value of expandAll if all rows are expanded', function() {
methods.expandRow(grid.rows[0]);

expect(grid.expandable.expandedAll).toEqual(true);
});
});
describe('collapseRow', function() {
beforeEach(function() {
grid.getRow.and.returnValue(null);
methods.collapseRow(grid.rows[0].entity);
grid.getRow.and.callFake(function(rowEntity) {
return rowEntity;
});

grid.rows = [
{isExpanded: false, grid: grid},
{isExpanded: true, grid: grid},
{isExpanded: true, grid: grid},
{isExpanded: true, grid: grid},
{isExpanded: true, grid: grid},
{isExpanded: true, grid: grid}
];
});
it('should call getRow on the grid with the row entity passed in', function() {
expect(grid.getRow).toHaveBeenCalledWith(grid.rows[0].entity);
it('should do nothing when the current row is collapsed', function() {
methods.collapseRow(grid.rows[0]);

expect(grid.rows[0].isExpanded).toEqual(false);
});
it('should collapse the current row if it is expanded', function() {
methods.collapseRow(grid.rows[1]);

expect(grid.rows[1].isExpanded).toEqual(false);
});
it('should update the value of expandAll if one row is collapsed', function() {
methods.collapseRow(grid.rows[0]);

expect(grid.expandable.expandedAll).toEqual(false);
});
});
describe('getExpandedRows', function() {
Expand Down Expand Up @@ -293,7 +340,18 @@ describe('ui.grid.expandable', function() {
});

describe('getExpandedRows', function() {
it('should return only the rows that are expanded', function() {
grid.rows = [
{isExpanded: false},
{isExpanded: true},
{isExpanded: false},
{isExpanded: true},
{isExpanded: true},
{isExpanded: true}
];

expect(uiGridExpandableService.getExpandedRows(grid).length).toEqual(4);
});
});
});
});

0 comments on commit 1b080b9

Please sign in to comment.