diff --git a/src/demo.js b/src/demo.js index d2d9e66bb..e62a79bc1 100644 --- a/src/demo.js +++ b/src/demo.js @@ -9,14 +9,19 @@ require(['config/require-config'], function() { noticeOnUnload: 0, container : '#gjs', height: '100%', - fromElement: true, - /* + //fromElement: true, components: [{ style:{ width:'100px', height:'100px' }, traits: ['title'] + },{ + style:{ + width:'150px', + height:'100px' + }, + traits: [{name:'title', value: "myTitleTest"}] },{ type: 'image' },{ @@ -24,7 +29,7 @@ require(['config/require-config'], function() { type: 'link', content: 'mylink', }], - */ + storageManager:{ autoload: 0}, commands: { defaults : [{ diff --git a/src/trait_manager/view/TraitView.js b/src/trait_manager/view/TraitView.js index 35329178b..69fc4c34e 100644 --- a/src/trait_manager/view/TraitView.js +++ b/src/trait_manager/view/TraitView.js @@ -34,11 +34,9 @@ define(['backbone'], function (Backbone) { onValueChange: function() { var m = this.model; var trg = this.target; - var attrs = trg.get('attributes'); + var attrs = _.clone(trg.get('attributes')); attrs[m.get('name')] = m.get('value'); - trg.set('attributes', {}); trg.set('attributes', attrs); - console.log(trg); }, /** diff --git a/test/runner/main.js b/test/runner/main.js index 1a3bb0750..13fba83b6 100644 --- a/test/runner/main.js +++ b/test/runner/main.js @@ -16,6 +16,7 @@ require(['../src/config/require-config.js', 'config/config.js'], function() { 'specs/storage_manager/main.js', 'specs/plugin_manager/main.js', 'specs/block_manager/main.js', + 'specs/trait_manager/main.js', 'specs/parser/main.js', 'specs/grapesjs/main.js', 'specs/utils/main.js' @@ -26,4 +27,4 @@ require(['../src/config/require-config.js', 'config/config.js'], function() { mocha.run(); }); -}); \ No newline at end of file +}); diff --git a/test/specs/trait_manager/main.js b/test/specs/trait_manager/main.js new file mode 100644 index 000000000..ec6b1742b --- /dev/null +++ b/test/specs/trait_manager/main.js @@ -0,0 +1,28 @@ +var modulePath = './../../../test/specs/trait_manager'; + +define([ 'TraitManager', + modulePath + '/model/TraitsModel', + modulePath + '/view/TraitsView', + ], + function(TraitManager, TraitsModel, TraitsView) { + + describe('TraitManager', function() { + + describe('Main', function() { + + var obj; + + beforeEach(function () { + obj = new TraitManager().init(); + }); + + afterEach(function () { + delete obj; + }); + + }); + + TraitsModel.run(); + TraitsView.run(); + }); +}); diff --git a/test/specs/trait_manager/model/TraitsModel.js b/test/specs/trait_manager/model/TraitsModel.js new file mode 100644 index 000000000..3614336a6 --- /dev/null +++ b/test/specs/trait_manager/model/TraitsModel.js @@ -0,0 +1,32 @@ +define(['TraitManager/model/Trait', 'DomComponents/model/Component'], + function(Trait, Component) { + + return { + run: function(){ + + describe('TraitModels', function() { + + var obj; + var target; + var modelName = 'title'; + + beforeEach(function () { + target = new Component(); + obj = new Trait({ + name: modelName, + target: target, + }); + }); + + afterEach(function () { + delete obj; + }); + + it('Object exists', function() { + Trait.should.be.exist; + }); + + }); + } + } +}); diff --git a/test/specs/trait_manager/view/TraitsView.js b/test/specs/trait_manager/view/TraitsView.js new file mode 100644 index 000000000..d06facf3a --- /dev/null +++ b/test/specs/trait_manager/view/TraitsView.js @@ -0,0 +1,76 @@ +var modulePath = 'TraitManager'; +define([modulePath+'/view/TraitView', +modulePath+'/model/Trait', +'DomComponents/model/Component'], + function(TraitView, Trait, Component) { + + return { + run: function(){ + + describe('TraitView', function() { + + var obj; + var model; + var modelName = 'title'; + var target; + + beforeEach(function () { + target = new Component(); + model = new Trait({ + name: modelName, + target: target, + }); + obj = new TraitView({ + model: model, + }); + }); + + afterEach(function () { + delete obj; + delete model; + delete target; + }); + + it('Object exists', function() { + Trait.should.be.exist; + }); + + it('Target has no attributes on init', function() { + target.get('attributes').should.deep.equal({}); + }); + + it('On update of the value updates the target attributes', function() { + model.set('value', 'test'); + var eq = {}; + eq[modelName] = 'test'; + target.get('attributes').should.deep.equal(eq); + }); + + it('Updates on different models do not alter other targets', function() { + var target1 = new Component(); + var target2 = new Component(); + var model1 = new Trait({ + name: modelName, + target: target1, + }); + var model2 = new Trait({ + name: modelName, + target: target2, + }); + var obj1 = new TraitView({model: model1}); + var obj2 = new TraitView({model: model2}); + + model1.set('value', 'test1'); + model2.set('value', 'test2'); + var eq1 = {}; + eq1[modelName] = 'test1'; + var eq2 = {}; + eq2[modelName] = 'test2'; + target1.get('attributes').should.deep.equal(eq1); + target2.get('attributes').should.deep.equal(eq2); + }); + + }); + } + } +});