gitlab-org--gitlab-foss/spec/javascripts/behaviors/quick_submit_spec.js

107 lines
3.5 KiB
JavaScript
Raw Normal View History

/* eslint-disable space-before-function-paren, no-var, no-return-assign, comma-dangle, jasmine/no-spec-dupes, new-cap, max-len */
2016-07-24 20:45:11 +00:00
import '~/behaviors/quick_submit';
2016-07-24 20:45:11 +00:00
(function() {
describe('Quick Submit behavior', function() {
var keydownEvent;
preloadFixtures('issues/open-issue.html.raw');
2016-07-24 20:45:11 +00:00
beforeEach(function() {
loadFixtures('issues/open-issue.html.raw');
2016-07-24 20:45:11 +00:00
$('form').submit(function(e) {
// Prevent a form submit from moving us off the testing page
2016-07-24 20:45:11 +00:00
return e.preventDefault();
});
2017-02-09 11:03:41 +00:00
this.spies = {
2016-07-24 20:45:11 +00:00
submit: spyOnEvent('form', 'submit')
};
2017-02-09 11:03:41 +00:00
this.textarea = $('.js-quick-submit textarea').first();
2016-07-24 20:45:11 +00:00
});
it('does not respond to other keyCodes', function() {
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
keyCode: 32
}));
return expect(this.spies.submit).not.toHaveBeenTriggered();
});
it('does not respond to Enter alone', function() {
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
ctrlKey: false,
metaKey: false
}));
return expect(this.spies.submit).not.toHaveBeenTriggered();
});
it('does not respond to repeated events', function() {
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
repeat: true
}));
return expect(this.spies.submit).not.toHaveBeenTriggered();
});
it('disables input of type submit', function() {
const submitButton = $('.js-quick-submit input[type=submit]');
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent());
expect(submitButton).toBeDisabled();
});
it('disables button of type submit', function() {
// button doesn't exist in fixture, add it manually
const submitButton = $('<button type="submit">Submit it</button>');
submitButton.insertAfter(this.textarea);
this.textarea.trigger(keydownEvent());
expect(submitButton).toBeDisabled();
2016-07-24 20:45:11 +00:00
});
2017-02-07 01:42:39 +00:00
// We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll
// only run the tests that apply to the current platform
2016-07-24 20:45:11 +00:00
if (navigator.userAgent.match(/Macintosh/)) {
it('responds to Meta+Enter', function() {
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent());
2016-07-24 20:45:11 +00:00
return expect(this.spies.submit).toHaveBeenTriggered();
});
it('excludes other modifier keys', function() {
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
altKey: true
}));
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
ctrlKey: true
}));
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
shiftKey: true
}));
return expect(this.spies.submit).not.toHaveBeenTriggered();
});
} else {
it('responds to Ctrl+Enter', function() {
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent());
2016-07-24 20:45:11 +00:00
return expect(this.spies.submit).toHaveBeenTriggered();
});
it('excludes other modifier keys', function() {
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
altKey: true
}));
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
metaKey: true
}));
2017-02-09 11:03:41 +00:00
this.textarea.trigger(keydownEvent({
2016-07-24 20:45:11 +00:00
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));
};
});
}).call(window);