Repository: ambari Updated Branches: refs/heads/trunk 598835003 -> c31a07770
AMBARI-15728. Modal dialogs no longer handle esc (alexantonenko) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c31a0777 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c31a0777 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c31a0777 Branch: refs/heads/trunk Commit: c31a077708213c8288f92f1ab7aa1019bf4be7c1 Parents: 5988350 Author: Alex Antonenko <[email protected]> Authored: Wed Apr 6 12:37:14 2016 +0300 Committer: Alex Antonenko <[email protected]> Committed: Wed Apr 6 13:01:30 2016 +0300 ---------------------------------------------------------------------- ambari-web/app/views/common/modal_popup.js | 4 +- .../test/views/common/modal_popup_test.js | 203 +++++++++++++++++++ 2 files changed, 205 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/c31a0777/ambari-web/app/views/common/modal_popup.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/views/common/modal_popup.js b/ambari-web/app/views/common/modal_popup.js index f016fcf..571bf82 100644 --- a/ambari-web/app/views/common/modal_popup.js +++ b/ambari-web/app/views/common/modal_popup.js @@ -83,7 +83,7 @@ App.ModalPopup = Ember.View.extend({ this.$().find('#modal').off('enter-key-pressed').off('escape-key-pressed'); }, - escapeKeyPressed: function() { + escapeKeyPressed: function (event) { var closeButton = this.$().find('.modal-header > .close').last(); if (closeButton.length > 0) { event.preventDefault(); @@ -93,7 +93,7 @@ App.ModalPopup = Ember.View.extend({ } }, - enterKeyPressed: function() { + enterKeyPressed: function (event) { var primaryButton = this.$().find('.modal-footer > .btn-success').last(); if ((!$("*:focus").is("textarea")) && primaryButton.length > 0 && primaryButton.attr('disabled') !== 'disabled') { event.preventDefault(); http://git-wip-us.apache.org/repos/asf/ambari/blob/c31a0777/ambari-web/test/views/common/modal_popup_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/views/common/modal_popup_test.js b/ambari-web/test/views/common/modal_popup_test.js index ed58834..c4b0104 100644 --- a/ambari-web/test/views/common/modal_popup_test.js +++ b/ambari-web/test/views/common/modal_popup_test.js @@ -53,4 +53,207 @@ describe('App.ModalPopup', function() { }); }); + describe('#escapeKeyPressed', function () { + + var returnedValue, + event = { + preventDefault: Em.K, + stopPropagation: Em.K + }, + cases = [ + { + buttons: [], + preventDefaultCallCount: 0, + stopPropagationCallCount: 0, + clickCallCount: 0, + returnedValue: undefined, + title: 'no close button' + }, + { + buttons: [{}], + preventDefaultCallCount: 1, + stopPropagationCallCount: 1, + clickCallCount: 1, + returnedValue: false, + title: 'close button available' + } + ]; + + cases.forEach(function (item) { + + describe(item.title, function () { + + beforeEach(function () { + item.buttons.click = Em.K; + sinon.stub(popup, '$').returns({ + find: function () { + return { + last: function () { + return item.buttons; + } + } + } + }); + sinon.spy(item.buttons, 'click'); + sinon.spy(event, 'preventDefault'); + sinon.spy(event, 'stopPropagation'); + returnedValue = popup.escapeKeyPressed(event); + }); + + afterEach(function () { + popup.$.restore(); + item.buttons.click.restore(); + event.preventDefault.restore(); + event.stopPropagation.restore(); + }); + + it('prevent default behaviour', function () { + expect(event.preventDefault.callCount).to.equal(item.preventDefaultCallCount); + }); + + it('stop event propagation', function () { + expect(event.stopPropagation.callCount).to.equal(item.stopPropagationCallCount); + }); + + it('close button click', function () { + expect(item.buttons.click.callCount).to.equal(item.clickCallCount); + }); + + it('returned value', function () { + expect(returnedValue).to.equal(item.returnedValue); + }); + + }); + + }); + + }); + + describe('#enterKeyPressed', function () { + + var returnedValue, + event = { + preventDefault: Em.K, + stopPropagation: Em.K + }, + cases = [ + { + buttons: [], + isTextArea: true, + preventDefaultCallCount: 0, + stopPropagationCallCount: 0, + clickCallCount: 0, + returnedValue: undefined, + title: 'focus on textarea, no primary button' + }, + { + buttons: [], + isTextArea: false, + preventDefaultCallCount: 0, + stopPropagationCallCount: 0, + clickCallCount: 0, + returnedValue: undefined, + title: 'no focus on textarea, no primary button' + }, + { + buttons: [{}], + isTextArea: true, + disabled: 'disabled', + preventDefaultCallCount: 0, + stopPropagationCallCount: 0, + clickCallCount: 0, + returnedValue: undefined, + title: 'focus on textarea, primary button disabled' + }, + { + buttons: [{}], + isTextArea: false, + disabled: 'disabled', + preventDefaultCallCount: 0, + stopPropagationCallCount: 0, + clickCallCount: 0, + returnedValue: undefined, + title: 'no focus on textarea, primary button disabled' + }, + { + buttons: [{}], + isTextArea: true, + disabled: '', + preventDefaultCallCount: 0, + stopPropagationCallCount: 0, + clickCallCount: 0, + returnedValue: undefined, + title: 'focus on textarea, primary button enabled' + }, + { + buttons: [{}], + isTextArea: false, + disabled: '', + preventDefaultCallCount: 1, + stopPropagationCallCount: 1, + clickCallCount: 1, + returnedValue: false, + title: 'no focus on textarea, primary button enabled' + } + ]; + + cases.forEach(function (item) { + + describe(item.title, function () { + + beforeEach(function () { + item.buttons.click = Em.K; + item.buttons.attr = function () { + return item.disabled; + }; + sinon.stub(popup, '$').returns({ + find: function () { + return { + last: function () { + return item.buttons; + } + } + } + }); + sinon.stub(window, '$').withArgs('*:focus').returns({ + is: function () { + return item.isTextArea + } + }); + sinon.spy(item.buttons, 'click'); + sinon.spy(event, 'preventDefault'); + sinon.spy(event, 'stopPropagation'); + returnedValue = popup.enterKeyPressed(event); + }); + + afterEach(function () { + popup.$.restore(); + window.$.restore(); + item.buttons.click.restore(); + event.preventDefault.restore(); + event.stopPropagation.restore(); + }); + + it('prevent default behaviour', function () { + expect(event.preventDefault.callCount).to.equal(item.preventDefaultCallCount); + }); + + it('stop event propagation', function () { + expect(event.stopPropagation.callCount).to.equal(item.stopPropagationCallCount); + }); + + it('primary button click', function () { + expect(item.buttons.click.callCount).to.equal(item.clickCallCount); + }); + + it('returned value', function () { + expect(returnedValue).to.equal(item.returnedValue); + }); + + }); + + }); + + }); + }); \ No newline at end of file
