2017-08-17 22:00:37 +05:30
|
|
|
/* eslint-disable space-before-function-paren, no-var, no-return-assign, comma-dangle, jasmine/no-spec-dupes, new-cap, max-len */
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
import '~/behaviors/quick_submit';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
(function() {
|
|
|
|
describe('Quick Submit behavior', function() {
|
|
|
|
var keydownEvent;
|
2017-08-17 22:00:37 +05:30
|
|
|
preloadFixtures('issues/open-issue.html.raw');
|
2016-09-13 17:45:13 +05:30
|
|
|
beforeEach(function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
loadFixtures('issues/open-issue.html.raw');
|
2016-09-13 17:45:13 +05:30
|
|
|
$('form').submit(function(e) {
|
2016-09-29 09:46:39 +05:30
|
|
|
// Prevent a form submit from moving us off the testing page
|
2016-09-13 17:45:13 +05:30
|
|
|
return e.preventDefault();
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
this.spies = {
|
2016-09-13 17:45:13 +05:30
|
|
|
submit: spyOnEvent('form', 'submit')
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
this.textarea = $('.js-quick-submit textarea').first();
|
2016-09-13 17:45:13 +05:30
|
|
|
});
|
|
|
|
it('does not respond to other keyCodes', function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
keyCode: 32
|
|
|
|
}));
|
|
|
|
return expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
it('does not respond to Enter alone', function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
ctrlKey: false,
|
|
|
|
metaKey: false
|
|
|
|
}));
|
|
|
|
return expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
it('does not respond to repeated events', function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
repeat: true
|
|
|
|
}));
|
|
|
|
return expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
it('disables input of type submit', function() {
|
|
|
|
const submitButton = $('.js-quick-submit input[type=submit]');
|
|
|
|
this.textarea.trigger(keydownEvent());
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(submitButton).toBeDisabled();
|
|
|
|
});
|
|
|
|
it('disables button of type submit', function() {
|
2017-09-10 17:25:29 +05:30
|
|
|
const submitButton = $('.js-quick-submit input[type=submit]');
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent());
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(submitButton).toBeDisabled();
|
2016-09-13 17:45:13 +05:30
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
it('only clicks one submit', function() {
|
|
|
|
const existingSubmit = $('.js-quick-submit input[type=submit]');
|
|
|
|
// Add an extra submit button
|
|
|
|
const newSubmit = $('<button type="submit">Submit it</button>');
|
|
|
|
newSubmit.insertAfter(this.textarea);
|
|
|
|
|
|
|
|
const oldClick = spyOnEvent(existingSubmit, 'click');
|
|
|
|
const newClick = spyOnEvent(newSubmit, 'click');
|
|
|
|
|
|
|
|
this.textarea.trigger(keydownEvent());
|
|
|
|
|
|
|
|
expect(oldClick).not.toHaveBeenTriggered();
|
|
|
|
expect(newClick).toHaveBeenTriggered();
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
// We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll
|
2016-09-29 09:46:39 +05:30
|
|
|
// only run the tests that apply to the current platform
|
2016-09-13 17:45:13 +05:30
|
|
|
if (navigator.userAgent.match(/Macintosh/)) {
|
|
|
|
it('responds to Meta+Enter', function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent());
|
2016-09-13 17:45:13 +05:30
|
|
|
return expect(this.spies.submit).toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
it('excludes other modifier keys', function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
altKey: true
|
|
|
|
}));
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
ctrlKey: true
|
|
|
|
}));
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
shiftKey: true
|
|
|
|
}));
|
|
|
|
return expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
it('responds to Ctrl+Enter', function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent());
|
2016-09-13 17:45:13 +05:30
|
|
|
return expect(this.spies.submit).toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
it('excludes other modifier keys', function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
altKey: true
|
|
|
|
}));
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
metaKey: true
|
|
|
|
}));
|
2017-08-17 22:00:37 +05:30
|
|
|
this.textarea.trigger(keydownEvent({
|
2016-09-13 17:45:13 +05:30
|
|
|
shiftKey: true
|
|
|
|
}));
|
|
|
|
return expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return keydownEvent = function(options) {
|
|
|
|
var defaults;
|
|
|
|
if (navigator.userAgent.match(/Macintosh/)) {
|
|
|
|
defaults = {
|
|
|
|
keyCode: 13,
|
|
|
|
metaKey: true
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
defaults = {
|
|
|
|
keyCode: 13,
|
|
|
|
ctrlKey: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return $.Event('keydown', $.extend({}, defaults, options));
|
|
|
|
};
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
}).call(window);
|