From 541d187d6b341569ab74b0c9d7551d17527f6391 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 20 May 2016 03:39:11 +0200 Subject: [PATCH] Add StorageManager tests --- src/storage_manager/config/config.js | 44 +---------- src/storage_manager/model/LocalStorage.js | 20 ++--- src/storage_manager/model/RemoteStorage.js | 76 +++++++------------ test/specs/storage_manager/main.js | 6 +- test/specs/storage_manager/model/Models.js | 85 ++++++++++++++++++++++ 5 files changed, 125 insertions(+), 106 deletions(-) create mode 100644 test/specs/storage_manager/model/Models.js diff --git a/src/storage_manager/config/config.js b/src/storage_manager/config/config.js index 3c255971d..33959e0c1 100644 --- a/src/storage_manager/config/config.js +++ b/src/storage_manager/config/config.js @@ -15,13 +15,13 @@ define(function () { storeComponents: true, //Enable/Disable styles model (JSON format) - storeStyles: false, + storeStyles: true, //Enable/Disable saving HTML template - storeHtml: false, + storeHtml: true, //Enable/Disable saving HTML template - storeCss: false, + storeCss: true, // ONLY FOR LOCAL STORAGE // If enabled, checks if browser supports Local Storage @@ -43,43 +43,5 @@ define(function () { //Callback after request onComplete: function(jqXHR, status){}, - // Defaults for remote storage - remoteStorage: { - //Enable/Disable components model (JSON format) - storeComponents: true, - //Enable/Disable styles model (JSON format) - storeStyles: false, - //Enable/Disable saving HTML template - storeHTML: false, - /** - * Url where to save all stuff. - * The request will send a POST via AJAX, like this: - * { - * components: '', - * style: '', - * html: '', //if storeHTML is enabled - * } - * */ - urlStore: '', - /** - * Use this url to fetch model data, does expect in response something like this: - * { data: { - * components: '', - * style: '', - * } } - */ - urlLoad: '', - /** - * Url where assets will be send - * */ - urlUpload: '', - paramsStore :{}, //Custom parameters to pass with set request - paramsLoad :{}, //Custom parameters to pass with get request - beforeSend : function(jqXHR,settings){}, //Callback before request - onComplete : function(jqXHR,status){}, //Callback after request - }, - - // Defaults for local storage - localStorage: {}, }; }); \ No newline at end of file diff --git a/src/storage_manager/model/LocalStorage.js b/src/storage_manager/model/LocalStorage.js index fd2f03ef9..d4b44ae09 100644 --- a/src/storage_manager/model/LocalStorage.js +++ b/src/storage_manager/model/LocalStorage.js @@ -8,29 +8,19 @@ define(['backbone'], }, /** @inheritdoc */ - store : function(name, value) { + store: function(name, value) { this.checkStorageEnvironment(); - localStorage.setItem(name, value ); + localStorage.setItem(name, value); }, /** @inheritdoc */ load: function(name){ - var result = null; this.checkStorageEnvironment(); - if(localStorage.getItem(name)) - result = localStorage.getItem(name); - try{ - var prx = "Loading '" + name + "': "; - if(!result) - throw prx + ' Resource was not found'; - }catch(err){ - console.warn(err); - } - return result; + return localStorage.getItem(name); }, /** @inheritdoc */ - remove : function(name) { + remove: function(name) { this.checkStorageEnvironment(); localStorage.removeItem(name); }, @@ -40,7 +30,7 @@ define(['backbone'], * */ checkStorageEnvironment: function() { if(this.get('checkLocal') && !localStorage) - console.warn("Your browser doesn't support localStorage"); + console.warn("Your browser doesn't support localStorage"); }, }); diff --git a/src/storage_manager/model/RemoteStorage.js b/src/storage_manager/model/RemoteStorage.js index 75701b421..e85ed19d5 100644 --- a/src/storage_manager/model/RemoteStorage.js +++ b/src/storage_manager/model/RemoteStorage.js @@ -6,68 +6,48 @@ define(['backbone'], return Backbone.Model.extend({ defaults: { - urlLoad : 'http://localhost/load', - urlStore : 'http://localhost/store', - beforeSend : function(){}, - onComplete : function(){}, - paramsStore : {}, - paramsLoad : {}, - errorLoad : 'Response is not a valid JSON', + urlStore: '', + urlLoad: '', + params: {}, + beforeSend: function(){}, + onComplete: function(){}, }, /** @inheritdoc */ - store : function(name, value) { - var fd = new FormData(), - params = this.get('paramsStore'); - fd.append( name, value ); - for(var key in params){ - fd.append(key, params[key] ); - } + store: function(name, value) { + var fd = new FormData(), + params = this.get('params'); + fd.append(name, value); + + for(var key in params) + fd.append(key, params[key]); + $.ajax({ - url: this.get('urlStore'), - beforeSend: this.get('beforeSend'), - complete: this.get('onComplete'), - type: 'POST', - processData: false, - contentType: false, - data: fd, + url: this.get('urlStore'), + beforeSend: this.get('beforeSend'), + complete: this.get('onComplete'), + processData: false, + contentType: false, + type: 'POST', + data: fd, }); }, /** @inheritdoc */ load: function(name){ - var result = null, - t = this; + var result = null; $.ajax({ - url : this.get('urlLoad'), - beforeSend : this.get('beforeSend'), - complete : this.get('onComplete'), - data : this.get('paramsLoad'), - async : false, - type : 'GET', + url: this.get('urlLoad'), + beforeSend: this.get('beforeSend'), + complete: this.get('onComplete'), + data: this.get('paramsLoad'), + async: false, + type: 'GET', }).done(function(d){ - try{ - var prx = "Loading '" + name + "': "; - - if(typeof d !== 'object') - throw prx + t.get('errorLoad'); - - result = d.data ? d.data[name] : d[name]; - - if(!result) - throw prx + ' Resource was not found'; - - }catch(err){ - console.warn(err); - } + result = d.data ? d.data[name] : d[name]; }); return result; }, - /** @inheritdoc */ - remove : function(name) { - - }, - }); }); diff --git a/test/specs/storage_manager/main.js b/test/specs/storage_manager/main.js index 3b14b3ecb..1dd3d1f42 100644 --- a/test/specs/storage_manager/main.js +++ b/test/specs/storage_manager/main.js @@ -2,9 +2,11 @@ var modulePath = './../../../test/specs/storage_manager'; define([ 'StorageManager', + modulePath + '/model/Models', ], function( - StorageManager + StorageManager, + Models ) { describe('Storage Manager', function() { @@ -116,7 +118,7 @@ define([ }); - //Models.run(); + Models.run(); }); }); \ No newline at end of file diff --git a/test/specs/storage_manager/model/Models.js b/test/specs/storage_manager/model/Models.js new file mode 100644 index 000000000..855071a28 --- /dev/null +++ b/test/specs/storage_manager/model/Models.js @@ -0,0 +1,85 @@ +var path = 'StorageManager/model/'; +define([path + 'LocalStorage', + path + 'RemoteStorage'], + function(LocalStorage, RemoteStorage) { + + return { + run : function(){ + + describe('LocalStorage', function() { + + var obj; + var itemName = 'testItem'; + + beforeEach(function () { + obj = new LocalStorage(); + }); + + afterEach(function () { + delete obj; + }); + + it('Store and load item', function() { + obj.store(itemName, 'test'); + obj.load(itemName).should.equal('test'); + }); + + it('Remove item', function() { + obj.store(itemName, 'test'); + obj.remove(itemName); + (obj.load(itemName) === null).should.equal(true); + }); + + }); + + describe('RemoteStorage', function() { + + var obj; + var itemName = 'testItem'; + var endpointStore = 'testStoreEndpoint'; + var endpointLoad = 'testLoadEndpoint'; + var params = { test: 'testValue' }; + var storageOptions; + + beforeEach(function () { + storageOptions = { + urlStore: endpointStore, + urlLoad: endpointLoad, + params: params, + }; + obj = new RemoteStorage(storageOptions); + }); + + afterEach(function () { + $.ajax.restore(); + delete obj; + }); + + it('Store data', function() { + sinon.stub($, "ajax"); + obj.store(itemName, 'test'); + var data = params; + data.itemName = 'test'; + $.ajax.calledWithMatch({ + url: endpointStore, + }).should.equal(true); + }); + + it('Load data', function() { + sinon.stub($, "ajax").returns({ + done: function(){} + }); + obj.load(itemName); + var data = params; + $.ajax.calledWithMatch({ + url: endpointLoad, + }).should.equal(true); + }); + + }); + + } + + }; + +}); \ No newline at end of file