Commit 8518a249 authored by Denys Mishunov's avatar Denys Mishunov

Tests coverage for Diff Editor

parent b278d8cb
...@@ -13,6 +13,8 @@ import { ...@@ -13,6 +13,8 @@ import {
describe('Base editor', () => { describe('Base editor', () => {
let editorEl; let editorEl;
let editor; let editor;
let defaultArguments;
const blobOriginalContent = 'Foo Foo';
const blobContent = 'Foo Bar'; const blobContent = 'Foo Bar';
const blobPath = 'test.md'; const blobPath = 'test.md';
const blobGlobalId = 'snippet_777'; const blobGlobalId = 'snippet_777';
...@@ -21,6 +23,7 @@ describe('Base editor', () => { ...@@ -21,6 +23,7 @@ describe('Base editor', () => {
beforeEach(() => { beforeEach(() => {
setFixtures('<div id="editor" data-editor-loading></div>'); setFixtures('<div id="editor" data-editor-loading></div>');
editorEl = document.getElementById('editor'); editorEl = document.getElementById('editor');
defaultArguments = { el: editorEl, blobPath, blobContent, blobGlobalId };
editor = new EditorLite(); editor = new EditorLite();
}); });
...@@ -42,7 +45,7 @@ describe('Base editor', () => { ...@@ -42,7 +45,7 @@ describe('Base editor', () => {
expect(editorEl.dataset.editorLoading).toBeUndefined(); expect(editorEl.dataset.editorLoading).toBeUndefined();
}); });
describe('instance of the Editor', () => { describe('instance of the Editor Lite', () => {
let modelSpy; let modelSpy;
let instanceSpy; let instanceSpy;
let use; let use;
...@@ -57,84 +60,149 @@ describe('Base editor', () => { ...@@ -57,84 +60,149 @@ describe('Base editor', () => {
dispose = jest.fn(); dispose = jest.fn();
use = jest.fn(); use = jest.fn();
modelsStorage = new Map(); modelsStorage = new Map();
modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel);
instanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({
setModel,
getModel,
dispose,
use,
onDidDispose: jest.fn(),
}));
jest.spyOn(monacoEditor, 'getModel').mockImplementation((uri) => {
return modelsStorage.get(uri.path);
});
}); });
it('throws an error if no dom element is supplied', () => { describe('instance of the Code Editor', () => {
expect(() => { beforeEach(() => {
editor.createInstance(); modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel);
}).toThrow(EDITOR_LITE_INSTANCE_ERROR_NO_EL); instanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({
setModel,
getModel,
dispose,
use,
onDidDispose: jest.fn(),
}));
jest.spyOn(monacoEditor, 'getModel').mockImplementation((uri) => {
return modelsStorage.get(uri.path);
});
});
expect(modelSpy).not.toHaveBeenCalled(); it('throws an error if no dom element is supplied', () => {
expect(instanceSpy).not.toHaveBeenCalled(); expect(() => {
expect(setModel).not.toHaveBeenCalled(); editor.createInstance();
}); }).toThrow(EDITOR_LITE_INSTANCE_ERROR_NO_EL);
it('creates model to be supplied to Monaco editor', () => { expect(modelSpy).not.toHaveBeenCalled();
editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId: '' }); expect(instanceSpy).not.toHaveBeenCalled();
expect(setModel).not.toHaveBeenCalled();
});
expect(modelSpy).toHaveBeenCalledWith(blobContent, undefined, createUri(blobPath)); it('creates model to be supplied to Monaco editor', () => {
expect(setModel).toHaveBeenCalledWith(fakeModel); editor.createInstance(defaultArguments);
});
it('does not create a new model if a model for the path already exists', () => { expect(modelSpy).toHaveBeenCalledWith(
modelSpy = jest blobContent,
.spyOn(monacoEditor, 'createModel') undefined,
.mockImplementation((content, lang, uri) => modelsStorage.set(uri.path, content)); createUri(blobGlobalId, blobPath),
const instanceOptions = { el: editorEl, blobPath, blobContent, blobGlobalId: '' }; );
const a = editor.createInstance(instanceOptions); expect(setModel).toHaveBeenCalledWith(fakeModel);
const b = editor.createInstance(instanceOptions); });
expect(a === b).toBe(false); it('does not create a model automatically if model is passed as `null`', () => {
expect(modelSpy).toHaveBeenCalledTimes(1); editor.createInstance({ ...defaultArguments, model: null });
}); expect(modelSpy).not.toHaveBeenCalled();
expect(setModel).not.toHaveBeenCalled();
});
it('initializes the instance on a supplied DOM node', () => { it('does not create a new model if a model for the path already exists', () => {
editor.createInstance({ el: editorEl }); modelSpy = jest
.spyOn(monacoEditor, 'createModel')
.mockImplementation((content, lang, uri) => modelsStorage.set(uri.path, content));
const a = editor.createInstance(defaultArguments);
const b = editor.createInstance(defaultArguments);
expect(editor.editorEl).not.toBe(null); expect(a === b).toBe(false);
expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.anything()); expect(modelSpy).toHaveBeenCalledTimes(1);
}); });
it('with blobGlobalId, creates model with id in uri', () => { it('initializes the instance on a supplied DOM node', () => {
editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId }); editor.createInstance({ el: editorEl });
expect(modelSpy).toHaveBeenCalledWith( expect(editor.editorEl).not.toBe(null);
blobContent, expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.anything());
undefined, });
createUri(blobGlobalId, blobPath),
);
});
it('initializes instance with passed properties', () => { it('with blobGlobalId, creates model with id in uri', () => {
const instanceOptions = { editor.createInstance(defaultArguments);
foo: 'bar',
}; expect(modelSpy).toHaveBeenCalledWith(
editor.createInstance({ blobContent,
el: editorEl, undefined,
...instanceOptions, createUri(blobGlobalId, blobPath),
);
});
it('initializes instance with passed properties', () => {
const instanceOptions = {
foo: 'bar',
};
editor.createInstance({
el: editorEl,
...instanceOptions,
});
expect(instanceSpy).toHaveBeenCalledWith(
editorEl,
expect.objectContaining(instanceOptions),
);
});
it('disposes instance when the editor is disposed', () => {
editor.createInstance(defaultArguments);
expect(dispose).not.toHaveBeenCalled();
editor.dispose();
expect(dispose).toHaveBeenCalled();
}); });
expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.objectContaining(instanceOptions));
}); });
it('disposes instance when the editor is disposed', () => { describe('instance of the Diff Editor', () => {
editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId }); beforeEach(() => {
modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel);
instanceSpy = jest.spyOn(monacoEditor, 'createDiffEditor').mockImplementation(() => ({
setModel,
getModel,
dispose,
use,
onDidDispose: jest.fn(),
}));
jest.spyOn(monacoEditor, 'getModel').mockImplementation((uri) => {
return modelsStorage.get(uri.path);
});
});
expect(dispose).not.toHaveBeenCalled(); it('Diff Editor goes through the normal path of Code Editor just with the flag ON', () => {
const spy = jest.spyOn(editor, 'createInstance').mockImplementation(() => {});
editor.createDiffInstance();
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
diff: true,
}),
);
});
editor.dispose(); it('initializes the instance on a supplied DOM node', () => {
const wrongInstanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({}));
editor.createDiffInstance({ ...defaultArguments, blobOriginalContent });
expect(dispose).toHaveBeenCalled(); expect(editor.editorEl).not.toBe(null);
expect(wrongInstanceSpy).not.toHaveBeenCalled();
expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.anything());
});
it('creates correct model for the Diff Editor', () => {
editor.createDiffInstance({ ...defaultArguments, blobOriginalContent });
const uri = createUri(blobGlobalId, blobPath);
expect(modelSpy).toHaveBeenCalledTimes(2);
expect(modelSpy.mock.calls[0]).toEqual([blobContent, undefined, uri]);
expect(modelSpy.mock.calls[1]).toEqual([blobOriginalContent, undefined, uri]);
expect(setModel).toHaveBeenCalledWith({
original: expect.anything(),
modified: fakeModel,
});
});
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment