Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Commit

Permalink
Test all code paths
Browse files Browse the repository at this point in the history
  • Loading branch information
treppo committed Jul 19, 2016
1 parent a886104 commit 0b04cf0
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 53 deletions.
24 changes: 12 additions & 12 deletions web-ui/app/js/page/shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ 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) {
var mailCompositionKeyEvents = {};
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) {
Expand All @@ -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) {
Expand Down
119 changes: 78 additions & 41 deletions web-ui/test/spec/page/shortcuts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,126 @@ describeComponent('page/shortcuts', function () {
this.setupComponent();
});

describe('shortcuts', function () {
it('triggers openComposeBox when <c> 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 <c> is pressed in an input field', function () {
this.$node.append('<input />');
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 <c> is pressed in a textarea', function () {
this.$node.append('<textarea></textarea>');
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('<input />');
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 <Esc> 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('<textarea></textarea>');
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 <Ctrl> + <Enter> is pressed and compose box is open', function () {
this.$node.append($('<div>', {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}));

expect(eventSpy).toHaveBeenTriggeredOn(document)
});

it('triggers ui.mail.send when <Cmd>/<Meta> + <Enter> is pressed and compose box is open', function () {
this.$node.append($('<div>', {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}));

expect(eventSpy).toHaveBeenTriggeredOn(document)
});

it('does not trigger ui.mail.send when <Ctrl> + <Enter> 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}));

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 <s> 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($('<div>', {id: this.component.composeBoxId}));
}
});

0 comments on commit 0b04cf0

Please sign in to comment.