2015-06-15 17:53:39 -04:00
|
|
|
#= require line_highlighter
|
2015-06-06 23:15:32 -04:00
|
|
|
|
2015-06-15 17:53:39 -04:00
|
|
|
describe 'LineHighlighter', ->
|
|
|
|
fixture.preload('line_highlighter.html')
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
clickLine = (number, eventData = {}) ->
|
|
|
|
if $.isEmptyObject(eventData)
|
|
|
|
$("#L#{number}").mousedown().click()
|
|
|
|
else
|
|
|
|
e = $.Event 'mousedown', eventData
|
|
|
|
$("#L#{number}").trigger(e).click()
|
|
|
|
|
|
|
|
beforeEach ->
|
2015-06-15 17:53:39 -04:00
|
|
|
fixture.load('line_highlighter.html')
|
|
|
|
@class = new LineHighlighter()
|
2015-06-19 02:01:53 -04:00
|
|
|
@css = @class.highlightClass
|
2015-06-06 23:15:32 -04:00
|
|
|
@spies = {
|
|
|
|
__setLocationHash__: spyOn(@class, '__setLocationHash__').and.callFake ->
|
|
|
|
}
|
|
|
|
|
|
|
|
describe 'behavior', ->
|
|
|
|
it 'highlights one line given in the URL hash', ->
|
2015-06-15 17:53:39 -04:00
|
|
|
new LineHighlighter('#L13')
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($('#LC13')).toHaveClass(@css)
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
it 'highlights a range of lines given in the URL hash', ->
|
2015-06-15 17:53:39 -04:00
|
|
|
new LineHighlighter('#L5-25')
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($(".#{@css}").length).toBe(21)
|
|
|
|
expect($("#LC#{line}")).toHaveClass(@css) for line in [5..25]
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
it 'scrolls to the first highlighted line on initial load', ->
|
|
|
|
spy = spyOn($, 'scrollTo')
|
2015-06-15 17:53:39 -04:00
|
|
|
new LineHighlighter('#L5-25')
|
2015-06-06 23:15:32 -04:00
|
|
|
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', ->
|
2015-06-15 17:53:39 -04:00
|
|
|
func = -> new LineHighlighter('#tree-content-holder')
|
2015-06-06 23:15:32 -04:00
|
|
|
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)
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($('#LC13')).toHaveClass(@css)
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
it 'unhighlights previously highlighted lines', ->
|
|
|
|
clickLine(13)
|
|
|
|
clickLine(20)
|
|
|
|
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($('#LC13')).not.toHaveClass(@css)
|
|
|
|
expect($('#LC20')).toHaveClass(@css)
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
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)
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($('#LC13')).toHaveClass(@css)
|
|
|
|
expect($(".#{@css}").length).toBe(1)
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
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)
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($(".#{@css}").length).toBe(6)
|
|
|
|
expect($("#LC#{line}")).toHaveClass(@css) for line in [15..20]
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
it 'uses existing line as first line when target is greater', ->
|
|
|
|
clickLine(5)
|
|
|
|
clickLine(10, shiftKey: true)
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($(".#{@css}").length).toBe(6)
|
|
|
|
expect($("#LC#{line}")).toHaveClass(@css) for line in [5..10]
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
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)
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($(".#{@css}").length).toBe(6)
|
|
|
|
expect($("#LC#{line}")).toHaveClass(@css) for line in [5..10]
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
it 'uses target as last line when it is greater than existing first line', ->
|
|
|
|
clickLine(15, shiftKey: true)
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($(".#{@css}").length).toBe(6)
|
|
|
|
expect($("#LC#{line}")).toHaveClass(@css) for line in [10..15]
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
describe '#hashToRange', ->
|
|
|
|
beforeEach ->
|
|
|
|
@subject = @class.hashToRange
|
|
|
|
|
|
|
|
it 'extracts a single line number from the hash', ->
|
2015-06-19 02:01:53 -04:00
|
|
|
expect(@subject('#L5')).toEqual([5, null])
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
it 'extracts a range of line numbers from the hash', ->
|
|
|
|
expect(@subject('#L5-15')).toEqual([5, 15])
|
|
|
|
|
2015-06-19 02:01:53 -04:00
|
|
|
it 'returns [null, null] when the hash is not a line number', ->
|
|
|
|
expect(@subject('#foo')).toEqual([null, null])
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
describe '#highlightLine', ->
|
|
|
|
beforeEach ->
|
|
|
|
@subject = @class.highlightLine
|
|
|
|
|
|
|
|
it 'highlights the specified line', ->
|
|
|
|
@subject(13)
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($('#LC13')).toHaveClass(@css)
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
it 'accepts a String-based number', ->
|
|
|
|
@subject('13')
|
2015-06-19 02:01:53 -04:00
|
|
|
expect($('#LC13')).toHaveClass(@css)
|
2015-06-06 23:15:32 -04:00
|
|
|
|
|
|
|
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')
|