2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
2014-11-25 01:01:37 +00:00
|
|
|
|
|
|
|
define(
|
2014-11-25 18:51:23 +00:00
|
|
|
["../../src/capabilities/EditorCapability"],
|
|
|
|
function (EditorCapability) {
|
2014-11-25 01:01:37 +00:00
|
|
|
|
2015-03-24 23:31:14 +00:00
|
|
|
describe("The editor capability", function () {
|
2016-05-12 23:14:31 +00:00
|
|
|
var mockDomainObject,
|
|
|
|
capabilities,
|
|
|
|
mockParentObject,
|
|
|
|
mockTransactionService,
|
|
|
|
mockStatusCapability,
|
|
|
|
mockParentStatus,
|
|
|
|
mockContextCapability,
|
2014-11-25 18:51:23 +00:00
|
|
|
capability;
|
|
|
|
|
2016-05-12 23:14:31 +00:00
|
|
|
function fastPromise(val) {
|
|
|
|
return {
|
|
|
|
then: function (callback) {
|
|
|
|
return callback(val);
|
|
|
|
}
|
2014-11-25 18:51:23 +00:00
|
|
|
};
|
2016-05-12 23:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2014-11-25 18:51:23 +00:00
|
|
|
mockDomainObject = jasmine.createSpyObj(
|
|
|
|
"domainObject",
|
2016-05-12 23:14:31 +00:00
|
|
|
["getId", "getModel", "hasCapability", "getCapability", "useCapability"]
|
2014-11-25 18:51:23 +00:00
|
|
|
);
|
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"
|
|
|
|
]
|
2014-11-25 18:51:23 +00:00
|
|
|
);
|
2016-05-12 23:14:31 +00:00
|
|
|
mockTransactionService.commit.andReturn(fastPromise());
|
|
|
|
mockTransactionService.cancel.andReturn(fastPromise());
|
2014-11-25 18:51:23 +00:00
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
mockDomainObject.hasCapability.andCallFake(function (name) {
|
2016-05-12 23:14:31 +00:00
|
|
|
return capabilities[name] !== undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
mockDomainObject.getCapability.andCallFake(function (name) {
|
|
|
|
return capabilities[name];
|
|
|
|
});
|
2015-03-24 23:31:14 +00:00
|
|
|
|
2016-05-12 23:14:31 +00:00
|
|
|
mockParentObject.getCapability.andReturn(mockParentStatus);
|
|
|
|
mockParentObject.hasCapability.andReturn(false);
|
2014-11-25 18:51:23 +00:00
|
|
|
|
|
|
|
capability = new EditorCapability(
|
2016-05-12 23:14:31 +00:00
|
|
|
mockTransactionService,
|
|
|
|
mockDomainObject
|
2014-11-25 18:51:23 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-05-12 23:14:31 +00:00
|
|
|
it("starts a transaction when edit is invoked", function () {
|
|
|
|
capability.edit();
|
|
|
|
expect(mockTransactionService.startTransaction).toHaveBeenCalled();
|
2014-11-25 18:51:23 +00:00
|
|
|
});
|
|
|
|
|
2016-05-12 23:14:31 +00:00
|
|
|
it("sets editing status on object", function () {
|
|
|
|
capability.edit();
|
|
|
|
expect(mockStatusCapability.set).toHaveBeenCalledWith("editing", true);
|
|
|
|
});
|
2014-11-25 18:51:23 +00:00
|
|
|
|
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);
|
2014-11-25 18:51:23 +00:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|
2014-11-25 18:51:23 +00:00
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
describe("save", function () {
|
|
|
|
beforeEach(function () {
|
2016-05-12 23:14:31 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2014-11-25 18:51:23 +00:00
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
describe("cancel", function () {
|
|
|
|
beforeEach(function () {
|
2016-05-12 23:14:31 +00:00
|
|
|
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);
|
2014-11-25 18:51:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
describe("dirty", function () {
|
2016-05-12 23:14:31 +00:00
|
|
|
var model = {};
|
2014-11-25 01:01:37 +00:00
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
beforeEach(function () {
|
2016-05-12 23:14:31 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2014-11-25 01:01:37 +00:00
|
|
|
});
|
|
|
|
}
|
2016-05-19 18:29:13 +00:00
|
|
|
);
|