Merge pull request #86 from nasa/open51

Addresses nasa/openmct#51
This commit is contained in:
Victor Woeltjen 2015-08-27 10:02:24 -07:00
commit 5230bdfc6b
3 changed files with 134 additions and 16 deletions

View File

@ -59,7 +59,7 @@
"glyph": "Z",
"name": "Remove",
"description": "Remove this object from its containing object.",
"depends": [ "$q" ]
"depends": [ "$q", "navigationService" ]
},
{
"key": "save",

View File

@ -41,9 +41,10 @@ define(
* @constructor
* @implements {Action}
*/
function RemoveAction($q, context) {
function RemoveAction($q, navigationService, context) {
this.domainObject = (context || {}).domainObject;
this.$q = $q;
this.navigationService = navigationService;
}
/**
@ -53,8 +54,8 @@ define(
*/
RemoveAction.prototype.perform = function () {
var $q = this.$q,
navigationService = this.navigationService,
domainObject = this.domainObject;
/*
* Check whether an object ID matches the ID of the object being
* removed (used to filter a parent's composition to handle the
@ -80,21 +81,53 @@ define(
var persistence = domainObject.getCapability('persistence');
return persistence && persistence.persist();
}
/*
* Checks current object and ascendants of current
* object with object being removed, if the current
* object or any in the current object's path is being removed,
* navigate back to parent of removed object.
*/
function checkObjectNavigation(object, parentObject) {
// Traverse object starts at current location
var traverseObject = (navigationService).getNavigation();
// Stop when object is not defined (above ROOT)
while (traverseObject) {
// If object currently traversed to is object being removed
// navigate to parent of current object and then exit loop
if (traverseObject.getId() === object.getId()) {
navigationService.setNavigation(parentObject);
return;
}
// Traverses to parent of current object, moving
// up the ascendant path
traverseObject = traverseObject.getCapability('context').getParent();
}
}
/*
* Remove the object from its parent, as identified by its context
* capability.
* capability. Based on object's location and selected object's location
* user may be navigated to existing parent object
*/
function removeFromContext(contextCapability) {
var parent = contextCapability.getParent();
function removeFromContext(object) {
var contextCapability = object.getCapability('context'),
parent = contextCapability.getParent();
// If currently within path of removed object(s),
// navigates to existing object up tree
checkObjectNavigation(object, parent);
return $q.when(
parent.useCapability('mutation', doMutate)
).then(function () {
return doPersist(parent);
});
parent.useCapability('mutation', doMutate)
).then(function () {
return doPersist(parent);
});
}
return $q.when(this.domainObject.getCapability('context'))
return $q.when(domainObject)
.then(removeFromContext);
};

View File

@ -28,9 +28,16 @@ define(
describe("The Remove action", function () {
var mockQ,
mockNavigationService,
mockDomainObject,
mockParent,
mockChildObject,
mockGrandchildObject,
mockRootObject,
mockContext,
mockChildContext,
mockGrandchildContext,
mockRootContext,
mockMutation,
mockPersistence,
mockType,
@ -54,6 +61,18 @@ define(
"domainObject",
[ "getId", "getCapability" ]
);
mockChildObject = jasmine.createSpyObj(
"domainObject",
[ "getId", "getCapability" ]
);
mockGrandchildObject = jasmine.createSpyObj(
"domainObject",
[ "getId", "getCapability" ]
);
mockRootObject = jasmine.createSpyObj(
"domainObject",
[ "getId", "getCapability" ]
);
mockQ = { when: mockPromise };
mockParent = {
getModel: function () {
@ -67,28 +86,41 @@ define(
}
};
mockContext = jasmine.createSpyObj("context", [ "getParent" ]);
mockChildContext = jasmine.createSpyObj("context", [ "getParent" ]);
mockGrandchildContext = jasmine.createSpyObj("context", [ "getParent" ]);
mockRootContext = jasmine.createSpyObj("context", [ "getParent" ]);
mockMutation = jasmine.createSpyObj("mutation", [ "invoke" ]);
mockPersistence = jasmine.createSpyObj("persistence", [ "persist" ]);
mockType = jasmine.createSpyObj("type", [ "hasFeature" ]);
mockNavigationService = jasmine.createSpyObj(
"navigationService",
[
"getNavigation",
"setNavigation",
"addListener",
"removeListener"
]
);
mockNavigationService.getNavigation.andReturn(mockDomainObject);
mockDomainObject.getId.andReturn("test");
mockDomainObject.getCapability.andReturn(mockContext);
mockContext.getParent.andReturn(mockParent);
mockType.hasFeature.andReturn(true);
capabilities = {
mutation: mockMutation,
persistence: mockPersistence,
type: mockType
};
model = {
composition: [ "a", "test", "b", "c" ]
composition: [ "a", "test", "b" ]
};
actionContext = { domainObject: mockDomainObject };
action = new RemoveAction(mockQ, actionContext);
action = new RemoveAction(mockQ, mockNavigationService, actionContext);
});
it("only applies to objects with parents", function () {
@ -123,11 +155,64 @@ define(
// Should have removed "test" - that was our
// mock domain object's id.
expect(result.composition).toEqual(["a", "b", "c"]);
expect(result.composition).toEqual(["a", "b"]);
// Finally, should have persisted
expect(mockPersistence.persist).toHaveBeenCalled();
});
it("removes parent of object currently navigated to", function () {
// Navigates to child object
mockNavigationService.getNavigation.andReturn(mockChildObject);
// Test is id of object being removed
// Child object has different id
mockDomainObject.getId.andReturn("test");
mockChildObject.getId.andReturn("not test");
// Sets context for the child and domainObject
mockDomainObject.getCapability.andReturn(mockContext);
mockChildObject.getCapability.andReturn(mockChildContext);
// Parents of child and domainObject are set
mockContext.getParent.andReturn(mockParent);
mockChildContext.getParent.andReturn(mockDomainObject);
mockType.hasFeature.andReturn(true);
action.perform();
// Expects navigation to parent of domainObject (removed object)
expect(mockNavigationService.setNavigation).toHaveBeenCalledWith(mockParent);
});
it("checks if removing object not in ascendent path (reaches ROOT)", function () {
// Navigates to grandchild of ROOT
mockNavigationService.getNavigation.andReturn(mockGrandchildObject);
// domainObject (grandparent) is set as ROOT, child and grandchild
// are set objects not being removed
mockDomainObject.getId.andReturn("test 1");
mockRootObject.getId.andReturn("ROOT");
mockChildObject.getId.andReturn("not test 2");
mockGrandchildObject.getId.andReturn("not test 3");
// Sets context for the grandchild, child, and domainObject
mockRootObject.getCapability.andReturn(mockRootContext);
mockChildObject.getCapability.andReturn(mockChildContext);
mockGrandchildObject.getCapability.andReturn(mockGrandchildContext);
// Parents of grandchild and child are set
mockChildContext.getParent.andReturn(mockRootObject);
mockGrandchildContext.getParent.andReturn(mockChildObject);
mockType.hasFeature.andReturn(true);
action.perform();
// Expects no navigation to occur
expect(mockNavigationService.setNavigation).not.toHaveBeenCalled();
});
});
}