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

Tests coverage for Diff Editor

parent b278d8cb
......@@ -13,6 +13,8 @@ import {
describe('Base editor', () => {
let editorEl;
let editor;
let defaultArguments;
const blobOriginalContent = 'Foo Foo';
const blobContent = 'Foo Bar';
const blobPath = 'test.md';
const blobGlobalId = 'snippet_777';
......@@ -21,6 +23,7 @@ describe('Base editor', () => {
beforeEach(() => {
setFixtures('<div id="editor" data-editor-loading></div>');
editorEl = document.getElementById('editor');
defaultArguments = { el: editorEl, blobPath, blobContent, blobGlobalId };
editor = new EditorLite();
});
......@@ -42,7 +45,7 @@ describe('Base editor', () => {
expect(editorEl.dataset.editorLoading).toBeUndefined();
});
describe('instance of the Editor', () => {
describe('instance of the Editor Lite', () => {
let modelSpy;
let instanceSpy;
let use;
......@@ -57,6 +60,10 @@ describe('Base editor', () => {
dispose = jest.fn();
use = jest.fn();
modelsStorage = new Map();
});
describe('instance of the Code Editor', () => {
beforeEach(() => {
modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel);
instanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({
setModel,
......@@ -81,19 +88,28 @@ describe('Base 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);
});
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', () => {
modelSpy = jest
.spyOn(monacoEditor, 'createModel')
.mockImplementation((content, lang, uri) => modelsStorage.set(uri.path, content));
const instanceOptions = { el: editorEl, blobPath, blobContent, blobGlobalId: '' };
const a = editor.createInstance(instanceOptions);
const b = editor.createInstance(instanceOptions);
const a = editor.createInstance(defaultArguments);
const b = editor.createInstance(defaultArguments);
expect(a === b).toBe(false);
expect(modelSpy).toHaveBeenCalledTimes(1);
......@@ -107,7 +123,7 @@ describe('Base editor', () => {
});
it('with blobGlobalId, creates model with id in uri', () => {
editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId });
editor.createInstance(defaultArguments);
expect(modelSpy).toHaveBeenCalledWith(
blobContent,
......@@ -124,11 +140,14 @@ describe('Base editor', () => {
el: editorEl,
...instanceOptions,
});
expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.objectContaining(instanceOptions));
expect(instanceSpy).toHaveBeenCalledWith(
editorEl,
expect.objectContaining(instanceOptions),
);
});
it('disposes instance when the editor is disposed', () => {
editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId });
editor.createInstance(defaultArguments);
expect(dispose).not.toHaveBeenCalled();
......@@ -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', () => {
let instanceSpy;
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