utils_spec.js 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
import Editor from '~/editor/editor_lite';
import * as utils from '~/blob/utils';

const mockCreateMonacoInstance = jest.fn();
jest.mock('~/editor/editor_lite', () => {
  return jest.fn().mockImplementation(() => {
    return { createInstance: mockCreateMonacoInstance };
  });
});

const mockCreateAceInstance = jest.fn();
global.ace = {
  edit: mockCreateAceInstance,
};

describe('Blob utilities', () => {
  beforeEach(() => {
    Editor.mockClear();
  });

  describe('initEditorLite', () => {
    let editorEl;
    const blobPath = 'foo.txt';
    const blobContent = 'Foo bar';

    beforeEach(() => {
      setFixtures('<div id="editor"></div>');
      editorEl = document.getElementById('editor');
    });

    describe('Monaco editor', () => {
      let origProp;

      beforeEach(() => {
        origProp = window.gon;
        window.gon = {
          features: {
            monacoSnippets: true,
          },
        };
      });

      afterEach(() => {
        window.gon = origProp;
      });

      it('initializes the Editor Lite', () => {
        utils.initEditorLite({ el: editorEl });
        expect(Editor).toHaveBeenCalled();
      });

      it('creates the instance with the passed parameters', () => {
        utils.initEditorLite({ el: editorEl });
        expect(mockCreateMonacoInstance.mock.calls[0]).toEqual([
          {
            el: editorEl,
            blobPath: undefined,
            blobContent: undefined,
          },
        ]);

        utils.initEditorLite({ el: editorEl, blobPath, blobContent });
        expect(mockCreateMonacoInstance.mock.calls[1]).toEqual([
          {
            el: editorEl,
            blobPath,
            blobContent,
          },
        ]);
      });
    });
    describe('ACE editor', () => {
      let origProp;

      beforeEach(() => {
        origProp = window.gon;
        window.gon = {
          features: {
            monacoSnippets: false,
          },
        };
      });

      afterEach(() => {
        window.gon = origProp;
      });

      it('does not initialize the Editor Lite', () => {
        utils.initEditorLite({ el: editorEl });
        expect(Editor).not.toHaveBeenCalled();
        expect(mockCreateAceInstance).toHaveBeenCalledWith(editorEl);
      });
    });
  });
});