debian-mirror-gitlab/spec/frontend/behaviors/quick_submit_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

157 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2022-07-16 23:28:13 +05:30
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2017-09-10 17:25:29 +05:30
import '~/behaviors/quick_submit';
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
describe('Quick Submit behavior', () => {
let testContext;
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-03-17 18:26:18 +05:30
beforeEach(() => {
2022-07-16 23:28:13 +05:30
loadHTMLFixture('snippets/show.html');
2020-04-08 14:13:33 +05:30
testContext = {};
testContext.spies = {
submit: jest.fn(),
};
2021-03-08 18:12:59 +05:30
$('form').submit((e) => {
2018-03-17 18:26:18 +05:30
// Prevent a form submit from moving us off the testing page
e.preventDefault();
2020-04-08 14:13:33 +05:30
// Explicitly call the spie to know this function get's not called
testContext.spies.submit();
2017-08-17 22:00:37 +05:30
});
2020-04-08 14:13:33 +05:30
testContext.textarea = $('.js-quick-submit textarea').first();
2018-03-17 18:26:18 +05:30
});
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2018-03-17 18:26:18 +05:30
it('does not respond to other keyCodes', () => {
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
keyCode: 32,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(testContext.spies.submit).not.toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
it('does not respond to Enter alone', () => {
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
ctrlKey: false,
metaKey: false,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(testContext.spies.submit).not.toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
it('does not respond to repeated events', () => {
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
repeat: true,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(testContext.spies.submit).not.toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
it('disables input of type submit', () => {
const submitButton = $('.js-quick-submit input[type=submit]');
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(keydownEvent());
2018-03-17 18:26:18 +05:30
expect(submitButton).toBeDisabled();
});
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
it('disables button of type submit', () => {
const submitButton = $('.js-quick-submit input[type=submit]');
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(keydownEvent());
2018-03-17 18:26:18 +05:30
expect(submitButton).toBeDisabled();
});
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
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-04-08 14:13:33 +05:30
newSubmit.insertAfter(testContext.textarea);
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
const spies = {
oldClickSpy: jest.fn(),
newClickSpy: jest.fn(),
};
existingSubmit.on('click', () => {
spies.oldClickSpy();
});
newSubmit.on('click', () => {
spies.newClickSpy();
});
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(keydownEvent());
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
expect(spies.oldClickSpy).not.toHaveBeenCalled();
expect(spies.newClickSpy).toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
// 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-04-08 14:13:33 +05:30
testContext.textarea.trigger(keydownEvent());
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(testContext.spies.submit).toHaveBeenCalled();
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
it('excludes other modifier keys', () => {
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
altKey: true,
}),
);
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
ctrlKey: true,
}),
);
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
shiftKey: true,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(testContext.spies.submit).not.toHaveBeenCalled();
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
});
} else {
it('responds to Ctrl+Enter', () => {
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(keydownEvent());
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(testContext.spies.submit).toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
it('excludes other modifier keys', () => {
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
altKey: true,
}),
);
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
metaKey: true,
}),
);
2020-04-08 14:13:33 +05:30
testContext.textarea.trigger(
2018-11-08 19:23:39 +05:30
keydownEvent({
shiftKey: true,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(testContext.spies.submit).not.toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
}
});