diff --git a/Gruntfile.js b/Gruntfile.js index 3bf1c2b5b..1ab7592a3 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -167,7 +167,7 @@ module.exports = function(grunt) { test: { files: ['test/specs/**/*.js'], tasks: ['mocha'], - options: { livereload: true }, //default port 35729 + //options: { livereload: true }, //default port 35729 } }, diff --git a/src/asset_manager/view/AssetView.js b/src/asset_manager/view/AssetView.js index 1658a0d31..8bb17c629 100644 --- a/src/asset_manager/view/AssetView.js +++ b/src/asset_manager/view/AssetView.js @@ -1,17 +1,17 @@ -define(['backbone'], +define(['backbone'], function (Backbone) { - /** + /** * @class AssetView * */ return Backbone.View.extend({ - + initialize: function(o) { - this.options = o; - this.config = o.config || {}; - this.pfx = this.config.stylePrefix; + this.options = o; + this.config = o.config || {}; + this.pfx = this.config.stylePrefix || ''; this.className = this.pfx + 'asset'; this.listenTo( this.model, 'destroy remove', this.remove ); }, - + }); }); diff --git a/src/asset_manager/view/FileUploader.js b/src/asset_manager/view/FileUploader.js index d0d3a1c31..8d7821e4e 100644 --- a/src/asset_manager/view/FileUploader.js +++ b/src/asset_manager/view/FileUploader.js @@ -13,7 +13,7 @@ define(['backbone', 'text!./../template/fileUploader.html'], initialize: function(o) { this.options = o || {}; this.config = o.config || {}; - this.pfx = this.config.stylePrefix; + this.pfx = this.config.stylePrefix || ''; this.target = this.collection || {}; this.uploadId = this.pfx + 'uploadFile'; this.disabled = this.config.disableUpload; diff --git a/src/dom_components/main.js b/src/dom_components/main.js index bc1ff8c6d..044f38196 100644 --- a/src/dom_components/main.js +++ b/src/dom_components/main.js @@ -16,7 +16,7 @@ define(function(require) { ComponentImageView = require('./view/ComponentImageView'), ComponentTextView = require('./view/ComponentTextView'); - // Set default options + // Set default options for (var name in defaults) { if (!(name in c)) c[name] = defaults[name]; @@ -34,26 +34,56 @@ define(function(require) { if(!c.wrapper.style) c.wrapper.style = {}; + c.wrapper.style.position = 'relative'; this.component = new Component(c.wrapper); var obj = { - model : this.component, - config : c, + model: this.component, + config: c, }; - this.ComponentView = new ComponentView(obj); + this.c = c; + this.ComponentView = new ComponentView(obj); } Components.prototype = { - render : function(){ - return this.ComponentView.render().$el; - }, - + /** + * Returns main wrapper which will contain all new components + * + * @return {Object} + */ getComponent : function(){ return this.component; }, + + /** + * Returns main wrapper which will contain all new components + * + * @return {Object} + */ + getWrapper: function(){ + return this.getComponent(); + }, + + /** + * Returns children from the wrapper + * + * @return {Object} + */ + getComponents: function(){ + return this.getWrapper().get('components'); + }, + + /** + * Render and returns wrapper + * + * @return {Object} + */ + render : function(){ + return this.ComponentView.render().el; + }, }; return Components; diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index 9d1c0a249..c77160541 100644 --- a/src/dom_components/model/Component.js +++ b/src/dom_components/model/Component.js @@ -7,7 +7,7 @@ define(['backbone','./Components'], defaults: { tagName : 'div', - type : '', + type : '', editable : false, removable : true, movable : true, @@ -18,12 +18,13 @@ define(['backbone','./Components'], status : '', previousModel : '', content : '', - style : {}, - attributes : {}, + style : {}, + attributes : {}, }, - initialize: function(options) { - this.defaultC = options.components || []; + initialize: function(o) { + this.config = o || {}; + this.defaultC = this.config.components || []; this.components = new Components(this.defaultC); this.set('components', this.components); }, @@ -47,7 +48,7 @@ define(['backbone','./Components'], /** * Get name of the component * - * @return string + * @return {String} * */ getName: function(){ if(!this.name){ diff --git a/test/runner/main.js b/test/runner/main.js index 808fe74f7..7efbeb7f8 100644 --- a/test/runner/main.js +++ b/test/runner/main.js @@ -8,6 +8,10 @@ require(['../src/config/require-config.js', 'config/config.js'], function() { 'specs/asset_manager/model/AssetImage.js', 'specs/asset_manager/model/Assets.js', 'specs/asset_manager/view/AssetsView.js', + 'specs/asset_manager/view/AssetView.js', + 'specs/asset_manager/view/AssetImageView.js', + 'specs/asset_manager/view/FileUploader.js', + 'specs/dom_components/main.js', ], function(chai) { var should = chai.should(), diff --git a/test/specs/asset_manager/view/AssetImageView.js b/test/specs/asset_manager/view/AssetImageView.js new file mode 100644 index 000000000..dc28fd5b3 --- /dev/null +++ b/test/specs/asset_manager/view/AssetImageView.js @@ -0,0 +1,77 @@ +define(['AssetManager/view/AssetImageView', 'AssetManager/model/AssetImage', 'AssetManager/model/Assets'], + function(AssetImageView, AssetImage, Assets) { + + describe('Asset Manager', function() { + + describe('AssetImageView', function() { + + before(function () { + this.$fixtures = $("#fixtures"); + this.$fixture = $('
'); + }); + + beforeEach(function () { + var coll = new Assets(); + var model = coll.add({ type:'image', src: '/test' }); + this.view = new AssetImageView({ + config : {}, + model: model + }); + this.$fixture.empty().appendTo(this.$fixtures); + this.$fixture.html(this.view.render().el); + }); + + afterEach(function () { + this.view.model.destroy(); + }); + + after(function () { + this.$fixture.remove(); + }); + + it('Object exists', function() { + AssetImageView.should.be.exist; + }); + + describe('Asset should be rendered correctly', function() { + + it('Has preview box', function() { + var $asset = this.view.$el; + $asset.find('#preview').should.have.property(0); + }); + + it('Has meta box', function() { + var $asset = this.view.$el; + $asset.find('#meta').should.have.property(0); + }); + + it('Has close button', function() { + var $asset = this.view.$el; + $asset.find('#close').should.have.property(0); + }); + + }); + + it('Could be selected', function() { + sinon.stub(this.view, 'updateTarget'); + this.view.$el.trigger('click'); + this.view.$el.attr('class').should.contain('highlight'); + this.view.updateTarget.calledOnce.should.equal(true); + }); + + it('Could be chosen', function() { + sinon.stub(this.view, 'updateTarget'); + this.view.$el.trigger('dblclick'); + this.view.updateTarget.calledOnce.should.equal(true); + }); + + it('Could be removed', function() { + var spy = sinon.spy(); + this.view.model.on("remove", spy); + this.view.$el.find('#close').trigger('click'); + spy.called.should.equal(true); + }); + + }); + }); +}); \ No newline at end of file diff --git a/test/specs/asset_manager/view/AssetView.js b/test/specs/asset_manager/view/AssetView.js new file mode 100644 index 000000000..4bf7f4a25 --- /dev/null +++ b/test/specs/asset_manager/view/AssetView.js @@ -0,0 +1,42 @@ +define(['AssetManager/view/AssetView', 'AssetManager/model/Asset', 'AssetManager/model/Assets'], + function(AssetView, Asset, Assets) { + + describe('Asset Manager', function() { + + describe('AssetView', function() { + + before(function () { + this.$fixtures = $("#fixtures"); + this.$fixture = $('
'); + }); + + beforeEach(function () { + var coll = new Assets(); + var model = coll.add({}); + this.view = new AssetView({ + config : {}, + model: model + }); + this.$fixture.empty().appendTo(this.$fixtures); + this.$fixture.html(this.view.render().el); + }); + + afterEach(function () { + this.view.model.destroy(); + }); + + after(function () { + this.$fixture.remove(); + }); + + it('Object exists', function() { + AssetView.should.be.exist; + }); + + it('Has correct prefix', function() { + this.view.pfx.should.equal(''); + }); + + }); + }); +}); \ No newline at end of file diff --git a/test/specs/asset_manager/view/FileUploader.js b/test/specs/asset_manager/view/FileUploader.js new file mode 100644 index 000000000..d9bfec677 --- /dev/null +++ b/test/specs/asset_manager/view/FileUploader.js @@ -0,0 +1,77 @@ +define(['AssetManager/view/FileUploader'], + function(FileUploader) { + + describe('Asset Manager', function() { + + describe('File Uploader', function() { + + before(function () { + this.$fixtures = $("#fixtures"); + this.$fixture = $('
'); + }); + + beforeEach(function () { + this.view = new FileUploader({ config : {} }); + this.$fixture.empty().appendTo(this.$fixtures); + this.$fixture.html(this.view.render().el); + }); + + afterEach(function () { + this.view.remove(); + }); + + after(function () { + this.$fixture.remove(); + }); + + it('Object exists', function() { + FileUploader.should.be.exist; + }); + + it('Has correct prefix', function() { + this.view.pfx.should.equal(''); + }); + + describe('Should be rendered correctly', function() { + + it('Has title', function() { + this.view.$el.find('#title').should.have.property(0); + }); + + it('Title is empty', function() { + this.view.$el.find('#title').html().should.equal(''); + }); + + it('Has file input', function() { + this.view.$el.find('input[type=file]').should.have.property(0); + }); + + it('File input is enabled', function() { + this.view.$el.find('input[type=file]').prop('disabled').should.equal(false); + }); + + }); + + describe('Interprets configurations correctly', function() { + + it('Has correct title', function() { + var view = new FileUploader({ config : { + uploadText : 'Test', + } }); + view.render(); + view.$el.find('#title').html().should.equal('Test'); + }); + + it('Could be disabled', function() { + var view = new FileUploader({ config : { + disableUpload: true, + } }); + view.render(); + view.$el.find('input[type=file]').prop('disabled').should.equal(true); + }); + + }); + + }); + }); +}); \ No newline at end of file diff --git a/test/specs/components/model/componentModel.js b/test/specs/components/model/componentModel.js deleted file mode 100644 index 8b5f285d5..000000000 --- a/test/specs/components/model/componentModel.js +++ /dev/null @@ -1,36 +0,0 @@ -define(['componentModel'], - function(componentModel) { - describe('Component', function() { - - it('Contiene valori di default', function() {//Has default values - var model = new componentModel({}); - var modelComponents = model.components; - model.should.be.ok; - model.get('tagName').should.equal("div"); - model.get('classes').should.be.empty; - model.get('css').should.be.empty; - model.get('attributes').should.be.empty; - modelComponents.models.should.be.empty; - }); - it('Non ci sono altri componenti all\'interno ', function() {//No other components inside - var model = new componentModel({}); - var modelComponents = model.components; - model.should.be.ok; - modelComponents.models.should.be.empty; - }); - it('Imposta valori passati', function() {//Sets passed attributes - var model = new componentModel({ - tagName : 'span', - classes : ['one','two','three'], - css : { 'one':'vone', 'two':'vtwo', }, - attributes : { 'data-one':'vone', 'data-two':'vtwo', }, - }); - model.should.be.ok; - model.get('tagName').should.equal("span"); - model.get('classes').should.have.length(3); - model.get('css').should.have.keys(["one", "two",]); - model.get('attributes').should.have.keys(["data-one", "data-two",]); - }); - it('Possibilità di istanziare componenti annidati'); //Possibility to init nested components - }); -}); \ No newline at end of file diff --git a/test/specs/components/model/components.js b/test/specs/components/model/components.js deleted file mode 100644 index a42d8a85a..000000000 --- a/test/specs/components/model/components.js +++ /dev/null @@ -1,58 +0,0 @@ -define(['Components'], - function(Components) { - describe('Components', function() { - before(function () { - this.collection = new Components(); - this.collection.localStorage._clear(); - }); - after(function () { - this.collection = null; - }); - describe('Creazione', function() { - it('Contiene valori di default', function() {//Has default values - this.collection.should.be.ok; - this.collection.should.have.length(0); - }); - }); - describe('Modifica', function() { - beforeEach(function () { - this.collection.create({ - tagName : 'span', - classes : ['one','two','three'], - css : { 'one':'vone', 'two':'vtwo', }, - attributes : { 'data-one':'vone', 'data-two':'vtwo', }, - }); - }); - afterEach(function () { - this.collection.localStorage._clear(); - this.collection.reset(); - }); - it('Contiene un singolo componente', function(done) { //Has single object - var collection = this.collection, model; - - collection.once("reset", function () { - collection.should.have.length(1); - model = collection.at(0); - model.should.be.ok; - model.get('tagName').should.equal("span"); - model.get('classes').should.have.length(3); - model.get('css').should.have.keys(["one", "two",]); - model.get('attributes').should.have.keys(["data-one", "data-two",]); - done(); - }); - collection.fetch({ reset: true }); - }); - - it("Componenete eliminabile", function (done) { //Can delete a component - var collection = this.collection, model; - collection.should.have.length(1); - collection.once("remove", function () { - collection.should.have.length(0); - done(); - }); - - model = collection.shift(); - }); - }); - }); - }); \ No newline at end of file diff --git a/test/specs/dom_components/main.js b/test/specs/dom_components/main.js new file mode 100644 index 000000000..eab7363ab --- /dev/null +++ b/test/specs/dom_components/main.js @@ -0,0 +1,46 @@ +var modulePath = './../../../test/specs/dom_components'; + +define([ + 'DomComponents', + modulePath + '/model/Component' + ], + function(DomComponents, + ComponentModels + ) { + + describe('DOM Components', function() { + + describe('Main', function() { + + beforeEach(function () { + this.obj = new DomComponents(); + }); + + afterEach(function () { + delete this.obj; + }); + + it('Object exists', function() { + DomComponents.should.be.exist; + }); + + it('Wrapper exists', function() { + this.obj.getWrapper().should.not.be.empty; + }); + + it('No components inside', function() { + this.obj.getComponents().length.should.equal(0); + }); + + it('Render wrapper', function() { + sinon.stub(this.obj.ComponentView, "render").returns({ el: '' }); + this.obj.render(); + this.obj.ComponentView.render.calledOnce.should.equal(true); + }); + + }); + + ComponentModels.run(); + + }); +}); \ No newline at end of file diff --git a/test/specs/dom_components/model/Component.js b/test/specs/dom_components/model/Component.js new file mode 100644 index 000000000..6d83ad92d --- /dev/null +++ b/test/specs/dom_components/model/Component.js @@ -0,0 +1,109 @@ +define(['DomComponents/model/Component', + 'DomComponents/model/ComponentImage', + 'DomComponents/model/ComponentText', + 'DomComponents/model/Components'], + function(Component, ComponentImage, ComponentText, Components) { + + return { + run : function(){ + describe('Component', function() { + + beforeEach(function () { + this.obj = new Component(); + }); + + afterEach(function () { + delete this.obj; + }); + + it('Has no children', function() { + this.obj.get('components').length.should.equal(0); + }); + + it('Clones correctly', function() { + var sAttr = this.obj.attributes; + var cloned = this.obj.clone(); + var eAttr = cloned.attributes; + sAttr.components = {}; + eAttr.components = {}; + sAttr.should.deep.equal(eAttr); + }); + + it('Has expected name', function() { + this.obj.cid = 'c999'; + this.obj.getName().should.equal('Box999'); + }); + + it('Has expected name 2', function() { + this.obj.cid = 'c999'; + this.obj.set('type','testType'); + this.obj.getName().should.equal('TestTypeBox999'); + }); + + }); + + describe('Image Component', function() { + + beforeEach(function () { + this.obj = new ComponentImage(); + }); + + afterEach(function () { + delete this.obj; + }); + + it('Has src property', function() { + this.obj.has('src').should.equal(true); + }); + + it('Not droppable', function() { + this.obj.get('droppable').should.equal(false); + }); + + }); + + describe('Text Component', function() { + + beforeEach(function () { + this.obj = new ComponentText(); + }); + + afterEach(function () { + delete this.obj; + }); + + it('Has content property', function() { + this.obj.has('content').should.equal(true); + }); + + it('Not droppable', function() { + this.obj.get('droppable').should.equal(false); + }); + + }); + + describe('Components', function() { + + it('Creates component correctly', function() { + var c = new Components(); + var m = c.add({}); + m.should.be.an.instanceOf(Component); + }); + + it('Creates image component correctly', function() { + var c = new Components(); + var m = c.add({ type: 'image' }); + m.should.be.an.instanceOf(ComponentImage); + }); + + it('Creates text component correctly', function() { + var c = new Components(); + var m = c.add({ type: 'text' }); + m.should.be.an.instanceOf(ComponentText); + }); + + }); + } + }; + +}); \ No newline at end of file diff --git a/test/specs/components/view/componentView.js b/test/specs/dom_components/view/componentView.js similarity index 100% rename from test/specs/components/view/componentView.js rename to test/specs/dom_components/view/componentView.js