2021-08-12 11:09:58 -04:00
|
|
|
import { editor as monacoEditor } from 'monaco-editor';
|
2022-05-13 08:08:49 -04:00
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2021-08-12 11:09:58 -04:00
|
|
|
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(() => {
|
2022-05-13 08:08:49 -04:00
|
|
|
setHTMLFixture('<div id="foo"><div id="bar">Foo</div></div>');
|
2021-08-12 11:09:58 -04:00
|
|
|
el = document.getElementById('foo');
|
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2021-08-12 11:09:58 -04:00
|
|
|
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'}
|
2021-08-17 11:10:19 -04:00
|
|
|
${undefined} | ${'plaintext'}
|
2021-08-12 11:09:58 -04:00
|
|
|
`(
|
|
|
|
'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();
|
2022-05-13 08:08:49 -04:00
|
|
|
setHTMLFixture('<pre id="foo"></pre>');
|
2021-08-12 11:09:58 -04:00
|
|
|
el = document.getElementById('foo');
|
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2021-08-12 11:09:58 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|