line_highlighter_spec.js.coffee 5.19 KB
Newer Older
1
#= require line_highlighter
2

3 4
describe 'LineHighlighter', ->
  fixture.preload('line_highlighter.html')
5 6 7 8 9 10 11 12 13

  clickLine = (number, eventData = {}) ->
    if $.isEmptyObject(eventData)
      $("#L#{number}").mousedown().click()
    else
      e = $.Event 'mousedown', eventData
      $("#L#{number}").trigger(e).click()

  beforeEach ->
14 15
    fixture.load('line_highlighter.html')
    @class = new LineHighlighter()
Robert Speicher's avatar
Robert Speicher committed
16
    @css   = @class.highlightClass
17 18 19 20 21 22
    @spies = {
      __setLocationHash__: spyOn(@class, '__setLocationHash__').and.callFake ->
    }

  describe 'behavior', ->
    it 'highlights one line given in the URL hash', ->
23
      new LineHighlighter('#L13')
Robert Speicher's avatar
Robert Speicher committed
24
      expect($('#LC13')).toHaveClass(@css)
25 26

    it 'highlights a range of lines given in the URL hash', ->
27
      new LineHighlighter('#L5-25')
Robert Speicher's avatar
Robert Speicher committed
28 29
      expect($(".#{@css}").length).toBe(21)
      expect($("#LC#{line}")).toHaveClass(@css) for line in [5..25]
30 31 32

    it 'scrolls to the first highlighted line on initial load', ->
      spy = spyOn($, 'scrollTo')
33
      new LineHighlighter('#L5-25')
34 35 36 37 38 39 40 41
      expect(spy).toHaveBeenCalledWith('#L5', jasmine.anything())

    it 'discards click events', ->
      spy = spyOnEvent('a[data-line-number]', 'click')
      clickLine(13)
      expect(spy).toHaveBeenPrevented()

    it 'handles garbage input from the hash', ->
42
      func = -> new LineHighlighter('#blob-content-holder')
43 44 45 46 47 48 49 50
      expect(func).not.toThrow()

  describe '#clickHandler', ->
    it 'discards the mousedown event', ->
      spy = spyOnEvent('a[data-line-number]', 'mousedown')
      clickLine(13)
      expect(spy).toHaveBeenPrevented()

51 52 53 54 55 56 57 58
    it 'handles clicking on a child icon element', ->
      spy = spyOn(@class, 'setHash').and.callThrough()

      $('#L13 i').mousedown().click()

      expect(spy).toHaveBeenCalledWith(13)
      expect($('#LC13')).toHaveClass(@css)

59 60 61
    describe 'without shiftKey', ->
      it 'highlights one line when clicked', ->
        clickLine(13)
Robert Speicher's avatar
Robert Speicher committed
62
        expect($('#LC13')).toHaveClass(@css)
63 64 65 66 67

      it 'unhighlights previously highlighted lines', ->
        clickLine(13)
        clickLine(20)

Robert Speicher's avatar
Robert Speicher committed
68 69
        expect($('#LC13')).not.toHaveClass(@css)
        expect($('#LC20')).toHaveClass(@css)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

      it 'sets the hash', ->
        spy = spyOn(@class, 'setHash').and.callThrough()
        clickLine(13)
        expect(spy).toHaveBeenCalledWith(13)

    describe 'with shiftKey', ->
      it 'sets the hash', ->
        spy = spyOn(@class, 'setHash').and.callThrough()
        clickLine(13)
        clickLine(20, shiftKey: true)
        expect(spy).toHaveBeenCalledWith(13)
        expect(spy).toHaveBeenCalledWith(13, 20)

      describe 'without existing highlight', ->
        it 'highlights the clicked line', ->
          clickLine(13, shiftKey: true)
Robert Speicher's avatar
Robert Speicher committed
87 88
          expect($('#LC13')).toHaveClass(@css)
          expect($(".#{@css}").length).toBe(1)
89 90 91 92 93 94 95 96 97 98

        it 'sets the hash', ->
          spy = spyOn(@class, 'setHash')
          clickLine(13, shiftKey: true)
          expect(spy).toHaveBeenCalledWith(13)

      describe 'with existing single-line highlight', ->
        it 'uses existing line as last line when target is lesser', ->
          clickLine(20)
          clickLine(15, shiftKey: true)
Robert Speicher's avatar
Robert Speicher committed
99 100
          expect($(".#{@css}").length).toBe(6)
          expect($("#LC#{line}")).toHaveClass(@css) for line in [15..20]
101 102 103 104

        it 'uses existing line as first line when target is greater', ->
          clickLine(5)
          clickLine(10, shiftKey: true)
Robert Speicher's avatar
Robert Speicher committed
105 106
          expect($(".#{@css}").length).toBe(6)
          expect($("#LC#{line}")).toHaveClass(@css) for line in [5..10]
107 108 109 110 111 112 113 114

      describe 'with existing multi-line highlight', ->
        beforeEach ->
          clickLine(10, shiftKey: true)
          clickLine(13, shiftKey: true)

        it 'uses target as first line when it is less than existing first line', ->
          clickLine(5, shiftKey: true)
Robert Speicher's avatar
Robert Speicher committed
115 116
          expect($(".#{@css}").length).toBe(6)
          expect($("#LC#{line}")).toHaveClass(@css) for line in [5..10]
117 118 119

        it 'uses target as last line when it is greater than existing first line', ->
          clickLine(15, shiftKey: true)
Robert Speicher's avatar
Robert Speicher committed
120 121
          expect($(".#{@css}").length).toBe(6)
          expect($("#LC#{line}")).toHaveClass(@css) for line in [10..15]
122 123 124 125 126 127

  describe '#hashToRange', ->
    beforeEach ->
      @subject = @class.hashToRange

    it 'extracts a single line number from the hash', ->
Robert Speicher's avatar
Robert Speicher committed
128
      expect(@subject('#L5')).toEqual([5, null])
129 130 131 132

    it 'extracts a range of line numbers from the hash', ->
      expect(@subject('#L5-15')).toEqual([5, 15])

Robert Speicher's avatar
Robert Speicher committed
133 134
    it 'returns [null, null] when the hash is not a line number', ->
      expect(@subject('#foo')).toEqual([null, null])
135 136 137 138 139 140 141

  describe '#highlightLine', ->
    beforeEach ->
      @subject = @class.highlightLine

    it 'highlights the specified line', ->
      @subject(13)
Robert Speicher's avatar
Robert Speicher committed
142
      expect($('#LC13')).toHaveClass(@css)
143 144 145

    it 'accepts a String-based number', ->
      @subject('13')
Robert Speicher's avatar
Robert Speicher committed
146
      expect($('#LC13')).toHaveClass(@css)
147 148 149 150 151 152 153 154 155 156 157 158

  describe '#setHash', ->
    beforeEach ->
      @subject = @class.setHash

    it 'sets the location hash for a single line', ->
      @subject(5)
      expect(@spies.__setLocationHash__).toHaveBeenCalledWith('#L5')

    it 'sets the location hash for a range', ->
      @subject(5, 15)
      expect(@spies.__setLocationHash__).toHaveBeenCalledWith('#L5-15')