2019-02-15 15:39:39 +05:30
|
|
|
/* eslint-disable
|
|
|
|
no-underscore-dangle
|
|
|
|
*/
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
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';
|
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';
|
|
|
|
|
|
|
|
describe('ShortcutsIssuable', function() {
|
|
|
|
const fixtureName = 'snippets/show.html.raw';
|
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">
|
|
|
|
<textare class="js-vue-comment-form"></textare>
|
|
|
|
</div>`,
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
document.querySelector('.js-new-note-form').classList.add('js-main-target-form');
|
|
|
|
this.shortcut = new ShortcutsIssuable(true);
|
|
|
|
});
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
$(FORM_SELECTOR).remove();
|
|
|
|
});
|
|
|
|
|
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) => {
|
|
|
|
ShortcutsIssuable.__Rewire__('getSelectedFragment', () => {
|
|
|
|
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
|
|
|
};
|
|
|
|
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`', () => {
|
2018-11-08 19:23:39 +05:30
|
|
|
const spy = spyOn(document.querySelector(FORM_SELECTOR), 'focus');
|
|
|
|
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
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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 => {
|
2018-11-08 19:23:39 +05:30
|
|
|
const spy = spyOn(document.querySelector(FORM_SELECTOR), 'focus');
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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 => {
|
2019-02-15 15:39:39 +05:30
|
|
|
const spy = spyOn(document.querySelector(FORM_SELECTOR), 'focus');
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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 => {
|
2019-02-15 15:39:39 +05:30
|
|
|
const spy = spyOn(document.querySelector(FORM_SELECTOR), 'focus');
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
expect(triggered).toBe(true);
|
|
|
|
done();
|
|
|
|
});
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with a selection in a valid block', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
ShortcutsIssuable.__Rewire__('getSelectedFragment', () => {
|
|
|
|
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);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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 => {
|
2019-02-15 15:39:39 +05:30
|
|
|
const spy = spyOn(document.querySelector(FORM_SELECTOR), 'focus');
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
expect(triggered).toBe(true);
|
|
|
|
done();
|
|
|
|
});
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with a selection in an invalid block', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
ShortcutsIssuable.__Rewire__('getSelectedFragment', () => {
|
|
|
|
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);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
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 => {
|
2019-02-15 15:39:39 +05:30
|
|
|
const spy = spyOn(document.querySelector(FORM_SELECTOR), 'focus');
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
});
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
});
|
2016-09-13 17:45:13 +05:30
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|