2020-12-17 12:09:57 +00:00
|
|
|
import { languages } from 'monaco-editor';
|
2021-01-13 15:10:40 +00:00
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2021-06-18 21:10:06 +00:00
|
|
|
import { CiSchemaExtension } from '~/editor/extensions/source_editor_ci_schema_ext';
|
2021-09-21 12:11:06 +00:00
|
|
|
import ciSchemaPath from '~/editor/schema/ci.json';
|
2021-06-18 21:10:06 +00:00
|
|
|
import SourceEditor from '~/editor/source_editor';
|
2020-12-17 12:09:57 +00:00
|
|
|
|
2021-06-14 18:10:28 +00:00
|
|
|
const mockRef = 'AABBCCDD';
|
|
|
|
|
2020-12-17 12:09:57 +00:00
|
|
|
describe('~/editor/editor_ci_config_ext', () => {
|
|
|
|
const defaultBlobPath = '.gitlab-ci.yml';
|
|
|
|
|
|
|
|
let editor;
|
|
|
|
let instance;
|
|
|
|
let editorEl;
|
2021-01-13 15:10:40 +00:00
|
|
|
let originalGitlabUrl;
|
2020-12-17 12:09:57 +00:00
|
|
|
|
|
|
|
const createMockEditor = ({ blobPath = defaultBlobPath } = {}) => {
|
|
|
|
setFixtures('<div id="editor"></div>');
|
|
|
|
editorEl = document.getElementById('editor');
|
2021-06-18 21:10:06 +00:00
|
|
|
editor = new SourceEditor();
|
2020-12-17 12:09:57 +00:00
|
|
|
instance = editor.createInstance({
|
|
|
|
el: editorEl,
|
|
|
|
blobPath,
|
|
|
|
blobContent: '',
|
|
|
|
});
|
|
|
|
instance.use(new CiSchemaExtension());
|
|
|
|
};
|
|
|
|
|
2021-01-13 15:10:40 +00:00
|
|
|
beforeAll(() => {
|
|
|
|
originalGitlabUrl = gon.gitlab_url;
|
|
|
|
gon.gitlab_url = TEST_HOST;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
gon.gitlab_url = originalGitlabUrl;
|
|
|
|
});
|
|
|
|
|
2020-12-17 12:09:57 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
createMockEditor();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
instance.dispose();
|
|
|
|
editorEl.remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('registerCiSchema', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(languages.yaml.yamlDefaults, 'setDiagnosticsOptions');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('register validations options with monaco for yaml language', () => {
|
|
|
|
const mockProjectNamespace = 'namespace1';
|
|
|
|
const mockProjectPath = 'project1';
|
|
|
|
|
|
|
|
const getConfiguredYmlSchema = () => {
|
|
|
|
return languages.yaml.yamlDefaults.setDiagnosticsOptions.mock.calls[0][0].schemas[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
it('with expected basic validation configuration', () => {
|
|
|
|
instance.registerCiSchema({
|
|
|
|
projectNamespace: mockProjectNamespace,
|
|
|
|
projectPath: mockProjectPath,
|
|
|
|
});
|
|
|
|
|
|
|
|
const expectedOptions = {
|
|
|
|
validate: true,
|
|
|
|
enableSchemaRequest: true,
|
|
|
|
hover: true,
|
|
|
|
completion: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(languages.yaml.yamlDefaults.setDiagnosticsOptions).toHaveBeenCalledTimes(1);
|
|
|
|
expect(languages.yaml.yamlDefaults.setDiagnosticsOptions).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining(expectedOptions),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with an schema uri that contains project and ref', () => {
|
|
|
|
instance.registerCiSchema({
|
|
|
|
projectNamespace: mockProjectNamespace,
|
|
|
|
projectPath: mockProjectPath,
|
|
|
|
ref: mockRef,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getConfiguredYmlSchema()).toEqual({
|
2021-09-21 12:11:06 +00:00
|
|
|
uri: `${TEST_HOST}${ciSchemaPath}`,
|
2020-12-17 12:09:57 +00:00
|
|
|
fileMatch: [defaultBlobPath],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with an alternative file name match', () => {
|
|
|
|
createMockEditor({ blobPath: 'dir1/dir2/another-ci-filename.yml' });
|
|
|
|
|
|
|
|
instance.registerCiSchema({
|
|
|
|
projectNamespace: mockProjectNamespace,
|
|
|
|
projectPath: mockProjectPath,
|
2021-06-14 18:10:28 +00:00
|
|
|
ref: mockRef,
|
2020-12-17 12:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(getConfiguredYmlSchema()).toEqual({
|
2021-09-21 12:11:06 +00:00
|
|
|
uri: `${TEST_HOST}${ciSchemaPath}`,
|
2020-12-17 12:09:57 +00:00
|
|
|
fileMatch: ['another-ci-filename.yml'],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|