Added tests, and fixed failing ones

This commit is contained in:
Henry 2016-05-24 10:23:48 -07:00
parent 96af931c0b
commit f0ab817e87
5 changed files with 103 additions and 80 deletions

View File

@ -309,9 +309,7 @@ define([
"type": "provider", "type": "provider",
"implementation": CreateActionProvider, "implementation": CreateActionProvider,
"depends": [ "depends": [
"$q",
"typeService", "typeService",
"navigationService",
"policyService" "policyService"
] ]
}, },

View File

@ -43,11 +43,8 @@ define(
* override this) * override this)
* @param {ActionContext} context the context in which the * @param {ActionContext} context the context in which the
* action is being performed * action is being performed
* @param {NavigationService} navigationService the navigation service,
* which handles changes in navigation. It allows the object
* being browsed/edited to be set.
*/ */
function CreateAction(type, parent, context, $q, navigationService) { function CreateAction(type, parent, context) {
this.metadata = { this.metadata = {
key: 'create', key: 'create',
glyph: type.getGlyph(), glyph: type.getGlyph(),
@ -56,24 +53,8 @@ define(
description: type.getDescription(), description: type.getDescription(),
context: context context: context
}; };
this.type = type; this.type = type;
this.parent = parent; this.parent = parent;
this.navigationService = navigationService;
this.$q = $q;
}
// Get a count of views which are not flagged as non-editable.
function countEditableViews(domainObject) {
var views = domainObject && domainObject.useCapability('view'),
count = 0;
// A view is editable unless explicitly flagged as not
(views || []).forEach(function (view) {
count += (view.editable !== false) ? 1 : 0;
});
return count;
} }
/** /**
@ -82,7 +63,6 @@ define(
*/ */
CreateAction.prototype.perform = function () { CreateAction.prototype.perform = function () {
var newModel = this.type.getInitialModel(), var newModel = this.type.getInitialModel(),
parentObject = this.navigationService.getNavigation(),
newObject, newObject,
editAction, editAction,
editorCapability; editorCapability;
@ -96,16 +76,18 @@ define(
} }
newModel.type = this.type.getKey(); newModel.type = this.type.getKey();
newModel.location = parentObject.getId(); newModel.location = this.parent.getId();
newObject = parentObject.useCapability('instantiation', newModel); newObject = this.parent.useCapability('instantiation', newModel);
editorCapability = newObject.hasCapability('editor') && newObject.getCapability("editor"); editorCapability = newObject.hasCapability('editor') && newObject.getCapability("editor");
editAction = newObject.getCapability("action").getActions("edit")[0]; editAction = newObject.getCapability("action").getActions("edit")[0];
//If an edit action is available, perform it
if (editAction) { if (editAction) {
return editAction.perform("edit"); return editAction.perform();
} else if (editorCapability) { } else if (editorCapability) {
//otherwise, use the save action
editorCapability.edit(); editorCapability.edit();
return newObject.useCapability("action").perform("save").then(onSave, onCancel); return newObject.getCapability("action").perform("save").then(onSave, onCancel);
} }
}; };

View File

@ -44,10 +44,8 @@ define(
* introduced in this bundle), responsible for handling actual * introduced in this bundle), responsible for handling actual
* object creation. * object creation.
*/ */
function CreateActionProvider($q, typeService, navigationService, policyService) { function CreateActionProvider(typeService, policyService) {
this.typeService = typeService; this.typeService = typeService;
this.navigationService = navigationService;
this.$q = $q;
this.policyService = policyService; this.policyService = policyService;
} }
@ -72,9 +70,7 @@ define(
return new CreateAction( return new CreateAction(
type, type,
destination, destination,
context, context
self.$q,
self.navigationService
); );
}); });
}; };

View File

