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-06-21 08:22:40 -04:00
|
|
|
const FORM_SELECTOR = '.js-main-target-form .js-vue-comment-form';
|
|
|
|
|
|
|
|
describe('ShortcutsIssuable', function() {
|
|
|
|
const fixtureName = 'snippets/show.html.raw';
|
2017-08-10 11:48:22 -04:00
|
|
|
preloadFixtures(fixtureName);
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(fixtureName);
|
2018-06-21 08:22:40 -04:00
|
|
|
$('body').append(
|
|
|
|
`<div class="js-main-target-form">
|
|
|
|
<textare class="js-vue-comment-form"></textare>
|
|
|
|
</div>`,
|
|
|
|
);
|
2017-08-10 11:48:22 -04:00
|
|
|
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
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
$(FORM_SELECTOR).remove();
|
|
|
|
});
|
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
describe('replyWithSelectedText', () => {
|
|
|
|
// Stub window.gl.utils.getSelectedFragment to return a node with the provided HTML.
|
2018-06-21 08:22:40 -04:00
|
|
|
const stubSelection = html => {
|
2017-08-10 11:48:22 -04:00
|
|
|
window.gl.utils.getSelectedFragment = () => {
|
|
|
|
const node = document.createElement('div');
|
|
|
|
node.innerHTML = html;
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
return node;
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2017-08-10 11:48:22 -04:00
|
|
|
};
|
|
|
|
describe('with empty selection', () => {
|
|
|
|
it('does not return an error', () => {
|
2018-06-21 08:22:40 -04:00
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
|
|
|
expect($(FORM_SELECTOR).val()).toBe('');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
it('triggers `focus`', () => {
|
2018-06-21 08:22:40 -04:00
|
|
|
const spy = spyOn(document.querySelector(FORM_SELECTOR), 'focus');
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalled();
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-08-10 11:48:22 -04:00
|
|
|
});
|
2018-06-21 08:22:40 -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
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
it('leaves existing input intact', () => {
|
2018-06-21 08:22:40 -04:00
|
|
|
$(FORM_SELECTOR).val('This text was already here.');
|
|
|
|
expect($(FORM_SELECTOR).val()).toBe('This text was already here.');
|
|
|
|
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
expect($(FORM_SELECTOR).val()).toBe('This text was already here.\n\n> Selected text.\n\n');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
it('triggers `input`', () => {
|
|
|
|
let triggered = false;
|
2018-06-21 08:22:40 -04:00
|
|
|
$(FORM_SELECTOR).on('input', () => {
|
2017-08-10 11:48:22 -04:00
|
|
|
triggered = true;
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
2017-08-10 11:48:22 -04:00
|
|
|
expect(triggered).toBe(true);
|
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
it('triggers `focus`', () => {
|
2018-06-21 08:22:40 -04:00
|
|
|
const spy = spyOn(document.querySelector(FORM_SELECTOR), 'focus');
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalled();
|
2017-08-10 11:48:22 -04:00
|
|
|
});
|
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
describe('with a one-line selection', () => {
|
|
|
|
it('quotes the selection', () => {
|
|
|
|
stubSelection('<p>This text has been selected.</p>');
|
2018-06-21 08:22:40 -04:00
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
|
|
|
expect($(FORM_SELECTOR).val()).toBe('> This text has been selected.\n\n');
|
2017-08-10 11:48:22 -04:00
|
|
|
});
|
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-08-10 11:48:22 -04:00
|
|
|
describe('with a multi-line selection', () => {
|
|
|
|
it('quotes the selected lines as a group', () => {
|
2018-06-21 08:22:40 -04:00
|
|
|
stubSelection(
|
|
|
|
'<p>Selected line one.</p>\n<p>Selected line two.</p>\n<p>Selected line three.</p>',
|
|
|
|
);
|
|
|
|
ShortcutsIssuable.replyWithSelectedText(true);
|
|
|
|
|
|
|
|
expect($(FORM_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
|
|
|
});
|