summaryrefslogtreecommitdiff
path: root/spec/frontend/syntax_highlight_spec.js
blob: a573c37ca446682f7626a7484f19d8309a255f3b (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
import $ from 'jquery';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import syntaxHighlight from '~/syntax_highlight';

describe('Syntax Highlighter', () => {
  const stubUserColorScheme = (value) => {
    window.gon.user_color_scheme = value;
  };

  // We have to bind `document.querySelectorAll` to `document` to not mess up the fn's context
  describe.each`
    desc                | fn
    ${'jquery'}         | ${$}
    ${'vanilla all'}    | ${document.querySelectorAll.bind(document)}
    ${'vanilla single'} | ${document.querySelector.bind(document)}
  `('highlight using $desc syntax', ({ fn }) => {
    describe('on a js-syntax-highlight element', () => {
      beforeEach(() => {
        setHTMLFixture('<div class="js-syntax-highlight"></div>');
      });

      afterEach(() => {
        resetHTMLFixture();
      });

      it('applies syntax highlighting', () => {
        stubUserColorScheme('monokai');
        syntaxHighlight(fn('.js-syntax-highlight'));

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

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

      afterEach(() => {
        resetHTMLFixture();
      });

      it('applies highlighting to all applicable children', () => {
        stubUserColorScheme('monokai');
        syntaxHighlight(fn('.parent'));

        expect(fn('.parent')).not.toHaveClass('monokai');
        expect(fn('.foo')).not.toHaveClass('monokai');

        expect(document.querySelectorAll('.monokai').length).toBe(2);
      });

      it('prevents an infinite loop when no matches exist', () => {
        setHTMLFixture('<div></div>');
        const highlight = () => syntaxHighlight(fn('div'));

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