[Inspector] Update controller and its tests

Update ObjectInspectorController to first look at
ngModel.inspectionObjects before defaulting back
to ngModel.selectedObject
Updated tests accordingly.
This commit is contained in:
slhale 2015-08-28 10:31:40 -07:00
parent 8c811c4a22
commit b0b87d7fd9
2 changed files with 58 additions and 34 deletions

View File

@ -39,15 +39,16 @@ define(
$scope.primaryParents = []; $scope.primaryParents = [];
$scope.contextutalParents = []; $scope.contextutalParents = [];
// Gets an array of the contextual parents/anscestors of the selected object // Gets an array of the contextual parents/anscestors of the (first) inspected object
function getContextualPath() { function getContextualPath() {
var currentObj, var currentObj,
currentParent, currentParent,
parents = []; parents = [];
if ($scope.ngModel && $scope.ngModel.inspectionObjects) { if ($scope.ngModel && $scope.ngModel.inspectionObjects && $scope.ngModel.inspectionObjects[0]) {
currentObj = $scope.ngModel.inspectionObjects[0]; currentObj = $scope.ngModel.inspectionObjects[0];
} else { } else {
// Fallback for if the inspection objects are not defined is the selected object
currentObj = $scope.ngModel && $scope.ngModel.selectedObject; currentObj = $scope.ngModel && $scope.ngModel.selectedObject;
} }
@ -55,6 +56,7 @@ define(
currentObj.hasCapability('context') && currentObj.hasCapability('context') &&
currentObj.getCapability('context').getParent(); currentObj.getCapability('context').getParent();
// Loop while this has a parent that is not the root object
while (currentParent && currentParent.getModel().type !== 'root' && while (currentParent && currentParent.getModel().type !== 'root' &&
currentParent.hasCapability('context')) { currentParent.hasCapability('context')) {
// Record this object // Record this object
@ -68,19 +70,22 @@ define(
$scope.contextutalParents = parents; $scope.contextutalParents = parents;
} }
// Gets an array of the parents/anscestors of the selected object's // Gets an array of the parents/anscestors of the (first) inspected object's
// primary location (locational of original non-link) // primary location (locational of original non-link)
function getPrimaryPath(current) { function getPrimaryPath(current) {
var location; var location;
// If this the the initial call of this recursive function // If this the the initial call of this recursive function
if (!current) { if (!current) {
if ($scope.ngModel && $scope.ngModel.inspectionObjects) { // Set the object we are looking at
if ($scope.ngModel && $scope.ngModel.inspectionObjects && $scope.ngModel.inspectionObjects[0]) {
current = $scope.ngModel.inspectionObjects[0]; current = $scope.ngModel.inspectionObjects[0];
} else { } else {
// Fallback for if the inspection objects are not defined is the selected object
current = $scope.ngModel && $scope.ngModel.selectedObject; current = $scope.ngModel && $scope.ngModel.selectedObject;
} }
// And reset the parents array
$scope.primaryParents = []; $scope.primaryParents = [];
} }
@ -98,41 +103,36 @@ define(
// Gets the metadata for the selected object // Gets the metadata for the selected object
function getMetadata() { function getMetadata() {
// We want to get info from the inspectionObjects, but otherwise just use if ($scope.ngModel &&
// the selectedObject. $scope.ngModel.inspectionObjects &&
if ($scope.ngModel && $scope.ngModel.inspectionObjects && $scope.ngModel.inspectionObjects[0] &&
$scope.ngModel.inspectionObjects[0].hasCapability('metadata')) { $scope.ngModel.inspectionObjects[0].hasCapability('metadata')) {
// Get metadata from the inspected object
$scope.metadata = $scope.ngModel.inspectionObjects[0].useCapability('metadata'); $scope.metadata = $scope.ngModel.inspectionObjects[0].useCapability('metadata');
} else { } else {
// Fallback for if the inspection objects are not defined is the selected object
$scope.metadata = $scope.ngModel && $scope.ngModel.selectedObject && $scope.metadata = $scope.ngModel && $scope.ngModel.selectedObject &&
$scope.ngModel.selectedObject.hasCapability('metadata') && $scope.ngModel.selectedObject.hasCapability('metadata') &&
$scope.ngModel.selectedObject.useCapability('metadata'); $scope.ngModel.selectedObject.useCapability('metadata');
} }
} }
// Set scope variables when the selected object changes $scope.$watch('ngModel.inspectionObjects', function () {
$scope.$watch('ngModel.selectedObject', function () { var isLink;
var isLink = $scope && $scope.ngModel &&
$scope.ngModel.selectedObject &&
$scope.ngModel.selectedObject.hasCapability('location') &&
$scope.ngModel.selectedObject.getCapability('location').isLink();
if (isLink) { if ($scope && $scope.ngModel &&
getPrimaryPath(); $scope.ngModel.inspectionObjects &&
getContextualPath(); $scope.ngModel.inspectionObjects[0]) {
isLink = $scope.ngModel.inspectionObjects[0].hasCapability('location') &&
$scope.ngModel.inspectionObjects[0].getCapability('location').isLink();
} else { } else {
$scope.primaryParents = []; // Fallback for if the inspection objects are not defined is the selected object
getContextualPath(); isLink = $scope && $scope.ngModel &&
$scope.ngModel.selectedObject &&
$scope.ngModel.selectedObject.hasCapability('location') &&
$scope.ngModel.selectedObject.getCapability('location').isLink();
} }
getMetadata();
});
$scope.$watch('ngModel.inspectionObjects[0]', function () {
var isLink = $scope && $scope.ngModel &&
$scope.ngModel.inspectionObjects &&
$scope.ngModel.inspectionObjects[0].hasCapability('location') &&
$scope.ngModel.inspectionObjects[0].getCapability('location').isLink();
if (isLink) { if (isLink) {
getPrimaryPath(); getPrimaryPath();

View File

@ -36,7 +36,8 @@ define(
mockDomainObject, mockDomainObject,
mockContextCapability, mockContextCapability,
mockLocationCapability, mockLocationCapability,
controller; controller,
treePosCounter = 0;
beforeEach(function () { beforeEach(function () {
mockScope = jasmine.createSpyObj( mockScope = jasmine.createSpyObj(
@ -45,6 +46,7 @@ define(
); );
mockScope.ngModel = {}; mockScope.ngModel = {};
mockScope.ngModel.selectedObject = 'mock selected object'; mockScope.ngModel.selectedObject = 'mock selected object';
mockScope.ngModel.inspectionObjects = [];
mockObjectService = jasmine.createSpyObj( mockObjectService = jasmine.createSpyObj(
"objectService", "objectService",
@ -57,10 +59,18 @@ define(
mockObjectService.getObjects.andReturn(mockPromise); mockObjectService.getObjects.andReturn(mockPromise);
mockDomainObject = jasmine.createSpyObj( mockDomainObject = jasmine.createSpyObj(
"selectedObject", "domainObject",
[ "hasCapability", "getCapability", "useCapability", "getModel" ] [ "hasCapability", "getCapability", "useCapability", "getModel" ]
); );
mockDomainObject.getModel.andReturn({location: 'somewhere'}); mockDomainObject.getModel.andCallFake(function () {
// Simulate having a tree by making it take iterations to reach root
if (treePosCounter > 5) {
return {location: 'somewhere', type: 'root'};
} else {
treePosCounter += 1;
return {location: 'somewhere', type: 'something'};
}
});
mockDomainObject.hasCapability.andReturn(true); mockDomainObject.hasCapability.andReturn(true);
mockContextCapability = jasmine.createSpyObj( mockContextCapability = jasmine.createSpyObj(
@ -78,15 +88,16 @@ define(
return mockContextCapability; return mockContextCapability;
} }
}); });
mockContextCapability.getParent.andReturn(mockDomainObject);
controller = new ObjectInspectorController(mockScope, mockObjectService); controller = new ObjectInspectorController(mockScope, mockObjectService);
// Change the selected object to trigger the watch call // Change the inspected object to trigger the watch call
mockScope.ngModel.selectedObject = mockDomainObject; mockScope.ngModel.inspectionObjects[0] = mockDomainObject;
}); });
it("watches for changes to the selected object", function () { it("watches for changes to the inspection objects", function () {
expect(mockScope.$watch).toHaveBeenCalledWith('ngModel.selectedObject', jasmine.any(Function)); expect(mockScope.$watch).toHaveBeenCalledWith('ngModel.inspectionObjects', jasmine.any(Function));
}); });
it("looks for contextual parent objects", function () { it("looks for contextual parent objects", function () {
@ -94,7 +105,7 @@ define(
expect(mockContextCapability.getParent).toHaveBeenCalled(); expect(mockContextCapability.getParent).toHaveBeenCalled();
}); });
it("if link, looks for primary parent objects", function () { it("looks for primary parent objects if it is a link", function () {
mockLocationCapability.isLink.andReturn(true); mockLocationCapability.isLink.andReturn(true);
mockScope.$watch.mostRecentCall.args[1](); mockScope.$watch.mostRecentCall.args[1]();
@ -107,6 +118,19 @@ define(
mockScope.$watch.mostRecentCall.args[1](); mockScope.$watch.mostRecentCall.args[1]();
expect(mockDomainObject.useCapability).toHaveBeenCalledWith('metadata'); expect(mockDomainObject.useCapability).toHaveBeenCalledWith('metadata');
}); });
it("falls back on the selected object if there are no inspection objects", function () {
mockDomainObject.useCapability.reset();
mockScope.ngModel.selectedObject = mockDomainObject;
mockScope.ngModel.inspectionObjects = undefined;
expect(mockScope.$watch).toHaveBeenCalledWith('ngModel.inspectionObjects', jasmine.any(Function));
mockLocationCapability.isLink.andReturn(true);
mockScope.$watch.mostRecentCall.args[1]();
expect(mockDomainObject.useCapability).toHaveBeenCalled();
});
}); });
} }
); );