2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-03-20 00:01:17 -04:00
|
|
|
import initCopyAsGFM from '~/behaviors/markdown/copy_as_gfm';
|
2017-10-10 05:10:11 -04:00
|
|
|
import ShortcutsIssuable from '~/shortcuts_issuable';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-10-12 23:41:21 -04:00
|
|
|
initCopyAsGFM();
|
|
|
|
|
2018-03-07 15:55:23 -05:00
|
|
|
describe('ShortcutsIssuable', function () {
|
2017-08-10 11:48:22 -04:00
|
|
|
const fixtureName = 'merge_requests/diff_comment.html.raw';
|
|
|
|
preloadFixtures(fixtureName);
|
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(fixtureName);
|
|
|
|
document.querySelector('.js-new-note-form').classList.add('js-main-target-form');
|
2017-08-10 19:13:12 -04:00
|
|
|
this.shortcut = new ShortcutsIssuable(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
});
|
|
|
|
describe('replyWithSelectedText', () => {
|
|
|
|
// Stub window.gl.utils.getSelectedFragment to return a node with the provided HTML.
|
|
|
|
const stubSelection = (html) => {
|
|
|
|
window.gl.utils.getSelectedFragment = () => {
|
|
|
|
const node = document.createElement('div');
|
|
|
|
node.innerHTML = html;
|
|
|
|
return node;
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2017-08-10 11:48:22 -04:00
|
|
|
};
|
|
|
|
beforeEach(() => {
|
2017-08-10 19:13:12 -04:00
|
|
|
this.selector = '.js-main-target-form #note_note';
|
2017-08-10 11:48:22 -04:00
|
|
|
});
|
|
|
|
describe('with empty selection', () => {
|
|
|
|
it('does not return an error', () => {
|
2017-08-10 19:13:12 -04:00
|
|
|
this.shortcut.replyWithSelectedText(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
expect($(this.selector).val()).toBe('');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-08-10 11:48:22 -04:00
|
|
|
it('triggers `focus`', () => {
|
2017-08-10 19:13:12 -04:00
|
|
|
this.shortcut.replyWithSelectedText(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
expect(document.activeElement).toBe(document.querySelector(this.selector));
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-08-10 11:48:22 -04:00
|
|
|
});
|
|
|
|
describe('with any selection', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
stubSelection('<p>Selected text.</p>');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-08-10 11:48:22 -04:00
|
|
|
it('leaves existing input intact', () => {
|
|
|
|
$(this.selector).val('This text was already here.');
|
|
|
|
expect($(this.selector).val()).toBe('This text was already here.');
|
2017-08-10 19:13:12 -04:00
|
|
|
this.shortcut.replyWithSelectedText(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
expect($(this.selector).val()).toBe('This text was already here.\n\n> Selected text.\n\n');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-08-10 11:48:22 -04:00
|
|
|
it('triggers `input`', () => {
|
|
|
|
let triggered = false;
|
|
|
|
$(this.selector).on('input', () => {
|
|
|
|
triggered = true;
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-08-10 19:13:12 -04:00
|
|
|
this.shortcut.replyWithSelectedText(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
expect(triggered).toBe(true);
|
|
|
|
});
|
|
|
|
it('triggers `focus`', () => {
|
2017-08-10 19:13:12 -04:00
|
|
|
this.shortcut.replyWithSelectedText(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
expect(document.activeElement).toBe(document.querySelector(this.selector));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('with a one-line selection', () => {
|
|
|
|
it('quotes the selection', () => {
|
|
|
|
stubSelection('<p>This text has been selected.</p>');
|
2017-08-10 19:13:12 -04:00
|
|
|
this.shortcut.replyWithSelectedText(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
expect($(this.selector).val()).toBe('> This text has been selected.\n\n');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('with a multi-line selection', () => {
|
|
|
|
it('quotes the selected lines as a group', () => {
|
2018-06-14 04:30:16 -04:00
|
|
|
stubSelection('<p>Selected line one.</p>\n<p>Selected line two.</p>\n<p>Selected line three.</p>');
|
2017-08-10 19:13:12 -04:00
|
|
|
this.shortcut.replyWithSelectedText(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
expect($(this.selector).val()).toBe('> Selected line one.\n>\n> Selected line two.\n>\n> Selected line three.\n\n');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-08-10 11:48:22 -04:00
|
|
|
});
|