2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2017-09-10 17:25:29 +05:30
|
|
|
import '~/behaviors/quick_submit';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
describe('Quick Submit behavior', function () {
|
2018-03-17 18:26:18 +05:30
|
|
|
const keydownEvent = (options = { keyCode: 13, metaKey: true }) => $.Event('keydown', options);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
preloadFixtures('snippets/show.html.raw');
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
beforeEach(() => {
|
2018-11-08 19:23:39 +05:30
|
|
|
loadFixtures('snippets/show.html.raw');
|
|
|
|
$('form').submit(e => {
|
2018-03-17 18:26:18 +05:30
|
|
|
// Prevent a form submit from moving us off the testing page
|
|
|
|
e.preventDefault();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
this.spies = {
|
|
|
|
submit: spyOnEvent('form', 'submit'),
|
|
|
|
};
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
this.textarea = $('.js-quick-submit textarea').first();
|
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
afterEach(() => {
|
|
|
|
// Undo what we did to the shared <body>
|
|
|
|
$('body').removeAttr('data-page');
|
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('does not respond to other keyCodes', () => {
|
2018-11-08 19:23:39 +05:30
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
keyCode: 32,
|
|
|
|
}),
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('does not respond to Enter alone', () => {
|
2018-11-08 19:23:39 +05:30
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
ctrlKey: false,
|
|
|
|
metaKey: false,
|
|
|
|
}),
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not respond to repeated events', () => {
|
2018-11-08 19:23:39 +05:30
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
repeat: true,
|
|
|
|
}),
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('disables input of type submit', () => {
|
|
|
|
const submitButton = $('.js-quick-submit input[type=submit]');
|
|
|
|
this.textarea.trigger(keydownEvent());
|
|
|
|
|
|
|
|
expect(submitButton).toBeDisabled();
|
|
|
|
});
|
|
|
|
it('disables button of type submit', () => {
|
|
|
|
const submitButton = $('.js-quick-submit input[type=submit]');
|
|
|
|
this.textarea.trigger(keydownEvent());
|
|
|
|
|
|
|
|
expect(submitButton).toBeDisabled();
|
|
|
|
});
|
|
|
|
it('only clicks one submit', () => {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
// We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll
|
|
|
|
// only run the tests that apply to the current platform
|
|
|
|
if (navigator.userAgent.match(/Macintosh/)) {
|
|
|
|
describe('In Macintosh', () => {
|
|
|
|
it('responds to Meta+Enter', () => {
|
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();
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it('excludes other modifier keys', () => {
|
2018-11-08 19:23:39 +05:30
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
altKey: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
ctrlKey: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
shiftKey: true,
|
|
|
|
}),
|
|
|
|
);
|
2016-09-13 17:45:13 +05:30
|
|
|
return expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
} else {
|
|
|
|
it('responds to Ctrl+Enter', () => {
|
|
|
|
this.textarea.trigger(keydownEvent());
|
|
|
|
return expect(this.spies.submit).toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('excludes other modifier keys', () => {
|
2018-11-08 19:23:39 +05:30
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
altKey: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
metaKey: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
this.textarea.trigger(
|
|
|
|
keydownEvent({
|
|
|
|
shiftKey: true,
|
|
|
|
}),
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
return expect(this.spies.submit).not.toHaveBeenTriggered();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|