2017-01-10 18:02:20 -05:00
|
|
|
/* eslint-disable space-before-function-paren, no-return-assign, no-var, quotes */
|
2016-12-14 00:26:26 -05:00
|
|
|
/* global ShortcutsIssuable */
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-01-27 20:33:58 -05:00
|
|
|
require('~/copy_as_gfm');
|
2017-01-09 18:23:54 -05:00
|
|
|
require('~/shortcuts_issuable');
|
2016-07-24 16:45:11 -04:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
describe('ShortcutsIssuable', function() {
|
2016-11-22 19:08:54 -05:00
|
|
|
var fixtureName = 'issues/open-issue.html.raw';
|
2016-12-30 19:14:33 -05:00
|
|
|
preloadFixtures(fixtureName);
|
2016-07-24 16:45:11 -04:00
|
|
|
beforeEach(function() {
|
2016-12-30 19:14:33 -05:00
|
|
|
loadFixtures(fixtureName);
|
2016-11-22 19:08:54 -05:00
|
|
|
document.querySelector('.js-new-note-form').classList.add('js-main-target-form');
|
2017-01-31 14:29:34 -05:00
|
|
|
this.shortcut = new ShortcutsIssuable();
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-01-31 14:29:34 -05:00
|
|
|
describe('#replyWithSelectedText', function() {
|
2016-07-24 16:45:11 -04:00
|
|
|
var stubSelection;
|
2017-01-18 17:19:51 -05:00
|
|
|
// Stub window.gl.utils.getSelectedFragment to return a node with the provided HTML.
|
2017-01-17 13:07:20 -05:00
|
|
|
stubSelection = function(html) {
|
2017-01-18 17:19:51 -05:00
|
|
|
window.gl.utils.getSelectedFragment = function() {
|
2017-01-17 13:07:20 -05:00
|
|
|
var node = document.createElement('div');
|
|
|
|
node.innerHTML = html;
|
|
|
|
return node;
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
beforeEach(function() {
|
2017-01-31 14:29:34 -05:00
|
|
|
this.selector = 'form.js-main-target-form textarea#note_note';
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
describe('with empty selection', function() {
|
2017-01-30 23:12:31 -05:00
|
|
|
it('does not return an error', function() {
|
2016-07-24 16:45:11 -04:00
|
|
|
this.shortcut.replyWithSelectedText();
|
2017-01-31 14:29:34 -05:00
|
|
|
expect($(this.selector).val()).toBe('');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-01-31 14:29:34 -05:00
|
|
|
it('triggers `input`', function() {
|
|
|
|
var focused = false;
|
2017-01-30 23:12:31 -05:00
|
|
|
$(this.selector).on('focus', function() {
|
2017-01-31 14:29:34 -05:00
|
|
|
focused = true;
|
2017-01-30 23:12:31 -05:00
|
|
|
});
|
|
|
|
this.shortcut.replyWithSelectedText();
|
2017-01-31 14:29:34 -05:00
|
|
|
expect(focused).toBe(true);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('with any selection', function() {
|
|
|
|
beforeEach(function() {
|
2017-01-31 14:29:34 -05:00
|
|
|
stubSelection('<p>Selected text.</p>');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
it('leaves existing input intact', function() {
|
|
|
|
$(this.selector).val('This text was already here.');
|
|
|
|
expect($(this.selector).val()).toBe('This text was already here.');
|
|
|
|
this.shortcut.replyWithSelectedText();
|
2017-01-31 14:29:34 -05: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
|
|
|
});
|
|
|
|
it('triggers `input`', function() {
|
2017-01-31 14:29:34 -05:00
|
|
|
var triggered = false;
|
2016-07-24 16:45:11 -04:00
|
|
|
$(this.selector).on('input', function() {
|
2017-01-31 14:29:34 -05:00
|
|
|
triggered = true;
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
this.shortcut.replyWithSelectedText();
|
2017-01-31 14:29:34 -05:00
|
|
|
expect(triggered).toBe(true);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-01-31 14:29:34 -05:00
|
|
|
it('triggers `focus`', function() {
|
2016-07-24 16:45:11 -04:00
|
|
|
this.shortcut.replyWithSelectedText();
|
2017-01-09 14:50:13 -05:00
|
|
|
expect(document.activeElement).toBe(document.querySelector(this.selector));
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('with a one-line selection', function() {
|
2017-01-31 14:29:34 -05:00
|
|
|
it('quotes the selection', function() {
|
2017-01-17 13:07:20 -05:00
|
|
|
stubSelection('<p>This text has been selected.</p>');
|
2016-07-24 16:45:11 -04:00
|
|
|
this.shortcut.replyWithSelectedText();
|
2017-01-31 14:29:34 -05:00
|
|
|
expect($(this.selector).val()).toBe("> This text has been selected.\n\n");
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
});
|
2017-01-31 14:29:34 -05:00
|
|
|
describe('with a multi-line selection', function() {
|
|
|
|
it('quotes the selected lines as a group', function() {
|
2017-01-17 13:07:20 -05:00
|
|
|
stubSelection("<p>Selected line one.</p>\n\n<p>Selected line two.</p>\n\n<p>Selected line three.</p>");
|
2016-07-24 16:45:11 -04:00
|
|
|
this.shortcut.replyWithSelectedText();
|
2017-01-31 14:29:34 -05: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-02-10 02:29:41 -05:00
|
|
|
}).call(window);
|