Welcome to mirror list, hosted at ThFree Co, Russian Federation.

highlight_util_spec.js « source_viewer « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c84a39274f879a1712b3a36e3612383568795494 (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
import hljs from 'highlight.js/lib/core';
import { registerPlugins } from '~/vue_shared/components/source_viewer/plugins/index';
import { highlight } from '~/vue_shared/components/source_viewer/workers/highlight_utils';
import { LINES_PER_CHUNK, NEWLINE } from '~/vue_shared/components/source_viewer/constants';

jest.mock('highlight.js/lib/core', () => ({
  highlight: jest.fn().mockReturnValue({ value: 'highlighted content' }),
  registerLanguage: jest.fn(),
  getLanguage: jest.fn(),
}));

jest.mock('~/vue_shared/components/source_viewer/plugins/index', () => ({
  registerPlugins: jest.fn(),
}));

const fileType = 'text';
const rawContent = 'function test() { return true }; \n // newline';
const highlightedContent = 'highlighted content';
const language = 'json';

describe('Highlight utility', () => {
  beforeEach(() => highlight(fileType, rawContent, language));

  it('registers the language', () => {
    expect(hljs.registerLanguage).toHaveBeenCalledWith(language, expect.any(Function));
  });

  it('registers the plugins', () => {
    expect(registerPlugins).toHaveBeenCalled();
  });

  describe('sub-languages', () => {
    const languageDefinition = {
      subLanguage: 'xml',
      contains: [{ subLanguage: 'javascript' }, { subLanguage: 'typescript' }],
    };

    beforeEach(async () => {
      jest.spyOn(hljs, 'getLanguage').mockReturnValue(languageDefinition);
      await highlight(fileType, rawContent, language);
    });

    it('registers the primary sub-language', () => {
      expect(hljs.registerLanguage).toHaveBeenCalledWith(
        languageDefinition.subLanguage,
        expect.any(Function),
      );
    });

    it.each(languageDefinition.contains)(
      'registers the rest of the sub-languages',
      ({ subLanguage }) => {
        expect(hljs.registerLanguage).toHaveBeenCalledWith(subLanguage, expect.any(Function));
      },
    );
  });

  it('highlights the content', () => {
    expect(hljs.highlight).toHaveBeenCalledWith(rawContent, { language });
  });

  it('splits the content into chunks', async () => {
    const contentArray = Array.from({ length: 140 }, () => 'newline'); // simulate 140 lines of code

    const chunks = [
      {
        language,
        highlightedContent,
        rawContent: contentArray.slice(0, 70).join(NEWLINE), // first 70 lines
        startingFrom: 0,
        totalLines: LINES_PER_CHUNK,
      },
      {
        language,
        highlightedContent: '',
        rawContent: contentArray.slice(70, 140).join(NEWLINE), // last 70 lines
        startingFrom: 70,
        totalLines: LINES_PER_CHUNK,
      },
    ];

    expect(await highlight(fileType, contentArray.join(NEWLINE), language)).toEqual(
      expect.arrayContaining(chunks),
    );
  });
});

describe('unsupported languages', () => {
  const unsupportedLanguage = 'some_unsupported_language';

  beforeEach(() => highlight(fileType, rawContent, unsupportedLanguage));

  it('does not register plugins', () => {
    expect(registerPlugins).not.toHaveBeenCalled();
  });

  it('does not attempt to highlight the content', () => {
    expect(hljs.highlight).not.toHaveBeenCalled();
  });

  it('does not return a result', async () => {
    expect(await highlight(fileType, rawContent, unsupportedLanguage)).toBe(undefined);
  });
});