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,6 +60,10 @@ describe('Base editor', () => { ...@@ -57,6 +60,10 @@ describe('Base editor', () => {
dispose = jest.fn(); dispose = jest.fn();
use = jest.fn(); use = jest.fn();
modelsStorage = new Map(); modelsStorage = new Map();
});
describe('instance of the Code Editor', () => {
beforeEach(() => {
modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel); modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel);
instanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({ instanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({
setModel, setModel,
...@@ -81,19 +88,28 @@ describe('Base editor', () => { ...@@ -81,19 +88,28 @@ describe('Base editor', () => {
}); });
it('creates model to be supplied to Monaco editor', () => { it('creates model to be supplied to Monaco editor', () => {
editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId: '' }); editor.createInstance(defaultArguments);
expect(modelSpy).toHaveBeenCalledWith(blobContent, undefined, createUri(blobPath)); expect(modelSpy).toHaveBeenCalledWith(
blobContent,
undefined,
createUri(blobGlobalId, blobPath),
);
expect(setModel).toHaveBeenCalledWith(fakeModel); expect(setModel).toHaveBeenCalledWith(fakeModel);
}); });
it('does not create a model automatically if model is passed as `null`', () => {
editor.createInstance({ ...defaultArguments, model: null });
expect(modelSpy).not.toHaveBeenCalled();
expect(setModel).not.toHaveBeenCalled();
});
it('does not create a new model if a model for the path already exists', () => { it('does not create a new model if a model for the path already exists', () => {
modelSpy = jest modelSpy = jest
.spyOn(monacoEditor, 'createModel') .spyOn(monacoEditor, 'createModel')
.mockImplementation((content, lang, uri) => modelsStorage.set(uri.path, content)); .mockImplementation((content, lang, uri) => modelsStorage.set(uri.path, content));
const instanceOptions = { el: editorEl, blobPath, blobContent, blobGlobalId: '' }; const a = editor.createInstance(defaultArguments);
const a = editor.createInstance(instanceOptions); const b = editor.createInstance(defaultArguments);
const b = editor.createInstance(instanceOptions);
expect(a === b).toBe(false); expect(a === b).toBe(false);
expect(modelSpy).toHaveBeenCalledTimes(1); expect(modelSpy).toHaveBeenCalledTimes(1);
...@@ -107,7 +123,7 @@ describe('Base editor', () => { ...@@ -107,7 +123,7 @@ describe('Base editor', () => {
}); });
it('with blobGlobalId, creates model with id in uri', () => { it('with blobGlobalId, creates model with id in uri', () => {
editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId }); editor.createInstance(defaultArguments);
expect(modelSpy).toHaveBeenCalledWith( expect(modelSpy).toHaveBeenCalledWith(
blobContent, blobContent,
...@@ -124,11 +140,14 @@ describe('Base editor', () => { ...@@ -124,11 +140,14 @@ describe('Base editor', () => {
el: editorEl, el: editorEl,
...instanceOptions, ...instanceOptions,
}); });
expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.objectContaining(instanceOptions)); expect(instanceSpy).toHaveBeenCalledWith(
editorEl,
expect.objectContaining(instanceOptions),
);
}); });
it('disposes instance when the editor is disposed', () => { it('disposes instance when the editor is disposed', () => {
editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId }); editor.createInstance(defaultArguments);
expect(dispose).not.toHaveBeenCalled(); expect(dispose).not.toHaveBeenCalled();
...@@ -138,6 +157,55 @@ describe('Base editor', () => { ...@@ -138,6 +157,55 @@ describe('Base editor', () => {
}); });
}); });
describe('instance of the Diff Editor', () => {
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);
});
});
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,
}),
);
});
it('initializes the instance on a supplied DOM node', () => {
const wrongInstanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({}));
editor.createDiffInstance({ ...defaultArguments, blobOriginalContent });
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,
});
});
});
});
describe('multiple instances', () => { describe('multiple instances', () => {
let instanceSpy; let instanceSpy;
let inst1Args; let inst1Args;
......
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