piskel/test/js/service/storage/StorageServiceTest.js
2015-09-25 01:41:22 +02:00

127 lines
No EOL
4.5 KiB
JavaScript

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);
});
});