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

utils_spec.js « editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e561cad1086a73a70661cbd082f630aedd21b33b (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
import { editor as monacoEditor } from 'monaco-editor';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import * as utils from '~/editor/utils';
import { DEFAULT_THEME } from '~/ide/lib/themes';

describe('Source Editor utils', () => {
  let el;

  const stubUserColorScheme = (value) => {
    if (window.gon == null) {
      window.gon = {};
    }
    window.gon.user_color_scheme = value;
  };

  describe('clearDomElement', () => {
    beforeEach(() => {
      setHTMLFixture('<div id="foo"><div id="bar">Foo</div></div>');
      el = document.getElementById('foo');
    });

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

    it('removes all child nodes from an element', () => {
      expect(el.children.length).toBe(1);
      utils.clearDomElement(el);
      expect(el.children.length).toBe(0);
    });
  });

  describe('setupEditorTheme', () => {
    beforeEach(() => {
      jest.spyOn(monacoEditor, 'defineTheme').mockImplementation();
      jest.spyOn(monacoEditor, 'setTheme').mockImplementation();
    });

    it.each`
      themeName            | expectedThemeName
      ${'solarized-light'} | ${'solarized-light'}
      ${DEFAULT_THEME}     | ${DEFAULT_THEME}
      ${'non-existent'}    | ${DEFAULT_THEME}
    `(
      'sets the $expectedThemeName theme when $themeName is set in the user preference',
      ({ themeName, expectedThemeName }) => {
        stubUserColorScheme(themeName);
        utils.setupEditorTheme();

        expect(monacoEditor.setTheme).toHaveBeenCalledWith(expectedThemeName);
      },
    );
  });

  describe('getBlobLanguage', () => {
    it.each`
      path           | expectedLanguage
      ${'foo.js'}    | ${'javascript'}
      ${'foo.js.rb'} | ${'ruby'}
      ${'foo.bar'}   | ${'plaintext'}
      ${undefined}   | ${'plaintext'}
    `(
      'sets the $expectedThemeName theme when $themeName is set in the user preference',
      ({ path, expectedLanguage }) => {
        const language = utils.getBlobLanguage(path);

        expect(language).toEqual(expectedLanguage);
      },
    );
  });

  describe('setupCodeSnipet', () => {
    beforeEach(() => {
      jest.spyOn(monacoEditor, 'colorizeElement').mockImplementation();
      jest.spyOn(monacoEditor, 'setTheme').mockImplementation();
      setHTMLFixture('<pre id="foo"></pre>');
      el = document.getElementById('foo');
    });

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

    it('colorizes the element and applies the preference theme', () => {
      expect(monacoEditor.colorizeElement).not.toHaveBeenCalled();
      expect(monacoEditor.setTheme).not.toHaveBeenCalled();

      utils.setupCodeSnippet(el);

      expect(monacoEditor.colorizeElement).toHaveBeenCalledWith(el);
      expect(monacoEditor.setTheme).toHaveBeenCalled();
    });
  });
});