shortcuts_issuable_spec.js 2.85 KB
Newer Older
Mike Greiling's avatar
Mike Greiling committed
1
import initCopyAsGFM from '~/behaviors/copy_as_gfm';
2
import ShortcutsIssuable from '~/shortcuts_issuable';
Fatih Acet's avatar
Fatih Acet committed
3

Mike Greiling's avatar
Mike Greiling committed
4 5
initCopyAsGFM();

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