Commit 3008e995 authored by Filipa Lacerda's avatar Filipa Lacerda

Fixes broken tests of quick_submit_spec and reduces tech debt

parent f67ff8e2
/* eslint-disable space-before-function-paren, no-var, no-return-assign, comma-dangle, jasmine/no-spec-dupes, new-cap, max-len */
import '~/behaviors/quick_submit'; import '~/behaviors/quick_submit';
(function() { describe('Quick Submit behavior', () => {
describe('Quick Submit behavior', function() { const keydownEvent = (options = { keyCode: 13, metaKey: true }) => $.Event('keydown', options);
var keydownEvent;
preloadFixtures('issues/open-issue.html.raw'); preloadFixtures('merge_requests/merge_request_with_task_list.html.raw');
beforeEach(function() {
loadFixtures('issues/open-issue.html.raw'); beforeEach(() => {
$('form').submit(function(e) { loadFixtures('merge_requests/merge_request_with_task_list.html.raw');
$('form').submit((e) => {
// Prevent a form submit from moving us off the testing page // Prevent a form submit from moving us off the testing page
return e.preventDefault(); e.preventDefault();
}); });
this.spies = { this.spies = {
submit: spyOnEvent('form', 'submit') submit: spyOnEvent('form', 'submit'),
}; };
this.textarea = $('.js-quick-submit textarea').first(); this.textarea = $('.js-quick-submit textarea').first();
}); });
it('does not respond to other keyCodes', function() {
it('does not respond to other keyCodes', () => {
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
keyCode: 32 keyCode: 32,
})); }));
return expect(this.spies.submit).not.toHaveBeenTriggered(); expect(this.spies.submit).not.toHaveBeenTriggered();
}); });
it('does not respond to Enter alone', function() {
it('does not respond to Enter alone', () => {
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
ctrlKey: false, ctrlKey: false,
metaKey: false metaKey: false,
})); }));
return expect(this.spies.submit).not.toHaveBeenTriggered(); expect(this.spies.submit).not.toHaveBeenTriggered();
}); });
it('does not respond to repeated events', function() {
it('does not respond to repeated events', () => {
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
repeat: true repeat: true,
})); }));
return expect(this.spies.submit).not.toHaveBeenTriggered(); expect(this.spies.submit).not.toHaveBeenTriggered();
}); });
it('disables input of type submit', function() {
it('disables input of type submit', () => {
const submitButton = $('.js-quick-submit input[type=submit]'); const submitButton = $('.js-quick-submit input[type=submit]');
this.textarea.trigger(keydownEvent()); this.textarea.trigger(keydownEvent());
expect(submitButton).toBeDisabled(); expect(submitButton).toBeDisabled();
}); });
it('disables button of type submit', function() { it('disables button of type submit', () => {
const submitButton = $('.js-quick-submit input[type=submit]'); const submitButton = $('.js-quick-submit input[type=submit]');
this.textarea.trigger(keydownEvent()); this.textarea.trigger(keydownEvent());
expect(submitButton).toBeDisabled(); expect(submitButton).toBeDisabled();
}); });
it('only clicks one submit', function() { it('only clicks one submit', () => {
const existingSubmit = $('.js-quick-submit input[type=submit]'); const existingSubmit = $('.js-quick-submit input[type=submit]');
// Add an extra submit button // Add an extra submit button
const newSubmit = $('<button type="submit">Submit it</button>'); const newSubmit = $('<button type="submit">Submit it</button>');
...@@ -66,54 +69,42 @@ import '~/behaviors/quick_submit'; ...@@ -66,54 +69,42 @@ import '~/behaviors/quick_submit';
// We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll // We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll
// only run the tests that apply to the current platform // only run the tests that apply to the current platform
if (navigator.userAgent.match(/Macintosh/)) { if (navigator.userAgent.match(/Macintosh/)) {
it('responds to Meta+Enter', function() { describe('In Macintosh', () => {
it('responds to Meta+Enter', () => {
this.textarea.trigger(keydownEvent()); this.textarea.trigger(keydownEvent());
return expect(this.spies.submit).toHaveBeenTriggered(); return expect(this.spies.submit).toHaveBeenTriggered();
}); });
it('excludes other modifier keys', function() {
it('excludes other modifier keys', () => {
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
altKey: true altKey: true,
})); }));
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
ctrlKey: true ctrlKey: true,
})); }));
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
shiftKey: true shiftKey: true,
})); }));
return expect(this.spies.submit).not.toHaveBeenTriggered(); return expect(this.spies.submit).not.toHaveBeenTriggered();
}); });
});
} else { } else {
it('responds to Ctrl+Enter', function() { it('responds to Ctrl+Enter', () => {
this.textarea.trigger(keydownEvent()); this.textarea.trigger(keydownEvent());
return expect(this.spies.submit).toHaveBeenTriggered(); return expect(this.spies.submit).toHaveBeenTriggered();
}); });
it('excludes other modifier keys', function() {
it('excludes other modifier keys', () => {
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
altKey: true altKey: true,
})); }));
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
metaKey: true metaKey: true,
})); }));
this.textarea.trigger(keydownEvent({ this.textarea.trigger(keydownEvent({
shiftKey: true shiftKey: true,
})); }));
return expect(this.spies.submit).not.toHaveBeenTriggered(); 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));
};
});
}).call(window);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment