Merge pull request #1127 from nasa/console-warning-1114

[Tree] Check for change before scope.$apply
This commit is contained in:
Andrew Henry 2016-08-28 16:49:59 +01:00 committed by GitHub
commit 9063996e84
2 changed files with 39 additions and 3 deletions

View File

@ -28,8 +28,10 @@ define([
function link(scope, element) {
var treeView = new TreeView(gestureService),
unobserve = treeView.observe(function (domainObject) {
scope.mctModel = domainObject;
scope.$apply();
if (scope.mctModel !== domainObject) {
scope.mctModel = domainObject;
scope.$apply();
}
});
element.append(angular.element(treeView.elements()));

View File

@ -29,6 +29,18 @@ define([
mockExpr,
mctTree;
function makeMockDomainObject(id) {
var mockDomainObject = jasmine.createSpyObj('domainObject-' + id, [
'getId',
'getModel',
'getCapability',
'hasCapability'
]);
mockDomainObject.getId.andReturn(id);
mockDomainObject.getModel.andReturn({});
return mockDomainObject;
}
beforeEach(function () {
mockGestureService = jasmine.createSpyObj(
'gestureService',
@ -56,7 +68,8 @@ define([
testAttrs;
beforeEach(function () {
mockScope = jasmine.createSpyObj('$scope', ['$watch', '$on']);
mockScope =
jasmine.createSpyObj('$scope', ['$watch', '$on', '$apply']);
mockElement = jasmine.createSpyObj('element', ['append']);
testAttrs = { mctModel: "some-expression" };
mockScope.$parent =
@ -88,6 +101,27 @@ define([
jasmine.any(Function)
);
});
// https://github.com/nasa/openmct/issues/1114
it("does not trigger $apply during $watches", function () {
mockScope.mctObject = makeMockDomainObject('root');
mockScope.mctMode = makeMockDomainObject('selection');
mockScope.$watch.calls.forEach(function (call) {
call.args[1](mockScope[call.args[0]]);
});
expect(mockScope.$apply).not.toHaveBeenCalled();
});
it("does trigger $apply from other value changes", function () {
// White-boxy; we know this is the setter for the tree's value
var treeValueFn = mockScope.$watch.calls[0].args[1];
mockScope.mctObject = makeMockDomainObject('root');
mockScope.mctMode = makeMockDomainObject('selection');
treeValueFn(makeMockDomainObject('other'));
expect(mockScope.$apply).toHaveBeenCalled();
});
});
});