shortcuts_issuable_spec.js 2.89 KB
Newer Older
1
/* eslint-disable space-before-function-paren, no-return-assign, no-undef, no-var, quotes, padded-blocks, max-len */
Fatih Acet's avatar
Fatih Acet committed
2 3 4 5 6 7 8 9 10 11 12 13

/*= require shortcuts_issuable */

(function() {
  describe('ShortcutsIssuable', function() {
    fixture.preload('issuable.html');
    beforeEach(function() {
      fixture.load('issuable.html');
      return this.shortcut = new ShortcutsIssuable();
    });
    return describe('#replyWithSelectedText', function() {
      var stubSelection;
14
      // Stub window.getSelection to return the provided String.
Fatih Acet's avatar
Fatih Acet committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
      stubSelection = function(text) {
        return window.getSelection = function() {
          return text;
        };
      };
      beforeEach(function() {
        return this.selector = 'form.js-main-target-form textarea#note_note';
      });
      describe('with empty selection', function() {
        return it('does nothing', function() {
          stubSelection('');
          this.shortcut.replyWithSelectedText();
          return expect($(this.selector).val()).toBe('');
        });
      });
      describe('with any selection', function() {
        beforeEach(function() {
          return stubSelection('Selected text.');
        });
        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();
          return expect($(this.selector).val()).toBe("This text was already here.\n> Selected text.\n\n");
        });
        it('triggers `input`', function() {
          var triggered;
          triggered = false;
          $(this.selector).on('input', function() {
            return triggered = true;
          });
          this.shortcut.replyWithSelectedText();
          return expect(triggered).toBe(true);
        });
        return it('triggers `focus`', function() {
          var focused;
          focused = false;
          $(this.selector).on('focus', function() {
            return focused = true;
          });
          this.shortcut.replyWithSelectedText();
          return expect(focused).toBe(true);
        });
      });
      describe('with a one-line selection', function() {
        return it('quotes the selection', function() {
          stubSelection('This text has been selected.');
          this.shortcut.replyWithSelectedText();
          return expect($(this.selector).val()).toBe("> This text has been selected.\n\n");
        });
      });
      return describe('with a multi-line selection', function() {
        return it('quotes the selected lines as a group', function() {
          stubSelection("Selected line one.\n\nSelected line two.\nSelected line three.\n");
          this.shortcut.replyWithSelectedText();
          return expect($(this.selector).val()).toBe("> Selected line one.\n> Selected line two.\n> Selected line three.\n\n");
        });
      });
    });
  });

}).call(this);