summaryrefslogtreecommitdiff
path: root/spec/javascripts/syntax_highlight_spec.js
blob: 5438368ccbed4ce313124c7237d9673924c58304 (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
/* eslint-disable no-var, no-return-assign */

import $ from 'jquery';
import syntaxHighlight from '~/syntax_highlight';

describe('Syntax Highlighter', function() {
  var stubUserColorScheme;
  stubUserColorScheme = function(value) {
    if (window.gon == null) {
      window.gon = {};
    }
    return (window.gon.user_color_scheme = value);
  };
  describe('on a js-syntax-highlight element', function() {
    beforeEach(function() {
      return setFixtures('<div class="js-syntax-highlight"></div>');
    });

    it('applies syntax highlighting', function() {
      stubUserColorScheme('monokai');
      syntaxHighlight($('.js-syntax-highlight'));

      expect($('.js-syntax-highlight')).toHaveClass('monokai');
    });
  });

  describe('on a parent element', function() {
    beforeEach(function() {
      return setFixtures(
        '<div class="parent">\n  <div class="js-syntax-highlight"></div>\n  <div class="foo"></div>\n  <div class="js-syntax-highlight"></div>\n</div>',
      );
    });

    it('applies highlighting to all applicable children', function() {
      stubUserColorScheme('monokai');
      syntaxHighlight($('.parent'));

      expect($('.parent, .foo')).not.toHaveClass('monokai');
      expect($('.monokai').length).toBe(2);
    });

    it('prevents an infinite loop when no matches exist', function() {
      var highlight;
      setFixtures('<div></div>');
      highlight = function() {
        return syntaxHighlight($('div'));
      };

      expect(highlight).not.toThrow();
    });
  });
});