Issue #277 : Add unit test for StorageService

This commit is contained in:
jdescottes 2015-09-25 01:41:22 +02:00
parent fe9875841a
commit acb6fd2172
2 changed files with 128 additions and 1 deletions

View File

@ -39,7 +39,7 @@
};
ns.StorageService.prototype.delegateSave_ = function(delegatedService, piskel, saveAsNew) {
if (this.isSaving_) {
if (this.savingFlag_) {
return Q.reject('Already saving');
}

View File

@ -0,0 +1,127 @@
describe("Storage Service test suite", function() {
var storageService = null;
var piskel = {};
beforeEach(function() {
pskl.app.galleryStorageService = {
save : function () {}
};
pskl.app.desktopStorageService = {
save : function () {}
};
pskl.app.fileDownloadStorageService = {
save : function () {}
};
pskl.app.localStorageService = {
save : function () {}
};
pskl.app.shortcutService = {
addShortcut : function () {}
};
storageService = new pskl.service.storage.StorageService();
storageService.init();
});
var checkSubServiceSuccessfulSave = function (service, methodName, done) {
spyOn(service, 'save').and.returnValue(Q.resolve());
storageService[methodName](piskel).then(function () {
expect(service.save).toHaveBeenCalledWith(piskel, undefined);
}, function (err) {
expect(false).toBe(true, 'Error callback should not have been called');
}).then(function () {
done();
});
};
var checkSubServiceFailedSave = function (service, methodName, done) {
spyOn(service, 'save').and.returnValue(Q.reject());
storageService[methodName](piskel).then(function () {
expect(false).toBe(true, 'Success callback should not have been called');
},function () {
expect(service.save).toHaveBeenCalledWith(piskel, undefined);
}).then(function () {
done();
});
};
// GalleryStorage
it("calls GalleryStorage#save in saveToGallery", function(done) {
checkSubServiceSuccessfulSave(pskl.app.galleryStorageService, 'saveToGallery', done);
});
it("calls GalleryStorage#save in saveToGallery - error case", function(done) {
checkSubServiceFailedSave(pskl.app.galleryStorageService, 'saveToGallery', done);
});
// DesktopStorage
it("calls DesktopStorage#save in saveToFileNodeWebkit", function(done) {
checkSubServiceSuccessfulSave(pskl.app.desktopStorageService, 'saveToFileNodeWebkit', done);
});
it("calls DesktopStorage#save in saveToFileNodeWebkit - error case", function(done) {
checkSubServiceFailedSave(pskl.app.desktopStorageService, 'saveToFileNodeWebkit', done);
});
// FileDownloadStorage
it("calls FileDownloadStorage#save in saveToFileBrowser", function(done) {
checkSubServiceSuccessfulSave(pskl.app.fileDownloadStorageService, 'saveToFileBrowser', done);
});
it("calls FileDownloadStorage#save in saveToFileBrowser - error case", function(done) {
checkSubServiceFailedSave(pskl.app.fileDownloadStorageService, 'saveToFileBrowser', done);
});
// LocalStorage
it("calls LocalStorage#save in saveToLocalStorage", function(done) {
checkSubServiceSuccessfulSave(pskl.app.localStorageService, 'saveToLocalStorage', done);
});
it("calls LocalStorage#save in saveToLocalStorage - error case", function(done) {
checkSubServiceFailedSave(pskl.app.localStorageService, 'saveToLocalStorage', done);
});
it("updates saving status properly", function(done) {
var deferred = Q.defer();
spyOn(pskl.app.galleryStorageService, 'save').and.returnValue(deferred.promise);
// check storageService is not in saving mode
expect(storageService.isSaving()).toBe(false);
// save
var storageServicePromise = storageService.saveToGallery(piskel);
// storageService is now in saving mode
expect(storageService.isSaving()).toBe(true);
// we have called save once
expect(pskl.app.galleryStorageService.save.calls.count()).toBe(1);
// call save again, should be ignored
storageService.saveToGallery(piskel);
expect(pskl.app.galleryStorageService.save.calls.count()).toBe(1);
deferred.resolve();
storageServicePromise.then(function () {
// after saving, isSaving() should be false again
expect(storageService.isSaving()).toBe(false);
done();
});
});
it("updates saving status on BEFORE_SAVING_PISKEL and AFTER_SAVING_PISKEL events", function() {
spyOn(pskl.app.galleryStorageService, 'save').and.returnValue(Q.resolve());
// check storageService is not in saving mode
expect(storageService.isSaving()).toBe(false);
// trigger before save event
$.publish(Events.BEFORE_SAVING_PISKEL);
expect(storageService.isSaving()).toBe(true);
// call save, should have been ignored
storageService.saveToGallery(piskel);
expect(pskl.app.galleryStorageService.save.calls.count()).toBe(0);
// trigger before save event
$.publish(Events.AFTER_SAVING_PISKEL);
expect(storageService.isSaving()).toBe(false);
});
});