openmct/platform/commonUI/edit/test/capabilities/EditorCapabilitySpec.js

173 lines
6.9 KiB
JavaScript
Raw Normal View History

/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define(
["../../src/capabilities/EditorCapability"],
function (EditorCapability) {
describe("The editor capability", function () {
2016-05-12 23:14:31 +00:00
var mockDomainObject,
capabilities,
mockParentObject,
mockTransactionService,
mockStatusCapability,
mockParentStatus,
mockContextCapability,
capability;
2016-05-12 23:14:31 +00:00
function fastPromise(val) {
return {
then: function (callback) {
return callback(val);
}
};
2016-05-12 23:14:31 +00:00
}
beforeEach(function () {
mockDomainObject = jasmine.createSpyObj(
"domainObject",
2016-05-12 23:14:31 +00:00
["getId", "getModel", "hasCapability", "getCapability", "useCapability"]
);
2016-05-12 23:14:31 +00:00
mockParentObject = jasmine.createSpyObj(
"domainObject",
["getId", "getModel", "hasCapability", "getCapability", "useCapability"]
);
mockTransactionService = jasmine.createSpyObj(
"transactionService",
[
"startTransaction",
2016-05-12 21:20:16 +00:00
"size",
2016-05-12 23:14:31 +00:00
"commit",
"cancel"
]
);
2016-05-12 23:14:31 +00:00
mockTransactionService.commit.andReturn(fastPromise());
mockTransactionService.cancel.andReturn(fastPromise());
2016-05-12 23:14:31 +00:00
mockStatusCapability = jasmine.createSpyObj(
"statusCapability",
["get", "set"]
);
mockParentStatus = jasmine.createSpyObj(
"statusCapability",
["get", "set"]
);
mockContextCapability = jasmine.createSpyObj(
"contextCapability",
["getParent"]
);
mockContextCapability.getParent.andReturn(mockParentObject);
capabilities = {
context: mockContextCapability,
status: mockStatusCapability
};
mockDomainObject.hasCapability.andCallFake(function(name) {
return capabilities[name] !== undefined;
});
mockDomainObject.getCapability.andCallFake(function (name) {
return capabilities[name];
});
2016-05-12 23:14:31 +00:00
mockParentObject.getCapability.andReturn(mockParentStatus);
mockParentObject.hasCapability.andReturn(false);
capability = new EditorCapability(
2016-05-12 23:14:31 +00:00
mockTransactionService,
mockDomainObject
);
});
2016-05-12 23:14:31 +00:00
it("starts a transaction when edit is invoked", function () {
capability.edit();
expect(mockTransactionService.startTransaction).toHaveBeenCalled();
});
2016-05-12 23:14:31 +00:00
it("sets editing status on object", function () {
capability.edit();
expect(mockStatusCapability.set).toHaveBeenCalledWith("editing", true);
});
2016-05-12 23:14:31 +00:00
it("uses editing status to determine editing context root", function () {
capability.edit();
mockStatusCapability.get.andReturn(false);
expect(capability.isEditContextRoot()).toBe(false);
mockStatusCapability.get.andReturn(true);
expect(capability.isEditContextRoot()).toBe(true);
});
2016-05-12 23:14:31 +00:00
it("inEditingContext returns true if parent object is being" +
" edited", function () {
mockStatusCapability.get.andReturn(false);
mockParentStatus.get.andReturn(false);
expect(capability.inEditContext()).toBe(false);
mockParentStatus.get.andReturn(true);
expect(capability.inEditContext()).toBe(true);
});
2016-05-12 23:14:31 +00:00
describe("save", function() {
beforeEach(function() {
capability.edit();
capability.save();
});
it("commits the transaction", function () {
expect(mockTransactionService.commit).toHaveBeenCalled();
});
it("resets the edit state", function () {
expect(mockStatusCapability.set).toHaveBeenCalledWith('editing', false);
});
});
2016-05-12 23:14:31 +00:00
describe("cancel", function() {
beforeEach(function() {
capability.edit();
capability.cancel();
});
it("cancels the transaction", function () {
expect(mockTransactionService.cancel).toHaveBeenCalled();
});
it("resets the edit state", function () {
expect(mockStatusCapability.set).toHaveBeenCalledWith('editing', false);
});
});
2016-05-12 23:14:31 +00:00
describe("dirty", function() {
var model = {};
2016-05-12 23:14:31 +00:00
beforeEach(function() {
mockDomainObject.getModel.andReturn(model);
capability.edit();
capability.cancel();
});
it("returns true if the object has been modified since it" +
" was last persisted", function () {
2016-05-12 21:20:16 +00:00
mockTransactionService.size.andReturn(0);
2016-05-12 23:14:31 +00:00
expect(capability.dirty()).toBe(false);
2016-05-12 21:20:16 +00:00
mockTransactionService.size.andReturn(1);
2016-05-12 23:14:31 +00:00
expect(capability.dirty()).toBe(true);
});
});
});
}
);