debian-mirror-gitlab/spec/frontend/behaviors/shortcuts/shortcuts_issuable_spec.js

323 lines
9.2 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2020-06-23 00:09:42 +05:30
import 'mousetrap';
2019-03-02 22:35:43 +05:30
import initCopyAsGFM, { CopyAsGFM } from '~/behaviors/markdown/copy_as_gfm';
2018-12-05 23:21:45 +05:30
import ShortcutsIssuable from '~/behaviors/shortcuts/shortcuts_issuable';
2020-06-23 00:09:42 +05:30
import { getSelectedFragment } from '~/lib/utils/common_utils';
2016-09-13 17:45:13 +05:30
2018-11-08 19:23:39 +05:30
const FORM_SELECTOR = '.js-main-target-form .js-vue-comment-form';
2020-06-23 00:09:42 +05:30
jest.mock('~/lib/utils/common_utils', () => ({
...jest.requireActual('~/lib/utils/common_utils'),
getSelectedFragment: jest.fn().mockName('getSelectedFragment'),
}));
describe('ShortcutsIssuable', () => {
2019-07-07 11:18:12 +05:30
const fixtureName = 'snippets/show.html';
2020-06-23 00:09:42 +05:30
2018-03-17 18:26:18 +05:30
preloadFixtures(fixtureName);
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
beforeAll(done => {
initCopyAsGFM();
// Fake call to nodeToGfm so the import of lazy bundle happened
CopyAsGFM.nodeToGFM(document.createElement('div'))
.then(() => {
done();
})
.catch(done.fail);
});
2018-03-17 18:26:18 +05:30
beforeEach(() => {
loadFixtures(fixtureName);
2018-11-08 19:23:39 +05:30
$('body').append(
`<div class="js-main-target-form">
2020-06-23 00:09:42 +05:30
<textarea class="js-vue-comment-form"></textarea>
2018-11-08 19:23:39 +05:30
</div>`,
);
2018-03-17 18:26:18 +05:30
document.querySelector('.js-new-note-form').classList.add('js-main-target-form');
2020-06-23 00:09:42 +05:30
window.shortcut = new ShortcutsIssuable(true);
2018-03-17 18:26:18 +05:30
});
2018-11-08 19:23:39 +05:30
afterEach(() => {
$(FORM_SELECTOR).remove();
2020-06-23 00:09:42 +05:30
delete window.shortcut;
2018-11-08 19:23:39 +05:30
});
2018-03-17 18:26:18 +05:30
describe('replyWithSelectedText', () => {
// Stub window.gl.utils.getSelectedFragment to return a node with the provided HTML.
2019-02-15 15:39:39 +05:30
const stubSelection = (html, invalidNode) => {
2020-06-23 00:09:42 +05:30
getSelectedFragment.mockImplementation(() => {
2019-02-15 15:39:39 +05:30
const documentFragment = document.createDocumentFragment();
2018-03-17 18:26:18 +05:30
const node = document.createElement('div');
2019-02-15 15:39:39 +05:30
2018-03-17 18:26:18 +05:30
node.innerHTML = html;
2019-02-15 15:39:39 +05:30
if (!invalidNode) node.className = 'md';
2018-11-08 19:23:39 +05:30
2019-02-15 15:39:39 +05:30
documentFragment.appendChild(node);
return documentFragment;
});
2018-03-17 18:26:18 +05:30
};
2020-03-13 15:44:24 +05:30
2018-03-17 18:26:18 +05:30
describe('with empty selection', () => {
it('does not return an error', () => {
2018-11-08 19:23:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
expect($(FORM_SELECTOR).val()).toBe('');
2016-09-13 17:45:13 +05:30
});
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
it('triggers `focus`', () => {
2020-06-23 00:09:42 +05:30
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
2018-11-08 19:23:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
expect(spy).toHaveBeenCalled();
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
});
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
describe('with any selection', () => {
beforeEach(() => {
stubSelection('<p>Selected text.</p>');
2016-09-13 17:45:13 +05:30
});
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
it('leaves existing input intact', done => {
2018-11-08 19:23:39 +05:30
$(FORM_SELECTOR).val('This text was already here.');
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect($(FORM_SELECTOR).val()).toBe('This text was already here.');
ShortcutsIssuable.replyWithSelectedText(true);
2018-12-13 13:39:08 +05:30
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect($(FORM_SELECTOR).val()).toBe(
'This text was already here.\n\n> Selected text.\n\n',
);
done();
});
2016-09-13 17:45:13 +05:30
});
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
it('triggers `input`', done => {
2018-03-17 18:26:18 +05:30
let triggered = false;
2018-11-08 19:23:39 +05:30
$(FORM_SELECTOR).on('input', () => {
2018-03-17 18:26:18 +05:30
triggered = true;
2016-09-13 17:45:13 +05:30
});
2018-11-08 19:23:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2018-12-13 13:39:08 +05:30
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect(triggered).toBe(true);
done();
});
2018-03-17 18:26:18 +05:30
});
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
it('triggers `focus`', done => {
2020-06-23 00:09:42 +05:30
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
2018-11-08 19:23:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect(spy).toHaveBeenCalled();
done();
});
2018-03-17 18:26:18 +05:30
});
});
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
describe('with a one-line selection', () => {
2019-03-02 22:35:43 +05:30
it('quotes the selection', done => {
2018-03-17 18:26:18 +05:30
stubSelection('<p>This text has been selected.</p>');
2018-11-08 19:23:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect($(FORM_SELECTOR).val()).toBe('> This text has been selected.\n\n');
done();
});
2018-03-17 18:26:18 +05:30
});
});
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
describe('with a multi-line selection', () => {
2019-03-02 22:35:43 +05:30
it('quotes the selected lines as a group', done => {
2018-11-08 19:23:39 +05:30
stubSelection(
'<p>Selected line one.</p>\n<p>Selected line two.</p>\n<p>Selected line three.</p>',
);
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect($(FORM_SELECTOR).val()).toBe(
'> Selected line one.\n>\n> Selected line two.\n>\n> Selected line three.\n\n',
);
done();
});
2016-09-13 17:45:13 +05:30
});
});
2019-02-15 15:39:39 +05:30
describe('with an invalid selection', () => {
beforeEach(() => {
stubSelection('<p>Selected text.</p>', true);
});
2019-03-02 22:35:43 +05:30
it('does not add anything to the input', done => {
2019-02-15 15:39:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect($(FORM_SELECTOR).val()).toBe('');
done();
});
2019-02-15 15:39:39 +05:30
});
2019-03-02 22:35:43 +05:30
it('triggers `focus`', done => {
2020-06-23 00:09:42 +05:30
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
2019-02-15 15:39:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect(spy).toHaveBeenCalled();
done();
});
2019-02-15 15:39:39 +05:30
});
});
describe('with a semi-valid selection', () => {
beforeEach(() => {
stubSelection('<div class="md">Selected text.</div><p>Invalid selected text.</p>', true);
});
2019-03-02 22:35:43 +05:30
it('only adds the valid part to the input', done => {
2019-02-15 15:39:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect($(FORM_SELECTOR).val()).toBe('> Selected text.\n\n');
done();
});
2019-02-15 15:39:39 +05:30
});
2019-03-02 22:35:43 +05:30
it('triggers `focus`', done => {
2020-06-23 00:09:42 +05:30
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
2019-02-15 15:39:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect(spy).toHaveBeenCalled();
done();
});
2019-02-15 15:39:39 +05:30
});
2019-03-02 22:35:43 +05:30
it('triggers `input`', done => {
2019-02-15 15:39:39 +05:30
let triggered = false;
$(FORM_SELECTOR).on('input', () => {
triggered = true;
});
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect(triggered).toBe(true);
done();
});
2019-02-15 15:39:39 +05:30
});
});
describe('with a selection in a valid block', () => {
beforeEach(() => {
2020-06-23 00:09:42 +05:30
getSelectedFragment.mockImplementation(() => {
2019-02-15 15:39:39 +05:30
const documentFragment = document.createDocumentFragment();
const node = document.createElement('div');
const originalNode = document.createElement('body');
originalNode.innerHTML = `<div class="issue">
<div class="otherElem">Text...</div>
<div class="md"><p><em>Selected text.</em></p></div>
</div>`;
documentFragment.originalNodes = [originalNode.querySelector('em')];
node.innerHTML = '<em>Selected text.</em>';
documentFragment.appendChild(node);
return documentFragment;
});
});
2019-03-02 22:35:43 +05:30
it('adds the quoted selection to the input', done => {
2019-02-15 15:39:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect($(FORM_SELECTOR).val()).toBe('> *Selected text.*\n\n');
done();
});
2019-02-15 15:39:39 +05:30
});
2019-03-02 22:35:43 +05:30
it('triggers `focus`', done => {
2020-06-23 00:09:42 +05:30
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
2019-02-15 15:39:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect(spy).toHaveBeenCalled();
done();
});
2019-02-15 15:39:39 +05:30
});
2019-03-02 22:35:43 +05:30
it('triggers `input`', done => {
2019-02-15 15:39:39 +05:30
let triggered = false;
$(FORM_SELECTOR).on('input', () => {
triggered = true;
});
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect(triggered).toBe(true);
done();
});
2019-02-15 15:39:39 +05:30
});
});
describe('with a selection in an invalid block', () => {
beforeEach(() => {
2020-06-23 00:09:42 +05:30
getSelectedFragment.mockImplementation(() => {
2019-02-15 15:39:39 +05:30
const documentFragment = document.createDocumentFragment();
const node = document.createElement('div');
const originalNode = document.createElement('body');
originalNode.innerHTML = `<div class="issue">
<div class="otherElem"><div><b>Selected text.</b></div></div>
<div class="md"><p><em>Valid text</em></p></div>
</div>`;
documentFragment.originalNodes = [originalNode.querySelector('b')];
node.innerHTML = '<b>Selected text.</b>';
documentFragment.appendChild(node);
return documentFragment;
});
});
2019-03-02 22:35:43 +05:30
it('does not add anything to the input', done => {
2019-02-15 15:39:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect($(FORM_SELECTOR).val()).toBe('');
done();
});
2019-02-15 15:39:39 +05:30
});
2019-03-02 22:35:43 +05:30
it('triggers `focus`', done => {
2020-06-23 00:09:42 +05:30
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
2019-02-15 15:39:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-03-02 22:35:43 +05:30
expect(spy).toHaveBeenCalled();
done();
});
2019-02-15 15:39:39 +05:30
});
});
2020-03-13 15:44:24 +05:30
describe('with a valid selection with no text content', () => {
it('returns the proper markdown', done => {
2020-06-23 00:09:42 +05:30
stubSelection('<img src="https://gitlab.com/logo.png" alt="logo" />');
2020-03-13 15:44:24 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
2020-06-23 00:09:42 +05:30
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe('> ![logo](https://gitlab.com/logo.png)\n\n');
2020-03-13 15:44:24 +05:30
done();
});
});
});
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
});