BigW Consortium Gitlab

shortcuts_issuable_spec.js 3.14 KB
Newer Older
1
/* eslint-disable space-before-function-paren, no-return-assign, no-var, quotes */
2
/* global ShortcutsIssuable */
Fatih Acet committed
3

4
require('~/copy_as_gfm');
5
require('~/shortcuts_issuable');
Fatih Acet committed
6 7 8

(function() {
  describe('ShortcutsIssuable', function() {
9
    var fixtureName = 'issues/open-issue.html.raw';
10
    preloadFixtures(fixtureName);
Fatih Acet committed
11
    beforeEach(function() {
12
      loadFixtures(fixtureName);
13
      document.querySelector('.js-new-note-form').classList.add('js-main-target-form');
14
      this.shortcut = new ShortcutsIssuable();
Fatih Acet committed
15
    });
16
    describe('#replyWithSelectedText', function() {
Fatih Acet committed
17
      var stubSelection;
18
      // Stub window.gl.utils.getSelectedFragment to return a node with the provided HTML.
19
      stubSelection = function(html) {
20
        window.gl.utils.getSelectedFragment = function() {
21 22 23
          var node = document.createElement('div');
          node.innerHTML = html;
          return node;
Fatih Acet committed
24 25 26
        };
      };
      beforeEach(function() {
27
        this.selector = 'form.js-main-target-form textarea#note_note';
Fatih Acet committed
28 29
      });
      describe('with empty selection', function() {
30
        it('does not return an error', function() {
Fatih Acet committed
31
          this.shortcut.replyWithSelectedText();
32
          expect($(this.selector).val()).toBe('');
Fatih Acet committed
33
        });
34
        it('triggers `focus`', function() {
35
          this.shortcut.replyWithSelectedText();
36
          expect(document.activeElement).toBe(document.querySelector(this.selector));
Fatih Acet committed
37 38 39 40
        });
      });
      describe('with any selection', function() {
        beforeEach(function() {
41
          stubSelection('<p>Selected text.</p>');
Fatih Acet committed
42 43 44 45 46
        });
        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();
47
          expect($(this.selector).val()).toBe("This text was already here.\n\n> Selected text.\n\n");
Fatih Acet committed
48 49
        });
        it('triggers `input`', function() {
50
          var triggered = false;
Fatih Acet committed
51
          $(this.selector).on('input', function() {
52
            triggered = true;
Fatih Acet committed
53 54
          });
          this.shortcut.replyWithSelectedText();
55
          expect(triggered).toBe(true);
Fatih Acet committed
56
        });
57
        it('triggers `focus`', function() {
Fatih Acet committed
58
          this.shortcut.replyWithSelectedText();
59
          expect(document.activeElement).toBe(document.querySelector(this.selector));
Fatih Acet committed
60 61 62
        });
      });
      describe('with a one-line selection', function() {
63
        it('quotes the selection', function() {
64
          stubSelection('<p>This text has been selected.</p>');
Fatih Acet committed
65
          this.shortcut.replyWithSelectedText();
66
          expect($(this.selector).val()).toBe("> This text has been selected.\n\n");
Fatih Acet committed
67 68
        });
      });
69 70
      describe('with a multi-line selection', function() {
        it('quotes the selected lines as a group', function() {
71
          stubSelection("<p>Selected line one.</p>\n\n<p>Selected line two.</p>\n\n<p>Selected line three.</p>");
Fatih Acet committed
72
          this.shortcut.replyWithSelectedText();
73
          expect($(this.selector).val()).toBe("> Selected line one.\n>\n> Selected line two.\n>\n> Selected line three.\n\n");
Fatih Acet committed
74 75 76 77
        });
      });
    });
  });
78
}).call(window);