From f5079f5e8e6aac7d31590864bd9fd424eca30dd3 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 21 Apr 2016 22:11:12 +0200 Subject: [PATCH] Add ButtonView tests --- src/panels/view/ButtonView.js | 29 ++++++----- test/specs/panels/main.js | 7 ++- test/specs/panels/view/ButtonView.js | 76 ++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 test/specs/panels/view/ButtonView.js diff --git a/src/panels/view/ButtonView.js b/src/panels/view/ButtonView.js index 3b9f50dde..b536550f7 100644 --- a/src/panels/view/ButtonView.js +++ b/src/panels/view/ButtonView.js @@ -11,26 +11,27 @@ function(Backbone, require) { initialize: function(o){ _.bindAll(this, 'startTimer', 'stopTimer', 'showButtons', 'hideButtons','closeOnKeyPress'); - this.config = o.config; - this.em = this.config.em || {}; - this.pfx = this.config.stylePrefix; - this.id = this.pfx + this.model.get('id'); - this.className = this.pfx + 'btn ' + this.model.get('className'); - this.activeCls = this.pfx + 'active'; - this.btnsVisCls = this.pfx + 'visible'; - this.parentM = o.parentM || null; + var cls = this.model.get('className'); + this.config = o.config || {}; + this.em = this.config.em || {}; + this.pfx = this.config.stylePrefix || ''; + this.id = this.pfx + this.model.get('id'); + this.activeCls = this.pfx + 'active'; + this.btnsVisCls = this.pfx + 'visible'; + this.parentM = o.parentM || null; + this.className = this.pfx + 'btn' + (cls ? ' ' + cls : ''); this.listenTo(this.model, 'change:active updateActive', this.updateActive); this.listenTo(this.model, 'checkActive', this.checkActive); - this.listenTo(this.model, 'change:bntsVis', this.updateBtnsVis); - this.listenTo(this.model, 'change:attributes', this.updateAttributes); - this.listenTo(this.model, 'change:className', this.updateClassName); + this.listenTo(this.model, 'change:bntsVis', this.updateBtnsVis); + this.listenTo(this.model, 'change:attributes', this.updateAttributes); + this.listenTo(this.model, 'change:className', this.updateClassName); if(this.model.get('buttons').length){ this.$el.on('mousedown', this.startTimer); this.$el.append($('
',{class: this.pfx + 'arrow-rd'})); } - if(this.em) + if(this.em && this.em.get) this.commands = this.em.get('Commands'); }, @@ -41,7 +42,8 @@ function(Backbone, require) { * */ updateClassName: function() { - this.$el.attr('class', this.pfx + 'btn ' + this.model.get('className')); + var cls = this.model.get('className'); + this.$el.attr('class', this.pfx + 'btn' + (cls ? ' ' + cls : '')); }, /** @@ -105,6 +107,7 @@ function(Backbone, require) { $(document).on('mousedown', this.hideButtons); $(document).on('keypress', this.closeOnKeyPress); }, + /** * Hide children buttons * diff --git a/test/specs/panels/main.js b/test/specs/panels/main.js index c89c7417f..da4a8a557 100644 --- a/test/specs/panels/main.js +++ b/test/specs/panels/main.js @@ -4,13 +4,15 @@ define([ 'Panels', modulePath + '/model/PanelModels', modulePath + '/view/PanelView', - modulePath + '/view/PanelsView' + modulePath + '/view/PanelsView', + modulePath + '/view/ButtonView' ], function( Panels, Models, PanelView, - PanelsView + PanelsView, + ButtonView ) { describe('Panels', function() { @@ -105,6 +107,7 @@ define([ Models.run(); PanelView.run(); PanelsView.run(); + ButtonView.run(); }); }); \ No newline at end of file diff --git a/test/specs/panels/view/ButtonView.js b/test/specs/panels/view/ButtonView.js new file mode 100644 index 000000000..d26c40ff1 --- /dev/null +++ b/test/specs/panels/view/ButtonView.js @@ -0,0 +1,76 @@ +var path = 'Panels/view/'; +define([path + 'ButtonView', 'Panels/model/Button'], + function(ButtonView, Button) { + + return { + run : function(){ + + describe('ButtonView', function() { + + var $fixtures; + var $fixture; + var model; + var view; + var btnClass = 'btn'; + + before(function () { + $fixtures = $("#fixtures"); + $fixture = $('
'); + }); + + beforeEach(function () { + model = new Button(); + view = new ButtonView({ + model: model + }); + $fixture.empty().appendTo($fixtures); + $fixture.html(view.render().el); + }); + + afterEach(function () { + view.remove(); + }); + + after(function () { + $fixture.remove(); + }); + + it('Button empty', function() { + $fixture.html().should.be.equal(''); + }); + + it('Update class', function() { + model.set('className','test'); + view.el.getAttribute('class').should.be.equal(btnClass + ' test'); + }); + + it('Update attributes', function() { + model.set('attributes',{ + 'data-test': 'test-value' + }); + view.el.getAttribute('data-test').should.be.equal('test-value'); + }); + + it('Check enable active', function() { + model.set('active', true, {silent: true}); + view.checkActive(); + view.el.getAttribute('class').should.be.equal(btnClass + ' active'); + }); + + it('Check disable active', function() { + model.set('active', true, {silent: true}); + view.checkActive(); + model.set('active', false, {silent: true}); + view.checkActive(); + view.el.getAttribute('class').should.be.equal(btnClass); + }); + + it('Renders correctly', function() { + view.render().should.be.ok; + }); + + }); + } + }; + +}); \ No newline at end of file