summaryrefslogtreecommitdiff
path: root/spec/javascripts/line_highlighter_spec.js.coffee
blob: 14fa487ff7f42a959d0a0bb70a947c383282dbbe (plain)
1
2
3
4
5
6
7
8
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#= require line_highlighter

describe 'LineHighlighter', ->
  fixture.preload('line_highlighter.html')

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

  beforeEach ->
    fixture.load('line_highlighter.html')
    @class = new LineHighlighter()
    @css   = @class.highlightClass
    @spies = {
      __setLocationHash__: spyOn(@class, '__setLocationHash__').and.callFake ->
    }

  describe 'behavior', ->
    it 'highlights one line given in the URL hash', ->
      new LineHighlighter('#L13')
      expect($('#LC13')).toHaveClass(@css)

    it 'highlights a range of lines given in the URL hash', ->
      new LineHighlighter('#L5-25')
      expect($(".#{@css}").length).toBe(21)
      expect($("#LC#{line}")).toHaveClass(@css) for line in [5..25]

    it 'scrolls to the first highlighted line on initial load', ->
      spy = spyOn($, 'scrollTo')
      new LineHighlighter('#L5-25')
      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', ->
      func = -> new LineHighlighter('#tree-content-holder')
      expect(func).not.toThrow()

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

    describe 'without shiftKey', ->
      it 'highlights one line when clicked', ->
        clickLine(13)
        expect($('#LC13')).toHaveClass(@css)

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

        expect($('#LC13')).not.toHaveClass(@css)
        expect($('#LC20')).toHaveClass(@css)

      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)
          expect($('#LC13')).toHaveClass(@css)
          expect($(".#{@css}").length).toBe(1)

        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)
          expect($(".#{@css}").length).toBe(6)
          expect($("#LC#{line}")).toHaveClass(@css) for line in [15..20]

        it 'uses existing line as first line when target is greater', ->
          clickLine(5)
          clickLine(10, shiftKey: true)
          expect($(".#{@css}").length).toBe(6)
          expect($("#LC#{line}")).toHaveClass(@css) for line in [5..10]

      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)
          expect($(".#{@css}").length).toBe(6)
          expect($("#LC#{line}")).toHaveClass(@css) for line in [5..10]

        it 'uses target as last line when it is greater than existing first line', ->
          clickLine(15, shiftKey: true)
          expect($(".#{@css}").length).toBe(6)
          expect($("#LC#{line}")).toHaveClass(@css) for line in [10..15]

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

    it 'extracts a single line number from the hash', ->
      expect(@subject('#L5')).toEqual([5, null])

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

    it 'returns [null, null] when the hash is not a line number', ->
      expect(@subject('#foo')).toEqual([null, null])

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

    it 'highlights the specified line', ->
      @subject(13)
      expect($('#LC13')).toHaveClass(@css)

    it 'accepts a String-based number', ->
      @subject('13')
      expect($('#LC13')).toHaveClass(@css)

  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')