@ -29,13 +29,10 @@ define(
describe("The create action provider", function () { describe("The create action provider", function () {
var mockTypeService, var mockTypeService,
mockDialogService,
mockNavigationService,
mockPolicyService, mockPolicyService,
mockCreationPolicy, mockCreationPolicy,
mockPolicyMap = {}, mockPolicyMap = {},
mockTypes, mockTypes,
mockQ,
provider; provider;
function createMockType(name) { function createMockType(name) {
@ -61,14 +58,6 @@ define(
"typeService", "typeService",
["listTypes"] ["listTypes"]
); );
mockDialogService = jasmine.createSpyObj(
"dialogService",
["getUserInput"]
);
mockNavigationService = jasmine.createSpyObj(
"navigationService",
["setNavigation"]
);
mockPolicyService = jasmine.createSpyObj( mockPolicyService = jasmine.createSpyObj(
"policyService", "policyService",
["allow"] ["allow"]
@ -91,9 +80,7 @@ define(
mockTypeService.listTypes.andReturn(mockTypes); mockTypeService.listTypes.andReturn(mockTypes);
provider = new CreateActionProvider( provider = new CreateActionProvider(
mockQ,
mockTypeService, mockTypeService,
mockNavigationService,
mockPolicyService mockPolicyService
); );
}); });

View File

@ -31,8 +31,10 @@ define(
var mockType, var mockType,
mockParent, mockParent,
mockContext, mockContext,
mockDialogService, mockDomainObject,
mockCreationService, capabilities = {},
mockEditAction,
mockSaveAction,
action; action;
function mockPromise(value) { function mockPromise(value) {
@ -60,20 +62,61 @@ define(
[ [
"getId", "getId",
"getModel", "getModel",
"getCapability" "getCapability",
"useCapability"
] ]
); );
mockDomainObject = jasmine.createSpyObj(
"domainObject",
[
"getId",
"getModel",
"getCapability",
"hasCapability",
"useCapability"
]
);
mockDomainObject.hasCapability.andCallFake(function (name) {
return !!capabilities[name];
});
mockDomainObject.getCapability.andCallFake(function (name) {
return capabilities[name];
});
mockSaveAction = jasmine.createSpyObj(
"saveAction",
[
"perform"
]
);
capabilities.action = jasmine.createSpyObj(
"actionCapability",
[
"getActions",
"perform"
]
);
capabilities.editor = jasmine.createSpyObj(
"editorCapability",
[
"edit",
"save",
"cancel"
]
);
mockEditAction = jasmine.createSpyObj(
"editAction",
[
"perform"
]
);
mockContext = { mockContext = {
domainObject: mockParent domainObject: mockParent
}; };
mockDialogService = jasmine.createSpyObj( mockParent.useCapability.andReturn(mockDomainObject);
"dialogService",
["getUserInput"]
);
mockCreationService = jasmine.createSpyObj(
"creationService",
["createObject"]
);
mockType.getKey.andReturn("test"); mockType.getKey.andReturn("test");
mockType.getGlyph.andReturn("T"); mockType.getGlyph.andReturn("T");
@ -82,14 +125,10 @@ define(
mockType.getProperties.andReturn([]); mockType.getProperties.andReturn([]);
mockType.getInitialModel.andReturn({}); mockType.getInitialModel.andReturn({});
mockDialogService.getUserInput.andReturn(mockPromise({}));
action = new CreateAction( action = new CreateAction(
mockType, mockType,
mockParent, mockParent,
mockContext, mockContext
mockDialogService,
mockCreationService
); );
}); });
@ -101,28 +140,49 @@ define(
expect(metadata.glyph).toEqual("T"); expect(metadata.glyph).toEqual("T");
}); });
//TODO: Disabled for NEM Beta describe("the perform function", function () {
xit("invokes the creation service when performed", function () { beforeEach(function () {
action.perform(); capabilities.action.getActions.andReturn([mockEditAction]);
expect(mockCreationService.createObject).toHaveBeenCalledWith(
{ type: "test" },
mockParent
);
});
//TODO: Disabled for NEM Beta
xit("does not create an object if the user cancels", function () {
mockDialogService.getUserInput.andReturn({
then: function (callback, fail) {
fail();
}
}); });
action.perform(); it("uses the instantiation capability when performed", function () {
action.perform();
expect(mockParent.useCapability).toHaveBeenCalledWith("instantiation", jasmine.any(Object));
});
expect(mockCreationService.createObject) it("uses the edit action if available", function () {
.not.toHaveBeenCalled(); action.perform();
expect(mockEditAction.perform).toHaveBeenCalled();
});
it("uses the save action if object does not have an edit action" +
" available", function () {
capabilities.action.getActions.andReturn([]);
capabilities.action.perform.andReturn(mockPromise(undefined));
action.perform();
expect(capabilities.action.perform).toHaveBeenCalledWith("save");
});
describe("uses to editor capability", function () {
var promise = jasmine.createSpyObj("promise", ["then"]);
beforeEach(function () {
capabilities.action.getActions.andReturn([]);
capabilities.action.perform.andReturn(promise);
});
it("to save the edit if user saves dialog", function () {
action.perform();
expect(promise.then).toHaveBeenCalled();
promise.then.mostRecentCall.args[0]();
expect(capabilities.editor.save).toHaveBeenCalled();
});
it("to cancel the edit if user cancels dialog", function () {
action.perform();
promise.then.mostRecentCall.args[1]();
expect(capabilities.editor.cancel).toHaveBeenCalled();
});
});
}); });
}); });