Added tests

This commit is contained in:
Henry
2016-05-12 14:20:16 -07:00
parent c305fba0a7
commit 69c4c3a2c8
7 changed files with 68 additions and 23 deletions

View File

@ -271,7 +271,8 @@ define([
"depends": [ "depends": [
"$q", "$q",
"transactionService" "transactionService"
] ],
"priority": "fallback"
}, },
{ {
"type": "provider", "type": "provider",

View File

@ -49,6 +49,7 @@ define(
this.persistenceCapability = persistenceCapability; this.persistenceCapability = persistenceCapability;
this.domainObject = domainObject; this.domainObject = domainObject;
this.$q = $q; this.$q = $q;
this.persistPending = false;
} }
/** /**
@ -57,11 +58,27 @@ define(
* @returns {*} * @returns {*}
*/ */
TransactionalPersistenceCapability.prototype.persist = function () { TransactionalPersistenceCapability.prototype.persist = function () {
var self = this;
function onCommit() {
return self.persistenceCapability.persist().then(function(result) {
self.persistPending = false;
return result;
});
}
function onCancel() {
return self.persistenceCapability.refresh().then(function(result) {
self.persistPending = false;
return result;
});
}
if (this.transactionService.isActive()) { if (this.transactionService.isActive()) {
this.transactionService.addToTransaction( if (!this.persistPending) {
this.persistenceCapability.persist.bind(this.persistenceCapability), this.transactionService.addToTransaction(onCommit, onCancel);
this.persistenceCapability.refresh.bind(this.persistenceCapability) this.persistPending = true;
); }
//Need to return a promise from this function //Need to return a promise from this function
return this.$q.when(true); return this.$q.when(true);
} else { } else {

View File

@ -141,7 +141,7 @@ define(
}; };
TransactionService.prototype.size = function () { TransactionService.prototype.size = function () {
return this.onCommits.length + this.onCancels.length; return this.onCommits.length;
}; };
return TransactionService; return TransactionService;

View File

@ -55,6 +55,7 @@ define(
"transactionService", "transactionService",
[ [
"startTransaction", "startTransaction",
"size",
"commit", "commit",
"cancel" "cancel"
] ]
@ -161,11 +162,9 @@ define(
}); });
it("returns true if the object has been modified since it" + it("returns true if the object has been modified since it" +
" was last persisted", function () { " was last persisted", function () {
model.modified = 0; mockTransactionService.size.andReturn(0);
model.persisted = 0;
expect(capability.dirty()).toBe(false); expect(capability.dirty()).toBe(false);
mockTransactionService.size.andReturn(1);
model.modified = 1;
expect(capability.dirty()).toBe(true); expect(capability.dirty()).toBe(true);
}); });
}); });

View File

@ -56,7 +56,8 @@ define(
"persistenceCapability", "persistenceCapability",
["persist", "refresh"] ["persist", "refresh"]
); );
mockPersistence.persist.andReturn(fastPromise());
mockPersistence.refresh.andReturn(fastPromise());
capability = new TransactionalPersistenceCapability(mockQ, mockTransactionService, mockPersistence, mockDomainObject); capability = new TransactionalPersistenceCapability(mockQ, mockTransactionService, mockPersistence, mockDomainObject);
}); });
@ -67,26 +68,26 @@ define(
expect(mockPersistence.persist).toHaveBeenCalled(); expect(mockPersistence.persist).toHaveBeenCalled();
}); });
it("if transaction is active, persist call is queued", function() { it("if transaction is active, persist and cancel calls are" +
" queued", function() {
mockTransactionService.isActive.andReturn(true); mockTransactionService.isActive.andReturn(true);
capability.persist(); capability.persist();
expect(mockTransactionService.addToTransaction).toHaveBeenCalled(); expect(mockTransactionService.addToTransaction).toHaveBeenCalled();
//Test that it was the persist call that was queued
mockTransactionService.addToTransaction.mostRecentCall.args[0](); mockTransactionService.addToTransaction.mostRecentCall.args[0]();
expect(mockPersistence.persist).toHaveBeenCalled(); expect(mockPersistence.persist).toHaveBeenCalled();
});
it("if transaction is active, refresh call is queued as cancel" +
" function", function() {
mockTransactionService.isActive.andReturn(true);
capability.persist();
//Test that it was the persist call that was queued
mockTransactionService.addToTransaction.mostRecentCall.args[1](); mockTransactionService.addToTransaction.mostRecentCall.args[1]();
expect(mockPersistence.refresh).toHaveBeenCalled(); expect(mockPersistence.refresh).toHaveBeenCalled();
}); });
it("persist call is only added to transaction once", function() {
mockTransactionService.isActive.andReturn(true);
capability.persist();
expect(mockTransactionService.addToTransaction).toHaveBeenCalled();
capability.persist();
expect(mockTransactionService.addToTransaction.calls.length).toBe(1);
});
}); });
} }
); );

View File

@ -62,6 +62,17 @@ define(
expect(transactionService.onCancels.length).toBe(1); expect(transactionService.onCancels.length).toBe(1);
}); });
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);
});
describe("commit", function () { describe("commit", function () {
var onCommits; var onCommits;

View File

@ -236,7 +236,7 @@ define(
expect(mockLog.warn).toHaveBeenCalled(); expect(mockLog.warn).toHaveBeenCalled();
}); });
it("clears out obsolete peroperties from scope", function () { it("clears out obsolete properties from scope", function () {
mockScope.key = "def"; mockScope.key = "def";
mockScope.domainObject = mockDomainObject; mockScope.domainObject = mockDomainObject;
mockDomainObject.useCapability.andReturn("some value"); mockDomainObject.useCapability.andReturn("some value");
@ -253,6 +253,21 @@ define(
expect(mockScope.testCapability).toBeUndefined(); expect(mockScope.testCapability).toBeUndefined();
}); });
it("registers a status change listener", function () {
mockScope.$watch.calls[2].args[1](mockDomainObject);
expect(mockStatusCapability.listen).toHaveBeenCalled();
});
it("unlistens for status change on scope destruction", function () {
var mockUnlistener = jasmine.createSpy("unlisten");
mockStatusCapability.listen.andReturn(mockUnlistener);
mockScope.$watch.calls[2].args[1](mockDomainObject);
expect(mockStatusCapability.listen).toHaveBeenCalled();
mockScope.$on.calls[1].args[1]();
expect(mockUnlistener).toHaveBeenCalled();
});
describe("when a domain object has been observed", function () { describe("when a domain object has been observed", function () {
var mockContext, var mockContext,
mockContext2, mockContext2,
@ -314,6 +329,7 @@ define(
mockScope.$watch.calls[0].args[1](); mockScope.$watch.calls[0].args[1]();
expect(mockChangeTemplate.calls.length).toEqual(callCount); expect(mockChangeTemplate.calls.length).toEqual(callCount);
}); });
}); });