line_highlighter_spec.js 7.4 KB
Newer Older
1
/* eslint-disable no-var, prefer-template, no-else-return, dot-notation, no-return-assign, no-new, no-underscore-dangle */
F
Fatih Acet 已提交
2

3
import $ from 'jquery';
P
Phil Hughes 已提交
4
import LineHighlighter from '~/line_highlighter';
F
Fatih Acet 已提交
5

M
Mike Greiling 已提交
6 7
describe('LineHighlighter', function() {
  var clickLine;
8
  preloadFixtures('static/line_highlighter.html.raw');
M
Mike Greiling 已提交
9 10 11 12 13 14 15 16 17
  clickLine = function(number, eventData = {}) {
    if ($.isEmptyObject(eventData)) {
      return $('#L' + number).click();
    } else {
      const e = $.Event('click', eventData);
      return $('#L' + number).trigger(e);
    }
  };
  beforeEach(function() {
18
    loadFixtures('static/line_highlighter.html.raw');
M
Mike Greiling 已提交
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
    this['class'] = new LineHighlighter();
    this.css = this['class'].highlightLineClass;
    return (this.spies = {
      __setLocationHash__: spyOn(this['class'], '__setLocationHash__').and.callFake(function() {}),
    });
  });

  describe('behavior', function() {
    it('highlights one line given in the URL hash', function() {
      new LineHighlighter({ hash: '#L13' });

      expect($('#LC13')).toHaveClass(this.css);
    });

    it('highlights one line given in the URL hash with given CSS class name', function() {
      const hiliter = new LineHighlighter({ hash: '#L13', highlightLineClass: 'hilite' });

      expect(hiliter.highlightLineClass).toBe('hilite');
      expect($('#LC13')).toHaveClass('hilite');
      expect($('#LC13')).not.toHaveClass('hll');
    });

    it('highlights a range of lines given in the URL hash', function() {
      var line;
      new LineHighlighter({ hash: '#L5-25' });

      expect($('.' + this.css).length).toBe(21);
      for (line = 5; line <= 25; line += 1) {
        expect($('#LC' + line)).toHaveClass(this.css);
F
Fatih Acet 已提交
48 49
      }
    });
50

M
Mike Greiling 已提交
51 52 53 54
    it('scrolls to the first highlighted line on initial load', function() {
      var spy;
      spy = spyOn($, 'scrollTo');
      new LineHighlighter({ hash: '#L5-25' });
55

M
Mike Greiling 已提交
56 57
      expect(spy).toHaveBeenCalledWith('#L5', jasmine.anything());
    });
58

M
Mike Greiling 已提交
59 60 61 62
    it('discards click events', function() {
      var spy;
      spy = spyOnEvent('a[data-line-number]', 'click');
      clickLine(13);
63

M
Mike Greiling 已提交
64 65
      expect(spy).toHaveBeenPrevented();
    });
66

M
Mike Greiling 已提交
67 68 69 70 71
    it('handles garbage input from the hash', function() {
      var func;
      func = function() {
        return new LineHighlighter({ fileHolderSelector: '#blob-content-holder' });
      };
72

M
Mike Greiling 已提交
73 74 75
      expect(func).not.toThrow();
    });
  });
76

M
Mike Greiling 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  describe('clickHandler', function() {
    it('handles clicking on a child icon element', function() {
      var spy;
      spy = spyOn(this['class'], 'setHash').and.callThrough();
      $('#L13 i')
        .mousedown()
        .click();

      expect(spy).toHaveBeenCalledWith(13);
      expect($('#LC13')).toHaveClass(this.css);
    });

    describe('without shiftKey', function() {
      it('highlights one line when clicked', function() {
        clickLine(13);
92

M
Mike Greiling 已提交
93
        expect($('#LC13')).toHaveClass(this.css);
F
Fatih Acet 已提交
94
      });
95

M
Mike Greiling 已提交
96
      it('unhighlights previously highlighted lines', function() {
F
Fatih Acet 已提交
97
        clickLine(13);
M
Mike Greiling 已提交
98
        clickLine(20);
99

M
Mike Greiling 已提交
100 101
        expect($('#LC13')).not.toHaveClass(this.css);
        expect($('#LC20')).toHaveClass(this.css);
F
Fatih Acet 已提交
102
      });
103

M
Mike Greiling 已提交
104 105 106 107
      it('sets the hash', function() {
        var spy;
        spy = spyOn(this['class'], 'setHash').and.callThrough();
        clickLine(13);
108

M
Mike Greiling 已提交
109
        expect(spy).toHaveBeenCalledWith(13);
F
Fatih Acet 已提交
110 111
      });
    });
112

M
Mike Greiling 已提交
113 114
    describe('with shiftKey', function() {
      it('sets the hash', function() {
F
Fatih Acet 已提交
115
        var spy;
M
Mike Greiling 已提交
116 117 118 119 120
        spy = spyOn(this['class'], 'setHash').and.callThrough();
        clickLine(13);
        clickLine(20, {
          shiftKey: true,
        });
121

F
Fatih Acet 已提交
122
        expect(spy).toHaveBeenCalledWith(13);
M
Mike Greiling 已提交
123
        expect(spy).toHaveBeenCalledWith(13, 20);
F
Fatih Acet 已提交
124
      });
125

M
Mike Greiling 已提交
126 127 128 129 130
      describe('without existing highlight', function() {
        it('highlights the clicked line', function() {
          clickLine(13, {
            shiftKey: true,
          });
131 132

          expect($('#LC13')).toHaveClass(this.css);
M
Mike Greiling 已提交
133
          expect($('.' + this.css).length).toBe(1);
F
Fatih Acet 已提交
134
        });
135 136

        it('sets the hash', function() {
F
Fatih Acet 已提交
137
          var spy;
M
Mike Greiling 已提交
138 139 140 141
          spy = spyOn(this['class'], 'setHash');
          clickLine(13, {
            shiftKey: true,
          });
142 143

          expect(spy).toHaveBeenCalledWith(13);
F
Fatih Acet 已提交
144 145
        });
      });
146

M
Mike Greiling 已提交
147 148 149 150 151 152
      describe('with existing single-line highlight', function() {
        it('uses existing line as last line when target is lesser', function() {
          var line;
          clickLine(20);
          clickLine(15, {
            shiftKey: true,
F
Fatih Acet 已提交
153
          });
154

M
Mike Greiling 已提交
155 156 157 158
          expect($('.' + this.css).length).toBe(6);
          for (line = 15; line <= 20; line += 1) {
            expect($('#LC' + line)).toHaveClass(this.css);
          }
F
Fatih Acet 已提交
159
        });
160

M
Mike Greiling 已提交
161 162 163 164 165
        it('uses existing line as first line when target is greater', function() {
          var line;
          clickLine(5);
          clickLine(10, {
            shiftKey: true,
F
Fatih Acet 已提交
166
          });
167

M
Mike Greiling 已提交
168 169 170 171
          expect($('.' + this.css).length).toBe(6);
          for (line = 5; line <= 10; line += 1) {
            expect($('#LC' + line)).toHaveClass(this.css);
          }
F
Fatih Acet 已提交
172
        });
M
Mike Greiling 已提交
173
      });
174

M
Mike Greiling 已提交
175 176 177 178
      describe('with existing multi-line highlight', function() {
        beforeEach(function() {
          clickLine(10, {
            shiftKey: true,
F
Fatih Acet 已提交
179
          });
M
Mike Greiling 已提交
180 181
          clickLine(13, {
            shiftKey: true,
F
Fatih Acet 已提交
182 183
          });
        });
184

M
Mike Greiling 已提交
185 186 187 188
        it('uses target as first line when it is less than existing first line', function() {
          var line;
          clickLine(5, {
            shiftKey: true,
F
Fatih Acet 已提交
189
          });
190

M
Mike Greiling 已提交
191 192 193 194 195
          expect($('.' + this.css).length).toBe(6);
          for (line = 5; line <= 10; line += 1) {
            expect($('#LC' + line)).toHaveClass(this.css);
          }
        });
196

M
Mike Greiling 已提交
197 198 199 200
        it('uses target as last line when it is greater than existing first line', function() {
          var line;
          clickLine(15, {
            shiftKey: true,
F
Fatih Acet 已提交
201
          });
202

M
Mike Greiling 已提交
203 204 205 206
          expect($('.' + this.css).length).toBe(6);
          for (line = 10; line <= 15; line += 1) {
            expect($('#LC' + line)).toHaveClass(this.css);
          }
F
Fatih Acet 已提交
207 208 209
        });
      });
    });
M
Mike Greiling 已提交
210
  });
211

M
Mike Greiling 已提交
212 213 214 215
  describe('hashToRange', function() {
    beforeEach(function() {
      this.subject = this['class'].hashToRange;
    });
216

M
Mike Greiling 已提交
217 218 219
    it('extracts a single line number from the hash', function() {
      expect(this.subject('#L5')).toEqual([5, null]);
    });
220

M
Mike Greiling 已提交
221 222 223
    it('extracts a range of line numbers from the hash', function() {
      expect(this.subject('#L5-15')).toEqual([5, 15]);
    });
224

M
Mike Greiling 已提交
225 226
    it('returns [null, null] when the hash is not a line number', function() {
      expect(this.subject('#foo')).toEqual([null, null]);
F
Fatih Acet 已提交
227
    });
M
Mike Greiling 已提交
228
  });
229

M
Mike Greiling 已提交
230 231 232 233
  describe('highlightLine', function() {
    beforeEach(function() {
      this.subject = this['class'].highlightLine;
    });
234

M
Mike Greiling 已提交
235 236
    it('highlights the specified line', function() {
      this.subject(13);
237

M
Mike Greiling 已提交
238 239
      expect($('#LC13')).toHaveClass(this.css);
    });
240

M
Mike Greiling 已提交
241 242
    it('accepts a String-based number', function() {
      this.subject('13');
243

M
Mike Greiling 已提交
244
      expect($('#LC13')).toHaveClass(this.css);
F
Fatih Acet 已提交
245
    });
M
Mike Greiling 已提交
246
  });
247

M
Mike Greiling 已提交
248 249 250 251
  describe('setHash', function() {
    beforeEach(function() {
      this.subject = this['class'].setHash;
    });
252

M
Mike Greiling 已提交
253 254
    it('sets the location hash for a single line', function() {
      this.subject(5);
255

M
Mike Greiling 已提交
256 257
      expect(this.spies.__setLocationHash__).toHaveBeenCalledWith('#L5');
    });
258

M
Mike Greiling 已提交
259 260
    it('sets the location hash for a range', function() {
      this.subject(5, 15);
261

M
Mike Greiling 已提交
262
      expect(this.spies.__setLocationHash__).toHaveBeenCalledWith('#L5-15');
F
Fatih Acet 已提交
263 264
    });
  });
M
Mike Greiling 已提交
265
});