BigW Consortium Gitlab

shortcuts_issuable_spec.js.coffee 2.13 KB
Newer Older
1
#= require shortcuts_issuable
2

3
describe 'ShortcutsIssuable', ->
4 5
  fixture.preload('issuable.html')

6
  beforeEach ->
7
    fixture.load('issuable.html')
8
    @shortcut = new ShortcutsIssuable()
9 10 11 12 13 14 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 77 78 79 80 81 82

  describe '#replyWithSelectedText', ->
    # Stub window.getSelection to return the provided String.
    stubSelection = (text) ->
      window.getSelection = -> text

    beforeEach ->
      @selector = 'form.js-main-target-form textarea#note_note'

    describe 'with empty selection', ->
      it 'does nothing', ->
        stubSelection('')
        @shortcut.replyWithSelectedText()
        expect($(@selector).val()).toBe('')

    describe 'with any selection', ->
      beforeEach ->
        stubSelection('Selected text.')

      it 'leaves existing input intact', ->
        $(@selector).val('This text was already here.')
        expect($(@selector).val()).toBe('This text was already here.')

        @shortcut.replyWithSelectedText()
        expect($(@selector).val()).
          toBe("This text was already here.\n> Selected text.\n\n")

      it 'triggers `input`', ->
        triggered = false
        $(@selector).on 'input', -> triggered = true
        @shortcut.replyWithSelectedText()

        expect(triggered).toBe(true)

      it 'triggers `focus`', ->
        focused = false
        $(@selector).on 'focus', -> focused = true
        @shortcut.replyWithSelectedText()

        expect(focused).toBe(true)

    describe 'with a one-line selection', ->
      it 'quotes the selection', ->
        stubSelection('This text has been selected.')

        @shortcut.replyWithSelectedText()

        expect($(@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(
          """
          Selected line one.

          Selected line two.
          Selected line three.

          """
        )

        @shortcut.replyWithSelectedText()

        expect($(@selector).val()).
          toBe(
            """
            > Selected line one.
            > Selected line two.
            > Selected line three.


            """
          )