Skip to content

Commit

Permalink
fix(gridEdit): Fixing scrollToFocus issues.
Browse files Browse the repository at this point in the history
Also, improving code coverage and fixing build issue.
  • Loading branch information
Portugal, Marcelo authored and mportuga committed Jan 17, 2018
1 parent 55635d1 commit cc8144c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/features/edit/js/gridEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@
cellNavNavigateDereg = uiGridCtrl.grid.api.cellNav.on.navigate($scope, function (newRowCol, oldRowCol, evt) {
if ($scope.col.colDef.enableCellEditOnFocus) {
// Don't begin edit if the cell hasn't changed
if (newRowCol.row === $scope.row && newRowCol.col === $scope.col && evt && (evt.type === 'click' || evt.type === 'keydown')) {
if (newRowCol.row === $scope.row && newRowCol.col === $scope.col &&
(!evt || (evt && (evt.type === 'click' || evt.type === 'keydown')))) {
$timeout(function() {
beginEdit(evt);
});
Expand All @@ -545,7 +546,6 @@
}

$scope.beginEditEventsWired = true;

}

function touchStart(event) {
Expand Down Expand Up @@ -896,7 +896,6 @@
}
return object;
}

}
};
}]);
Expand Down
97 changes: 96 additions & 1 deletion src/features/edit/test/uiGridCell.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

describe('ui.grid.edit GridCellDirective', function() {
var $compile, $rootScope, $templateCache, $timeout, element, grid, gridClassFactory, gridUtil,
recompile, scope, uiGridConstants, uiGridCtrl, uiGridEditService;
recompile, scope, uiGridConstants, uiGridCtrl, uiGridEditService, onNavigateCb;

beforeEach(function() {
module('ui.grid');
module('ui.grid.edit');

inject(function(_$compile_, _$rootScope_, _$templateCache_, _$timeout_, _gridClassFactory_,
Expand Down Expand Up @@ -37,6 +38,27 @@
uiGridEditService.initializeGrid(grid);
grid.buildColumns();
grid.modifyRows(grid.options.data);
grid.options.onRegisterApi = function(gridApi) {
uiGridCtrl = {
grid: {
api: gridApi
}
};
gridApi.cellNav = {
on: {
navigate: function(scope, callback) {
uiGridCtrl.gridScope = scope;
onNavigateCb = callback;

return angular.noop;
},
viewPortKeyDown: function() {
return angular.noop;
}
}
};
};
element = angular.element('<div ui-grid-cell/>');

scope.grid = grid;
scope.col = grid.columns[0];
Expand Down Expand Up @@ -89,6 +111,79 @@
});
});

describe('on navigate', function() {
beforeEach(function() {
element = angular.element('<div ui-grid="grid.options" ui-grid-edit/>');
});
describe('if enableCellEditOnFocus is set to true', function() {
beforeEach(function() {
recompile();
uiGridCtrl.gridScope.col.colDef.enableCellEditOnFocus = true;
uiGridCtrl.gridScope.col.editableCellTemplate = '';
uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary = jasmine.createSpy('scrollToIfNecessary').and.returnValue({then: angular.noop});
scope.$apply();
});
afterEach(function() {
uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary.calls.reset();
});
it('should trigger scrolling if necessary when the navigating to the current row and column via click', function(done) {
var event = new jQuery.Event('click');

onNavigateCb({row: uiGridCtrl.gridScope.row, col: uiGridCtrl.gridScope.col}, {}, event);
$timeout.flush();
setTimeout(function() {
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).toHaveBeenCalled();
done();
}, 1);
});
it('should trigger scrolling if necessary when the navigating to the current row and column via keydown', function(done) {
var event = new jQuery.Event('keydown');

onNavigateCb({row: uiGridCtrl.gridScope.row, col: uiGridCtrl.gridScope.col}, {}, event);
$timeout.flush();
setTimeout(function() {
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).toHaveBeenCalled();
done();
}, 1);
});
it('should trigger scrolling if necessary when the navigating to the current row and column without an event', function(done) {
onNavigateCb({row: uiGridCtrl.gridScope.row, col: uiGridCtrl.gridScope.col}, {}, null);
$timeout.flush();
setTimeout(function() {
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).toHaveBeenCalled();
done();
}, 1);
});
it('should not trigger scrolling if necessary when the navigating to a different row and column', function(done) {
onNavigateCb({row: 'DifferentRow', col: 'DifferentCol'}, {}, null);
$timeout.flush();
setTimeout(function() {
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).not.toHaveBeenCalled();
done();
}, 1);
});
});
describe('if enableCellEditOnFocus is set to false', function() {
beforeEach(function() {
recompile();
uiGridCtrl.gridScope.col.colDef.enableCellEditOnFocus = false;
uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary = jasmine.createSpy('scrollToIfNecessary').and.returnValue({then: angular.noop});
onNavigateCb({row: uiGridCtrl.gridScope.row, col: uiGridCtrl.gridScope.col}, {}, null);
$timeout.flush();
scope.$apply();
});
afterEach(function() {
uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary.calls.reset();
});
it('should not trigger scrolling', function(done) {
setTimeout(function() {
expect(uiGridCtrl.gridScope.grid.api.core.scrollToIfNecessary).not.toHaveBeenCalled();
done();
}, 1);
});
});
});

describe('touchStart', function() {
function testTouchStart(withOriginalEvent) {
beforeEach(function() {
Expand Down
12 changes: 8 additions & 4 deletions src/features/edit/test/uiGridEditFileChooserDirective.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,27 @@ describe('ui.grid.edit uiGridEditFileChooser', function() {
}
describe('when no target exists', function() {
beforeEach(function() {
var event = new Event('change', {target: null});
var event = document.createEvent('HTMLEvents');

event.initEvent('change', false, true);
fileChooser[0].dispatchEvent(event);
});
testNegativeScenario();
});
describe('when no target files exists', function() {
beforeEach(function() {
var event = new Event('change', {target: {}});
var event = document.createEvent('HTMLEvents');

event.initEvent('change', false, true);
fileChooser[0].dispatchEvent(event);
});
testNegativeScenario();
});
describe('when there are 0 target files', function() {
beforeEach(function() {
var event = new Event('change', {target: {files: []}});
var event = document.createEvent('HTMLEvents');

event.initEvent('change', false, true);
fileChooser[0].dispatchEvent(event);
});
testNegativeScenario();
Expand All @@ -83,8 +86,9 @@ describe('ui.grid.edit uiGridEditFileChooser', function() {
expect(fileChooser[0].select).toHaveBeenCalled();
});
it('should emit an end cell edit event on blur', function() {
var event = new Event('blur');
var event = document.createEvent('HTMLEvents');

event.initEvent('blur', false, true);
spyOn($scope, '$emit').and.callFake(angular.noop);
fileChooser.triggerHandler(event);

Expand Down

0 comments on commit cc8144c

Please sign in to comment.