2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2021-01-29 00:20:46 +05:30
|
|
|
import Mousetrap from '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
|
|
|
|
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', () => {
|
2021-01-29 00:20:46 +05:30
|
|
|
const snippetShowFixtureName = 'snippets/show.html';
|
|
|
|
const mrShowFixtureName = 'merge_requests/merge_request_of_current_user.html';
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
beforeAll((done) => {
|
2019-03-02 22:35:43 +05:30
|
|
|
initCopyAsGFM();
|
|
|
|
|
|
|
|
// Fake call to nodeToGfm so the import of lazy bundle happened
|
|
|
|
CopyAsGFM.nodeToGFM(document.createElement('div'))
|
|
|
|
.then(() => {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
describe('replyWithSelectedText', () => {
|
|
|
|
const FORM_SELECTOR = '.js-main-target-form .js-vue-comment-form';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(snippetShowFixtureName);
|
|
|
|
$('body').append(
|
|
|
|
`<div class="js-main-target-form">
|
|
|
|
<textarea class="js-vue-comment-form"></textarea>
|
|
|
|
</div>`,
|
|
|
|
);
|
|
|
|
document.querySelector('.js-new-note-form').classList.add('js-main-target-form');
|
|
|
|
|
|
|
|
window.shortcut = new ShortcutsIssuable(true);
|
|
|
|
});
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
afterEach(() => {
|
|
|
|
$(FORM_SELECTOR).remove();
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
delete window.shortcut;
|
|
|
|
});
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
// Stub 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
|
|
|
|
2021-03-08 18:12:59 +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
|
|
|
|
2021-03-08 18:12:59 +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
|
|
|
|
2021-03-08 18:12:59 +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', () => {
|
2021-03-08 18:12:59 +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', () => {
|
2021-03-08 18:12:59 +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);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +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', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
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
|
|
|
});
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
describe('copyBranchName', () => {
|
|
|
|
let sidebarCollapsedBtn;
|
|
|
|
let sidebarExpandedBtn;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(mrShowFixtureName);
|
|
|
|
|
|
|
|
window.shortcut = new ShortcutsIssuable();
|
|
|
|
|
|
|
|
[sidebarCollapsedBtn, sidebarExpandedBtn] = document.querySelectorAll(
|
2021-04-29 21:17:54 +05:30
|
|
|
'.js-sidebar-source-branch button',
|
2021-01-29 00:20:46 +05:30
|
|
|
);
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
[sidebarCollapsedBtn, sidebarExpandedBtn].forEach((btn) => jest.spyOn(btn, 'click'));
|
2021-01-29 00:20:46 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
delete window.shortcut;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the sidebar is expanded', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// simulate the applied CSS styles when the
|
|
|
|
// sidebar is expanded
|
|
|
|
sidebarCollapsedBtn.style.display = 'none';
|
|
|
|
|
|
|
|
Mousetrap.trigger('b');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clicks the "expanded" version of the copy source branch button', () => {
|
|
|
|
expect(sidebarExpandedBtn.click).toHaveBeenCalled();
|
|
|
|
expect(sidebarCollapsedBtn.click).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the sidebar is collapsed', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// simulate the applied CSS styles when the
|
|
|
|
// sidebar is collapsed
|
|
|
|
sidebarExpandedBtn.style.display = 'none';
|
|
|
|
|
|
|
|
Mousetrap.trigger('b');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clicks the "collapsed" version of the copy source branch button', () => {
|
|
|
|
expect(sidebarCollapsedBtn.click).toHaveBeenCalled();
|
|
|
|
expect(sidebarExpandedBtn.click).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|