2016-05-12 23:14:31 +00:00
|
|
|
/*****************************************************************************
|
2018-05-14 22:46:17 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2018, United States Government
|
2016-05-12 23:14:31 +00:00
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
2016-05-12 23:14:31 +00:00
|
|
|
* "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.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT includes source code licensed under additional open source
|
2016-05-12 23:14:31 +00:00
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
/*global define,describe,it,expect,beforeEach,jasmine*/
|
|
|
|
|
|
|
|
define(
|
|
|
|
["../../src/services/TransactionService"],
|
|
|
|
function (TransactionService) {
|
|
|
|
|
|
|
|
describe("The Transaction Service", function () {
|
|
|
|
var mockQ,
|
|
|
|
mockLog,
|
2020-08-10 16:47:04 +00:00
|
|
|
mockCacheService,
|
2016-05-12 23:14:31 +00:00
|
|
|
transactionService;
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
function fastPromise(val) {
|
2016-05-12 23:14:31 +00:00
|
|
|
return {
|
|
|
|
then: function (callback) {
|
|
|
|
return fastPromise(callback(val));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockQ = jasmine.createSpyObj("$q", ["all"]);
|
2020-08-10 16:47:04 +00:00
|
|
|
mockCacheService = jasmine.createSpyObj("cacheService", ["flush"]);
|
2018-06-30 00:32:59 +00:00
|
|
|
mockQ.all.and.returnValue(fastPromise());
|
2016-05-12 23:14:31 +00:00
|
|
|
mockLog = jasmine.createSpyObj("$log", ["error"]);
|
2020-08-10 16:47:04 +00:00
|
|
|
transactionService = new TransactionService(mockQ, mockLog, mockCacheService);
|
2016-05-12 23:14:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("isActive returns true if a transaction is in progress", function () {
|
|
|
|
expect(transactionService.isActive()).toBe(false);
|
|
|
|
transactionService.startTransaction();
|
|
|
|
expect(transactionService.isActive()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("addToTransaction queues onCommit and onCancel functions", function () {
|
|
|
|
var onCommit = jasmine.createSpy('onCommit'),
|
|
|
|
onCancel = jasmine.createSpy('onCancel');
|
|
|
|
|
|
|
|
transactionService.startTransaction();
|
|
|
|
transactionService.addToTransaction(onCommit, onCancel);
|
2016-07-25 23:45:32 +00:00
|
|
|
expect(transactionService.size()).toBe(1);
|
2016-05-12 23:14:31 +00:00
|
|
|
});
|
|
|
|
|
2016-05-12 21:20:16 +00:00
|
|
|
it("size function returns size of commit and cancel queues", function () {
|
|
|
|
var onCommit = jasmine.createSpy('onCommit'),
|
|
|
|
onCancel = jasmine.createSpy('onCancel');
|
|
|
|
|
|
|
|
transactionService.startTransaction();
|
|
|
|
transactionService.addToTransaction(onCommit, onCancel);
|
|
|
|
transactionService.addToTransaction(onCommit, onCancel);
|
|
|
|
transactionService.addToTransaction(onCommit, onCancel);
|
|
|
|
expect(transactionService.size()).toBe(3);
|
|
|
|
});
|
|
|
|
|
2016-05-12 23:14:31 +00:00
|
|
|
describe("commit", function () {
|
|
|
|
var onCommits;
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
onCommits = [0, 1, 2].map(function (val) {
|
2018-08-07 21:47:50 +00:00
|
|
|
return jasmine.createSpy("onCommit" + val);
|
|
|
|
});
|
2016-05-12 23:14:31 +00:00
|
|
|
|
|
|
|
transactionService.startTransaction();
|
|
|
|
onCommits.forEach(transactionService.addToTransaction.bind(transactionService));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("commit calls all queued commit functions", function () {
|
2016-07-25 23:45:32 +00:00
|
|
|
expect(transactionService.size()).toBe(3);
|
2020-08-10 16:47:04 +00:00
|
|
|
|
|
|
|
return transactionService.commit().then(() => {
|
|
|
|
onCommits.forEach(function (spy) {
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
});
|
2016-05-12 23:14:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("commit resets active state and clears queues", function () {
|
2020-08-10 16:47:04 +00:00
|
|
|
return transactionService.commit().then(() => {
|
|
|
|
expect(transactionService.isActive()).toBe(false);
|
|
|
|
expect(transactionService.size()).toBe(0);
|
|
|
|
expect(transactionService.size()).toBe(0);
|
|
|
|
});
|
2016-05-12 23:14:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("cancel", function () {
|
|
|
|
var onCancels;
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
onCancels = [0, 1, 2].map(function (val) {
|
2016-05-12 23:14:31 +00:00
|
|
|
return jasmine.createSpy("onCancel" + val);
|
|
|
|
});
|
|
|
|
|
|
|
|
transactionService.startTransaction();
|
|
|
|
onCancels.forEach(function (onCancel) {
|
|
|
|
transactionService.addToTransaction(undefined, onCancel);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("cancel calls all queued cancel functions", function () {
|
2016-07-25 23:45:32 +00:00
|
|
|
expect(transactionService.size()).toBe(3);
|
2016-05-12 23:14:31 +00:00
|
|
|
transactionService.cancel();
|
2016-05-19 18:29:13 +00:00
|
|
|
onCancels.forEach(function (spy) {
|
2016-05-12 23:14:31 +00:00
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("cancel resets active state and clears queues", function () {
|
|
|
|
transactionService.cancel();
|
|
|
|
expect(transactionService.isActive()).toBe(false);
|
2016-07-25 23:45:32 +00:00
|
|
|
expect(transactionService.size()).toBe(0);
|
2016-05-12 23:14:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2016-05-19 18:29:13 +00:00
|
|
|
);
|