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

154 lines
3.9 KiB
JavaScript
Raw Normal View History

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