mirror of https://github.com/artf/grapesjs.git
5 changed files with 244 additions and 236 deletions
@ -0,0 +1,120 @@ |
|||
const StorageManager = require('storage_manager'); |
|||
const Models = require('./model/Models'); |
|||
|
|||
describe('Storage Manager', () => { |
|||
|
|||
describe('Main', () => { |
|||
|
|||
var obj; |
|||
|
|||
beforeEach(() => { |
|||
obj = new StorageManager().init(); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
obj = null; |
|||
}); |
|||
|
|||
it('Object exists', () => { |
|||
expect(StorageManager).toExist(); |
|||
}); |
|||
|
|||
it('Autosave is active by default', () => { |
|||
expect(obj.isAutosave()).toEqual(true); |
|||
}); |
|||
|
|||
it('Change autosave', () => { |
|||
obj.setAutosave(0); |
|||
expect(obj.isAutosave()).toEqual(false); |
|||
}); |
|||
|
|||
it('Steps before save are set as default', () => { |
|||
expect(obj.getStepsBeforeSave()).toEqual(1); |
|||
}); |
|||
|
|||
it('Change steps before save', () => { |
|||
obj.setStepsBeforeSave(5); |
|||
expect(obj.getStepsBeforeSave()).toEqual(5); |
|||
}); |
|||
|
|||
it('No storages inside', () => { |
|||
expect(obj.getStorages()).toEqual({}); |
|||
}); |
|||
|
|||
it('Add and get new storage', () => { |
|||
obj.add('test', 'gen'); |
|||
expect(obj.get('test')).toEqual('gen'); |
|||
}); |
|||
|
|||
it('LocalStorage is set as default', () => { |
|||
expect(obj.getCurrent()).toEqual('local'); |
|||
}); |
|||
|
|||
it('Change storage type', () => { |
|||
obj.setCurrent('remote'); |
|||
expect(obj.getCurrent()).toEqual('remote'); |
|||
}); |
|||
|
|||
it('Store do not execute if empty', () => { |
|||
expect(obj.store({item:'test'})).toEqual(null); |
|||
}); |
|||
|
|||
it('Load do not execute if empty', () => { |
|||
expect(obj.load(['item'])).toEqual({}); |
|||
}); |
|||
|
|||
it('Load default storages ', () => { |
|||
obj.loadDefaultProviders(); |
|||
expect(obj.get('local')).toExist(); |
|||
expect(obj.get('remote')).toExist(); |
|||
expect(obj.get('test')).toNotExist(); |
|||
}); |
|||
|
|||
describe('With custom storage', () => { |
|||
|
|||
var storeValue; |
|||
var storageId = 'testStorage'; |
|||
var storage = { |
|||
store(data) { |
|||
storeValue = data; |
|||
}, |
|||
load(keys) { |
|||
return storeValue; |
|||
}, |
|||
}; |
|||
|
|||
beforeEach(() => { |
|||
storeValue = []; |
|||
obj = new StorageManager().init({ |
|||
type: storageId, |
|||
}); |
|||
obj.add(storageId, storage); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
obj = null; |
|||
}); |
|||
|
|||
it('Store and load data', () => { |
|||
var data = { |
|||
item: 'testData', |
|||
item2: 'testData2', |
|||
}; |
|||
var data2 = {}; |
|||
var id = obj.getConfig().id; |
|||
data2[id + 'item'] = 'testData'; |
|||
data2[id + 'item2'] = 'testData2'; |
|||
|
|||
obj.store(data); |
|||
var load = obj.load(['item', 'item2']); |
|||
expect(storeValue).toEqual(data2); |
|||
expect(load).toEqual(data); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
}); |
|||
|
|||
Models.run(); |
|||
|
|||
}); |
|||
@ -1,123 +0,0 @@ |
|||
define(function(require, exports, module){ |
|||
'use strict'; |
|||
var StorageManager = require('StorageManager'); |
|||
var Models = require('undefined'); |
|||
|
|||
describe('Storage Manager', function() { |
|||
|
|||
describe('Main', function() { |
|||
|
|||
var obj; |
|||
|
|||
beforeEach(function () { |
|||
obj = new StorageManager().init(); |
|||
}); |
|||
|
|||
afterEach(function () { |
|||
delete obj; |
|||
}); |
|||
|
|||
it('Object exists', function() { |
|||
StorageManager.should.be.exist; |
|||
}); |
|||
|
|||
it('Autosave is active by default', function() { |
|||
obj.isAutosave().should.equal(true); |
|||
}); |
|||
|
|||
it('Change autosave', function() { |
|||
obj.setAutosave(0); |
|||
obj.isAutosave().should.equal(false); |
|||
}); |
|||
|
|||
it('Steps before save are set as default', function() { |
|||
obj.getStepsBeforeSave().should.equal(1); |
|||
}); |
|||
|
|||
it('Change steps before save', function() { |
|||
obj.setStepsBeforeSave(5); |
|||
obj.getStepsBeforeSave().should.equal(5); |
|||
}); |
|||
|
|||
it('No storages inside', function() { |
|||
obj.getStorages().should.be.empty; |
|||
}); |
|||
|
|||
it('Add and get new storage', function() { |
|||
obj.add('test', 'gen'); |
|||
obj.get('test').should.equal('gen'); |
|||
}); |
|||
|
|||
it('LocalStorage is set as default', function() { |
|||
obj.getCurrent().should.equal('local'); |
|||
}); |
|||
|
|||
it('Change storage type', function() { |
|||
obj.setCurrent('remote'); |
|||
obj.getCurrent().should.equal('remote'); |
|||
}); |
|||
|
|||
it('Store do not execute if empty', function() { |
|||
(obj.store({item:'test'}) === null).should.equal(true); |
|||
}); |
|||
|
|||
it('Load do not execute if empty', function() { |
|||
obj.load(['item']).should.be.empty; |
|||
}); |
|||
|
|||
it('Load default storages ', function() { |
|||
obj.loadDefaultProviders(); |
|||
obj.get('local').should.not.be.empty; |
|||
obj.get('remote').should.not.be.empty; |
|||
(obj.get('test') === null).should.equal(true); |
|||
}); |
|||
|
|||
describe('With custom storage', function() { |
|||
|
|||
var storeValue; |
|||
var storageId = 'testStorage'; |
|||
var storage = { |
|||
store: function(data){ |
|||
storeValue = data; |
|||
}, |
|||
load: function(keys){ |
|||
return storeValue; |
|||
}, |
|||
}; |
|||
|
|||
beforeEach(function () { |
|||
storeValue = []; |
|||
obj = new StorageManager().init({ |
|||
type: storageId, |
|||
}); |
|||
obj.add(storageId, storage); |
|||
}); |
|||
|
|||
afterEach(function () { |
|||
delete obj; |
|||
}); |
|||
|
|||
it('Store and load data', function() { |
|||
var data = { |
|||
item: 'testData', |
|||
item2: 'testData2', |
|||
}; |
|||
var data2 = {}; |
|||
var id = obj.getConfig().id; |
|||
data2[id + 'item'] = 'testData'; |
|||
data2[id + 'item2'] = 'testData2'; |
|||
|
|||
obj.store(data); |
|||
var load = obj.load(['item', 'item2']); |
|||
storeValue.should.deep.equal(data2); |
|||
load.should.deep.equal(data); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
}); |
|||
|
|||
Models.run(); |
|||
|
|||
}); |
|||
}); |
|||
@ -1,119 +1,116 @@ |
|||
define(function(require, exports, module){ |
|||
'use strict'; |
|||
var RemoteStorage = require('undefined'); |
|||
|
|||
module.exports = { |
|||
run : function(){ |
|||
|
|||
describe('LocalStorage', function() { |
|||
|
|||
var obj; |
|||
var itemName = 'testItem'; |
|||
var data = { |
|||
'item1': 'value1', |
|||
'item2': 'value2', |
|||
}; |
|||
|
|||
beforeEach(function () { |
|||
obj = new LocalStorage(); |
|||
}); |
|||
|
|||
afterEach(function () { |
|||
delete obj; |
|||
}); |
|||
|
|||
it('Store and load items', function() { |
|||
obj.store(data); |
|||
var result = obj.load(['item1', 'item2']); |
|||
result.should.deep.equal(data); |
|||
}); |
|||
|
|||
it('Store, update and load items', function() { |
|||
obj.store(data); |
|||
obj.store({item3: 'value3'}); |
|||
obj.store({item2: 'value22'}); |
|||
var result = obj.load(['item1', 'item2', 'item3']); |
|||
result.should.deep.equal({ |
|||
'item1': 'value1', |
|||
'item2': 'value22', |
|||
'item3': 'value3', |
|||
}); |
|||
}); |
|||
|
|||
it('Remove items', function() { |
|||
var items = ['item1', 'item2', 'item3']; |
|||
obj.store(data); |
|||
obj.remove(items); |
|||
obj.load(items).should.be.empty; |
|||
}); |
|||
|
|||
const LocalStorage = require('storage_manager/model/LocalStorage'); |
|||
const RemoteStorage = require('storage_manager/model/RemoteStorage'); |
|||
|
|||
module.exports = { |
|||
run() { |
|||
|
|||
describe('LocalStorage', () => { |
|||
|
|||
var obj; |
|||
var itemName = 'testItem'; |
|||
var data = { |
|||
'item1': 'value1', |
|||
'item2': 'value2', |
|||
}; |
|||
|
|||
beforeEach(() => { |
|||
obj = new LocalStorage(); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
obj = null; |
|||
}); |
|||
|
|||
it('Store and load items', () => { |
|||
obj.store(data); |
|||
var result = obj.load(['item1', 'item2']); |
|||
expect(result).toEqual(data); |
|||
}); |
|||
|
|||
it('Store, update and load items', () => { |
|||
obj.store(data); |
|||
obj.store({item3: 'value3'}); |
|||
obj.store({item2: 'value22'}); |
|||
var result = obj.load(['item1', 'item2', 'item3']); |
|||
expect(result).toEqual({ |
|||
'item1': 'value1', |
|||
'item2': 'value22', |
|||
'item3': 'value3', |
|||
}); |
|||
}); |
|||
|
|||
it('Remove items', () => { |
|||
var items = ['item1', 'item2', 'item3']; |
|||
obj.store(data); |
|||
obj.remove(items); |
|||
expect(obj.load(items)).toEqual({}); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('RemoteStorage', () => { |
|||
|
|||
var obj; |
|||
var itemName = 'testItem'; |
|||
var endpointStore = 'testStoreEndpoint'; |
|||
var endpointLoad = 'testLoadEndpoint'; |
|||
var params = { test: 'testValue' }; |
|||
var storageOptions; |
|||
var data; |
|||
|
|||
beforeEach(() => { |
|||
data = { |
|||
'item1': 'value1', |
|||
'item2': 'value2', |
|||
}; |
|||
storageOptions = { |
|||
urlStore: endpointStore, |
|||
urlLoad: endpointLoad, |
|||
params, |
|||
}; |
|||
obj = new RemoteStorage(storageOptions); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
$.ajax.restore(); |
|||
obj = null; |
|||
}); |
|||
|
|||
// Stubbing will not return the original object so
|
|||
// .always will not work
|
|||
it.skip('Store data', () => { |
|||
sinon.stub($, "ajax"); |
|||
|
|||
for(var k in params) |
|||
data[k] = params[k]; |
|||
|
|||
obj.store(data); |
|||
$.ajax.calledWithMatch({ |
|||
url: endpointStore, |
|||
data, |
|||
}).should.equal(true); |
|||
}); |
|||
|
|||
it('Load data', () => { |
|||
sinon.stub($, "ajax").returns({ |
|||
done() {} |
|||
}); |
|||
var dt = {}; |
|||
var keys = ['item1', 'item2']; |
|||
obj.load(keys); |
|||
dt.keys = keys; |
|||
|
|||
describe('RemoteStorage', function() { |
|||
|
|||
var obj; |
|||
var itemName = 'testItem'; |
|||
var endpointStore = 'testStoreEndpoint'; |
|||
var endpointLoad = 'testLoadEndpoint'; |
|||
var params = { test: 'testValue' }; |
|||
var storageOptions; |
|||
var data; |
|||
|
|||
beforeEach(function () { |
|||
data = { |
|||
'item1': 'value1', |
|||
'item2': 'value2', |
|||
}; |
|||
storageOptions = { |
|||
urlStore: endpointStore, |
|||
urlLoad: endpointLoad, |
|||
params: params, |
|||
}; |
|||
obj = new RemoteStorage(storageOptions); |
|||
}); |
|||
|
|||
afterEach(function () { |
|||
$.ajax.restore(); |
|||
delete obj; |
|||
}); |
|||
|
|||
// Stubbing will not return the original object so
|
|||
// .always will not work
|
|||
it.skip('Store data', function() { |
|||
sinon.stub($, "ajax"); |
|||
|
|||
for(var k in params) |
|||
data[k] = params[k]; |
|||
|
|||
obj.store(data); |
|||
$.ajax.calledWithMatch({ |
|||
url: endpointStore, |
|||
data: data, |
|||
}).should.equal(true); |
|||
}); |
|||
|
|||
it('Load data', function() { |
|||
sinon.stub($, "ajax").returns({ |
|||
done: function(){} |
|||
}); |
|||
var dt = {}; |
|||
var keys = ['item1', 'item2']; |
|||
obj.load(keys); |
|||
dt.keys = keys; |
|||
|
|||
for(var k in params) |
|||
dt[k] = params[k]; |
|||
|
|||
$.ajax.calledWithMatch({ |
|||
url: endpointLoad, |
|||
data: dt |
|||
}).should.equal(true); |
|||
}); |
|||
for(var k in params) |
|||
dt[k] = params[k]; |
|||
|
|||
}); |
|||
expect($.ajax.calledWithMatch({ |
|||
url: endpointLoad, |
|||
data: dt |
|||
})).toEqual(true); |
|||
}); |
|||
|
|||
} |
|||
}); |
|||
|
|||
}; |
|||
} |
|||
|
|||
}); |
|||
}; |
|||
|
|||
Loading…
Reference in new issue