2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2017-05-16 21:01:51 +00:00
|
|
|
import '~/behaviors/quick_submit';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
describe('Quick Submit behavior', () => {
|
|
|
|
let testContext;
|
|
|
|
|
2017-08-10 13:08:57 +00:00
|
|
|
const keydownEvent = (options = { keyCode: 13, metaKey: true }) => $.Event('keydown', options);
|
2017-02-09 11:03:41 +00:00
|
|
|
|
2017-08-10 13:08:57 +00:00
|
|
|
beforeEach(() => {
|
2019-03-26 16:03:28 +00:00
|
|
|
loadFixtures('snippets/show.html');
|
2020-02-28 18:09:07 +00:00
|
|
|
|
|
|
|
testContext = {};
|
|
|
|
|
|
|
|
testContext.spies = {
|
|
|
|
submit: jest.fn(),
|
|
|
|
};
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
$('form').submit((e) => {
|
2017-08-10 13:08:57 +00:00
|
|
|
// Prevent a form submit from moving us off the testing page
|
|
|
|
e.preventDefault();
|
2020-02-28 18:09:07 +00:00
|
|
|
// Explicitly call the spie to know this function get's not called
|
|
|
|
testContext.spies.submit();
|
2017-02-09 11:47:55 +00:00
|
|
|
});
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea = $('.js-quick-submit textarea').first();
|
2017-10-06 20:36:15 +00:00
|
|
|
});
|
|
|
|
|
2017-08-10 13:08:57 +00:00
|
|
|
it('does not respond to other keyCodes', () => {
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
keyCode: 32,
|
|
|
|
}),
|
|
|
|
);
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
expect(testContext.spies.submit).not.toHaveBeenCalled();
|
2017-08-10 13:08:57 +00:00
|
|
|
});
|
2017-06-27 16:06:38 +00:00
|
|
|
|
2017-08-10 13:08:57 +00:00
|
|
|
it('does not respond to Enter alone', () => {
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
ctrlKey: false,
|
|
|
|
metaKey: false,
|
|
|
|
}),
|
|
|
|
);
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
expect(testContext.spies.submit).not.toHaveBeenCalled();
|
2017-08-10 13:08:57 +00:00
|
|
|
});
|
2017-06-27 16:06:38 +00:00
|
|
|
|
2017-08-10 13:08:57 +00:00
|
|
|
it('does not respond to repeated events', () => {
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
repeat: true,
|
|
|
|
}),
|
|
|
|
);
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
expect(testContext.spies.submit).not.toHaveBeenCalled();
|
2017-08-10 13:08:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('disables input of type submit', () => {
|
|
|
|
const submitButton = $('.js-quick-submit input[type=submit]');
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(keydownEvent());
|
2017-08-10 13:08:57 +00:00
|
|
|
|
|
|
|
expect(submitButton).toBeDisabled();
|
|
|
|
});
|
2018-10-09 17:01:49 +00:00
|
|
|
|
2017-08-10 13:08:57 +00:00
|
|
|
it('disables button of type submit', () => {
|
|
|
|
const submitButton = $('.js-quick-submit input[type=submit]');
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(keydownEvent());
|
2017-08-10 13:08:57 +00:00
|
|
|
|
|
|
|
expect(submitButton).toBeDisabled();
|
|
|
|
});
|
2018-10-09 17:01:49 +00:00
|
|
|
|
2017-08-10 13:08:57 +00:00
|
|
|
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>');
|
2020-02-28 18:09:07 +00:00
|
|
|
newSubmit.insertAfter(testContext.textarea);
|
2017-08-10 13:08:57 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
const spies = {
|
|
|
|
oldClickSpy: jest.fn(),
|
|
|
|
newClickSpy: jest.fn(),
|
|
|
|
};
|
|
|
|
existingSubmit.on('click', () => {
|
|
|
|
spies.oldClickSpy();
|
|
|
|
});
|
|
|
|
newSubmit.on('click', () => {
|
|
|
|
spies.newClickSpy();
|
|
|
|
});
|
2017-08-10 13:08:57 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(keydownEvent());
|
2017-08-10 13:08:57 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
expect(spies.oldClickSpy).not.toHaveBeenCalled();
|
|
|
|
expect(spies.newClickSpy).toHaveBeenCalled();
|
2017-08-10 13:08:57 +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
|
|
|
|
if (navigator.userAgent.match(/Macintosh/)) {
|
|
|
|
describe('In Macintosh', () => {
|
|
|
|
it('responds to Meta+Enter', () => {
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(keydownEvent());
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
expect(testContext.spies.submit).toHaveBeenCalled();
|
2016-07-24 20:45:11 +00:00
|
|
|
});
|
2017-08-10 13:08:57 +00:00
|
|
|
|
|
|
|
it('excludes other modifier keys', () => {
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
altKey: true,
|
|
|
|
}),
|
|
|
|
);
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
ctrlKey: true,
|
|
|
|
}),
|
|
|
|
);
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
shiftKey: true,
|
|
|
|
}),
|
|
|
|
);
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
expect(testContext.spies.submit).not.toHaveBeenCalled();
|
2016-07-24 20:45:11 +00:00
|
|
|
});
|
2017-08-10 13:08:57 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
it('responds to Ctrl+Enter', () => {
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(keydownEvent());
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
expect(testContext.spies.submit).toHaveBeenCalled();
|
2017-08-10 13:08:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('excludes other modifier keys', () => {
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
altKey: true,
|
|
|
|
}),
|
|
|
|
);
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
metaKey: true,
|
|
|
|
}),
|
|
|
|
);
|
2020-02-28 18:09:07 +00:00
|
|
|
testContext.textarea.trigger(
|
2018-06-21 12:22:40 +00:00
|
|
|
keydownEvent({
|
|
|
|
shiftKey: true,
|
|
|
|
}),
|
|
|
|
);
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2020-02-28 18:09:07 +00:00
|
|
|
expect(testContext.spies.submit).not.toHaveBeenCalled();
|
2017-08-10 13:08:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|