debian-mirror-gitlab/spec/frontend/editor/utils_spec.js

101 lines
3 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import { editor as monacoEditor } from 'monaco-editor';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2021-10-27 15:23:28 +05:30
import * as utils from '~/editor/utils';
2023-06-20 00:43:36 +05:30
import languages from '~/ide/lib/languages';
import { registerLanguages } from '~/ide/utils';
2021-10-27 15:23:28 +05:30
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(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture('<div id="foo"><div id="bar">Foo</div></div>');
2021-10-27 15:23:28 +05:30
el = document.getElementById('foo');
});
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2021-10-27 15:23:28 +05:30
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', () => {
2023-06-20 00:43:36 +05:30
beforeEach(() => {
registerLanguages(...languages);
});
2021-10-27 15:23:28 +05:30
it.each`
2023-06-20 00:43:36 +05:30
path | expectedLanguage
${'foo.js'} | ${'javascript'}
${'foo.js.rb'} | ${'ruby'}
${'foo.bar'} | ${'plaintext'}
${undefined} | ${'plaintext'}
${'foo/bar/foo.js'} | ${'javascript'}
${'CODEOWNERS'} | ${'codeowners'}
${'.gitlab/CODEOWNERS'} | ${'codeowners'}
2023-05-27 22:25:52 +05:30
`(`returns '$expectedLanguage' for '$path' path`, ({ path, expectedLanguage }) => {
const language = utils.getBlobLanguage(path);
2021-10-27 15:23:28 +05:30
2023-05-27 22:25:52 +05:30
expect(language).toEqual(expectedLanguage);
});
2021-10-27 15:23:28 +05:30
});
describe('setupCodeSnipet', () => {
beforeEach(() => {
jest.spyOn(monacoEditor, 'colorizeElement').mockImplementation();
jest.spyOn(monacoEditor, 'setTheme').mockImplementation();
2022-07-16 23:28:13 +05:30
setHTMLFixture('<pre id="foo"></pre>');
2021-10-27 15:23:28 +05:30
el = document.getElementById('foo');
});
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2021-10-27 15:23:28 +05:30
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();
});
});
});