gitlab-org--gitlab-foss/spec/javascripts/shortcuts_issuable_spec.js

83 lines
3.2 KiB
JavaScript
Raw Normal View History

/* eslint-disable space-before-function-paren, no-return-assign, no-var, quotes */
/* global ShortcutsIssuable */
2016-07-24 16:45:11 -04:00
require('~/copy_as_gfm');
require('~/shortcuts_issuable');
2016-07-24 16:45:11 -04:00
(function() {
describe('ShortcutsIssuable', function() {
var fixtureName = 'issues/open-issue.html.raw';
preloadFixtures(fixtureName);
2016-07-24 16:45:11 -04:00
beforeEach(function() {
loadFixtures(fixtureName);
document.querySelector('.js-new-note-form').classList.add('js-main-target-form');
this.shortcut = new ShortcutsIssuable();
2016-07-24 16:45:11 -04: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.
stubSelection = function(html) {
2017-01-18 17:19:51 -05:00
window.gl.utils.getSelectedFragment = function() {
var node = document.createElement('div');
node.innerHTML = html;
return node;
2016-07-24 16:45:11 -04:00
};
};
beforeEach(function() {
this.selector = 'form.js-main-target-form textarea#note_note';
2016-07-24 16:45:11 -04:00
});
describe('with empty selection', function() {
it('does not return an error', function() {
2016-07-24 16:45:11 -04:00
this.shortcut.replyWithSelectedText();
expect($(this.selector).val()).toBe('');
2016-07-24 16:45:11 -04:00
});
it('triggers `input`', function() {
var focused = false;
$(this.selector).on('focus', function() {
focused = true;
});
this.shortcut.replyWithSelectedText();
expect(focused).toBe(true);
2016-07-24 16:45:11 -04:00
});
});
describe('with any selection', function() {
beforeEach(function() {
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();
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() {
var triggered = false;
2016-07-24 16:45:11 -04:00
$(this.selector).on('input', function() {
triggered = true;
2016-07-24 16:45:11 -04:00
});
this.shortcut.replyWithSelectedText();
expect(triggered).toBe(true);
2016-07-24 16:45:11 -04: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() {
it('quotes the selection', function() {
stubSelection('<p>This text has been selected.</p>');
2016-07-24 16:45:11 -04:00
this.shortcut.replyWithSelectedText();
expect($(this.selector).val()).toBe("> This text has been selected.\n\n");
2016-07-24 16:45:11 -04:00
});
});
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-07-24 16:45:11 -04:00
this.shortcut.replyWithSelectedText();
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
});
});
});
});
}).call(window);