debian-mirror-gitlab/spec/javascripts/shortcuts_issuable_spec.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
import initCopyAsGFM from '~/behaviors/markdown/copy_as_gfm';
2018-03-17 18:26:18 +05:30
import ShortcutsIssuable from '~/shortcuts_issuable';
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
initCopyAsGFM();
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
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.
2018-11-08 19:23:39 +05:30
const stubSelection = html => {
2018-03-17 18:26:18 +05:30
window.gl.utils.getSelectedFragment = () => {
const node = document.createElement('div');
node.innerHTML = html;
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
return node;
2016-09-13 17:45:13 +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`', () => {
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
2018-03-17 18:26:18 +05:30
it('leaves existing input intact', () => {
2018-11-08 19:23:39 +05:30
$(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-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 `input`', () => {
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-03-17 18:26:18 +05:30
expect(triggered).toBe(true);
});
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();
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', () => {
it('quotes the selection', () => {
stubSelection('<p>This text has been selected.</p>');
2018-11-08 19:23:39 +05:30
ShortcutsIssuable.replyWithSelectedText(true);
expect($(FORM_SELECTOR).val()).toBe('> This text has been selected.\n\n');
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', () => {
it('quotes the selected lines as a group', () => {
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);
expect($(FORM_SELECTOR).val()).toBe(
'> Selected line one.\n>\n> Selected line two.\n>\n> Selected line three.\n\n',
);
2016-09-13 17:45:13 +05:30
});
});
});
2018-03-17 18:26:18 +05:30
});