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

79 lines
3.1 KiB
JavaScript
Raw Normal View History

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