diff --git a/web-ui/app/js/page/shortcuts.js b/web-ui/app/js/page/shortcuts.js index 8435cc111..b3b9c408e 100644 --- a/web-ui/app/js/page/shortcuts.js +++ b/web-ui/app/js/page/shortcuts.js @@ -67,10 +67,10 @@ define([ var globalKeyEvents = {}; globalKeyEvents[keyCodes.ESC] = events.dispatchers.rightPane.openNoMessageSelected; - if (globalKeyEvents.hasOwnProperty(event.which)) { - triggerFunc(globalKeyEvents[event.which]); - event.preventDefault(); - } + if (!globalKeyEvents.hasOwnProperty(event.which)) return; + + triggerFunc(globalKeyEvents[event.which]); + event.preventDefault(); } function tryMailCompositionKeyEvents(event, triggerFunc) { @@ -78,10 +78,10 @@ define([ mailCompositionKeyEvents[modifierKeys.CTRL + keyCodes.ENTER] = events.ui.mail.send; mailCompositionKeyEvents[modifierKeys.META + keyCodes.ENTER] = events.ui.mail.send; - if (mailCompositionKeyEvents.hasOwnProperty(modifierKey(event) + event.which)) { - triggerFunc(mailCompositionKeyEvents[modifierKey(event) + event.which]); - event.preventDefault(); - } + if (!mailCompositionKeyEvents.hasOwnProperty(modifierKey(event) + event.which)) return; + + event.preventDefault(); + return triggerFunc(mailCompositionKeyEvents[modifierKey(event) + event.which]); } function tryMailHandlingKeyEvents(event, triggerFunc) { @@ -92,10 +92,10 @@ define([ mailHandlingKeyEvents[keyCodes.FORWARD_SLASH] = events.search.focus; mailHandlingKeyEvents[keyCodes.C] = events.dispatchers.rightPane.openComposeBox; - if (mailHandlingKeyEvents.hasOwnProperty(event.which)) { - triggerFunc(mailHandlingKeyEvents[event.which]); - event.preventDefault(); - } + if (!mailHandlingKeyEvents.hasOwnProperty(event.which)) return; + + event.preventDefault(); + return triggerFunc(mailHandlingKeyEvents[event.which]); } function modifierKey(event) { diff --git a/web-ui/test/spec/page/shortcuts.spec.js b/web-ui/test/spec/page/shortcuts.spec.js index 9066f0f08..d621f122a 100644 --- a/web-ui/test/spec/page/shortcuts.spec.js +++ b/web-ui/test/spec/page/shortcuts.spec.js @@ -5,43 +5,84 @@ describeComponent('page/shortcuts', function () { this.setupComponent(); }); - describe('shortcuts', function () { - it('triggers openComposeBox when is pressed and no input is focused', function () { - var eventSpy = openComposeBoxEventSpy(); + describe('global shortcuts', function () { + it('triggers openNoMessageSelected when [Esc] is pressed', function () { + var eventSpy = spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openNoMessageSelected); - $(document).trigger(keydownEvent(this.component.keyCodes.C)); + $(document).trigger(keydownEvent(this.component.keyCodes.ESC)); expect(eventSpy).toHaveBeenTriggeredOn(document) }); + }); - it('does not trigger openComposeBox when is pressed in an input field', function () { - this.$node.append(''); - var eventSpy = openComposeBoxEventSpy(); + describe('mail list shortcuts', function () { + function shortcutEventAndTriggeredEventSpy() { + return [ + { + eventSpy: openComposeBoxEventSpy(), + shortcutEvent: keydownEvent(this.component.keyCodes.C) + }, + { + eventSpy: spyOnEvent(document, Pixelated.events.search.focus), + shortcutEvent: keydownEvent(this.component.keyCodes.FORWARD_SLASH) + }, + { + eventSpy: spyOnEvent(document, Pixelated.events.search.focus), + shortcutEvent: keydownEvent(this.component.keyCodes.S) + } + ] + } - this.$node.find('input').trigger(keydownEvent(this.component.keyCodes.C)); + it('are triggered when no input or textarea is focused', function () { + shortcutEventAndTriggeredEventSpy.call(this).forEach(function (args) { + var eventSpy = args.eventSpy; - expect(eventSpy).not.toHaveBeenTriggeredOn(document) + $(document).trigger(args.shortcutEvent); + + expect(eventSpy).toHaveBeenTriggeredOn(document); + }); }); - it('does not trigger openComposeBox when is pressed in a textarea', function () { - this.$node.append(''); - var eventSpy = openComposeBoxEventSpy(); + it('are not triggered when an input is focused', function () { + var self = this; + shortcutEventAndTriggeredEventSpy.call(this).forEach(function (args) { + self.$node.append(''); + var eventSpy = args.eventSpy; - this.$node.find('textarea').trigger(keydownEvent(this.component.keyCodes.C)); + self.$node.find('input').trigger(args.shortcutEvent); - expect(eventSpy).not.toHaveBeenTriggeredOn(document) + expect(eventSpy).not.toHaveBeenTriggeredOn(document); + }); }); - it('triggers openNoMessageSelected when is pressed', function () { - var eventSpy = spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openNoMessageSelected); + it('are not triggered when a textarea is focused', function () { + var self = this; + shortcutEventAndTriggeredEventSpy.call(this).forEach(function (args) { + self.$node.append(''); + var eventSpy = args.eventSpy; - $(document).trigger(keydownEvent(this.component.keyCodes.ESC)); + self.$node.find('textarea').trigger(args.shortcutEvent); - expect(eventSpy).toHaveBeenTriggeredOn(document) + expect(eventSpy).not.toHaveBeenTriggeredOn(document); + }); }); - it('triggers ui.mail.send when + is pressed and compose box is open', function () { - this.$node.append($('
', {id: this.component.composeBoxId})); + it('are not triggered when the composeBox is opened', function () { + var self = this; + shortcutEventAndTriggeredEventSpy.call(this).forEach(function (args) { + addComposeBox.call(self); + var eventSpy = args.eventSpy; + + $(document).trigger(args.shortcutEvent); + + expect(eventSpy).not.toHaveBeenTriggeredOn(document); + }); + }); + }); + + describe('mail composition shortcuts', function () { + it('triggers ui.mail.send when [Ctrl] + [Enter] is pressed and compose box is open', function () { + addComposeBox.call(this); var eventSpy = spyOnEvent(document, Pixelated.events.ui.mail.send); $(document).trigger(jQuery.Event('keydown', {ctrlKey: true, which: this.component.keyCodes.ENTER})); @@ -49,8 +90,8 @@ describeComponent('page/shortcuts', function () { expect(eventSpy).toHaveBeenTriggeredOn(document) }); - it('triggers ui.mail.send when / + is pressed and compose box is open', function () { - this.$node.append($('
', {id: this.component.composeBoxId})); + it('triggers ui.mail.send when [Meta] + [Enter] is pressed and compose box is open', function () { + addComposeBox.call(this); var eventSpy = spyOnEvent(document, Pixelated.events.ui.mail.send); $(document).trigger(jQuery.Event('keydown', {metaKey: true, which: this.component.keyCodes.ENTER})); @@ -58,7 +99,7 @@ describeComponent('page/shortcuts', function () { expect(eventSpy).toHaveBeenTriggeredOn(document) }); - it('does not trigger ui.mail.send when + is pressed and compose box is closed', function () { + it('does not trigger ui.mail.send when [Ctrl] + [Enter] is pressed and compose box is closed', function () { var eventSpy = spyOnEvent(document, Pixelated.events.ui.mail.send); $(document).trigger(jQuery.Event('keydown', {ctrlKey: true, which: this.component.keyCodes.ENTER})); @@ -66,28 +107,24 @@ describeComponent('page/shortcuts', function () { expect(eventSpy).not.toHaveBeenTriggeredOn(document) }); - it('triggers search.focus when is pressed', function () { - var eventSpy = spyOnEvent(document, Pixelated.events.search.focus); + it('does not trigger ui.mail.send when [Meta] + [Enter] is pressed and compose box is closed', function () { + var eventSpy = spyOnEvent(document, Pixelated.events.ui.mail.send); - $(document).trigger(keydownEvent(this.component.keyCodes.FORWARD_SLASH)); + $(document).trigger(jQuery.Event('keydown', {metaKey: true, which: this.component.keyCodes.ENTER})); - expect(eventSpy).toHaveBeenTriggeredOn(document) + expect(eventSpy).not.toHaveBeenTriggeredOn(document) }); + }); - it('triggers search.focus when is pressed', function () { - var eventSpy = spyOnEvent(document, Pixelated.events.search.focus); - - $(document).trigger(keydownEvent(this.component.keyCodes.S)); - - expect(eventSpy).toHaveBeenTriggeredOn(document) - }); + function openComposeBoxEventSpy() { + return spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openComposeBox); + } - function openComposeBoxEventSpy() { - return spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openComposeBox); - } + function keydownEvent(code) { + return jQuery.Event('keydown', {which: code}); + } - function keydownEvent(code) { - return jQuery.Event('keydown', {which: code}); - } - }); + function addComposeBox() { + this.$node.append($('
', {id: this.component.composeBoxId})); + } });