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": [
"$q",
"transactionService"
]
],
"priority": "fallback"
},
{
"type": "provider",

View File

@ -49,6 +49,7 @@ define(
this.persistenceCapability = persistenceCapability;
this.domainObject = domainObject;
this.$q = $q;
this.persistPending = false;
}
/**
@ -57,11 +58,27 @@ define(
* @returns {*}
*/
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()) {
this.transactionService.addToTransaction(
this.persistenceCapability.persist.bind(this.persistenceCapability),
this.persistenceCapability.refresh.bind(this.persistenceCapability)
);
if (!this.persistPending) {
this.transactionService.addToTransaction(onCommit, onCancel);
this.persistPending = true;
}
//Need to return a promise from this function
return this.$q.when(true);
} else {

View File

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

View File

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

View File

@ -56,7 +56,8 @@ define(
"persistenceCapability",
["persist", "refresh"]
);
mockPersistence.persist.andReturn(fastPromise());
mockPersistence.refresh.andReturn(fastPromise());
capability = new TransactionalPersistenceCapability(mockQ, mockTransactionService, mockPersistence, mockDomainObject);
});
@ -67,26 +68,26 @@ define(
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);
capability.persist();
expect(mockTransactionService.addToTransaction).toHaveBeenCalled();
//Test that it was the persist call that was queued
mockTransactionService.addToTransaction.mostRecentCall.args[0]();
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]();
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);
});
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 () {
var onCommits;

View File

@ -236,7 +236,7 @@ define(
expect(mockLog.warn).toHaveBeenCalled();
});
it("clears out obsolete peroperties from scope", function () {
it("clears out obsolete properties from scope", function () {
mockScope.key = "def";
mockScope.domainObject = mockDomainObject;
mockDomainObject.useCapability.andReturn("some value");
@ -253,6 +253,21 @@ define(
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 () {
var mockContext,
mockContext2,
@ -314,6 +329,7 @@ define(
mockScope.$watch.calls[0].args[1]();
expect(mockChangeTemplate.calls.length).toEqual(callCount);
});
});