Browse Source

Add StorageManager tests

pull/36/head
Artur Arseniev 10 years ago
parent
commit
541d187d6b
  1. 44
      src/storage_manager/config/config.js
  2. 20
      src/storage_manager/model/LocalStorage.js
  3. 76
      src/storage_manager/model/RemoteStorage.js
  4. 6
      test/specs/storage_manager/main.js
  5. 85
      test/specs/storage_manager/model/Models.js

44
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: {},
};
});

20
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");
},
});

76
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) {
},
});
});

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

85
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);
});
});
}
};
});
Loading…
Cancel
Save