repo_editor_spec.js 3.1 KB
Newer Older
P
Phil Hughes 已提交
1
import Vue from 'vue';
P
Phil Hughes 已提交
2 3 4 5
import store from '~/ide/stores';
import repoEditor from '~/ide/components/repo_editor.vue';
import monacoLoader from '~/ide/monaco_loader';
import Editor from '~/ide/lib/editor';
P
Phil Hughes 已提交
6 7 8 9 10 11
import { createComponentWithStore } from '../../helpers/vue_mount_component_helper';
import { file, resetStore } from '../helpers';

describe('RepoEditor', () => {
  let vm;

P
Phil Hughes 已提交
12
  beforeEach(done => {
P
Phil Hughes 已提交
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
    const f = file();
    const RepoEditor = Vue.extend(repoEditor);

    vm = createComponentWithStore(RepoEditor, store, {
      file: f,
    });

    f.active = true;
    f.tempFile = true;
    f.html = 'testing';
    vm.$store.state.openFiles.push(f);
    vm.$store.state.entries[f.path] = f;
    vm.monaco = true;

    vm.$mount();

    monacoLoader(['vs/editor/editor.main'], () => {
      setTimeout(done, 0);
    });
  });

  afterEach(() => {
    vm.$destroy();

    resetStore(vm.$store);

    Editor.editorInstance.modelManager.dispose();
  });

P
Phil Hughes 已提交
42
  it('renders an ide container', done => {
P
Phil Hughes 已提交
43 44 45 46 47 48 49 50
    Vue.nextTick(() => {
      expect(vm.shouldHideEditor).toBeFalsy();

      done();
    });
  });

  describe('when open file is binary and not raw', () => {
P
Phil Hughes 已提交
51
    beforeEach(done => {
P
Phil Hughes 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      vm.file.binary = true;

      vm.$nextTick(done);
    });

    it('does not render the IDE', () => {
      expect(vm.shouldHideEditor).toBeTruthy();
    });

    it('shows activeFile html', () => {
      expect(vm.$el.textContent).toContain('testing');
    });
  });

  describe('createEditorInstance', () => {
P
Phil Hughes 已提交
67
    it('calls createInstance when viewer is editor', done => {
P
Phil Hughes 已提交
68 69 70 71 72 73 74 75 76 77 78
      spyOn(vm.editor, 'createInstance');

      vm.createEditorInstance();

      vm.$nextTick(() => {
        expect(vm.editor.createInstance).toHaveBeenCalled();

        done();
      });
    });

P
Phil Hughes 已提交
79
    it('calls createDiffInstance when viewer is diff', done => {
P
Phil Hughes 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
      vm.$store.state.viewer = 'diff';

      spyOn(vm.editor, 'createDiffInstance');

      vm.createEditorInstance();

      vm.$nextTick(() => {
        expect(vm.editor.createDiffInstance).toHaveBeenCalled();

        done();
      });
    });
  });

  describe('setupEditor', () => {
    it('creates new model', () => {
      spyOn(vm.editor, 'createModel').and.callThrough();

      Editor.editorInstance.modelManager.dispose();

      vm.setupEditor();

      expect(vm.editor.createModel).toHaveBeenCalledWith(vm.file);
      expect(vm.model).not.toBeNull();
    });

    it('attaches model to editor', () => {
      spyOn(vm.editor, 'attachModel').and.callThrough();

      Editor.editorInstance.modelManager.dispose();

      vm.setupEditor();

      expect(vm.editor.attachModel).toHaveBeenCalledWith(vm.model);
    });

    it('adds callback methods', () => {
      spyOn(vm.editor, 'onPositionChange').and.callThrough();

      Editor.editorInstance.modelManager.dispose();

      vm.setupEditor();

      expect(vm.editor.onPositionChange).toHaveBeenCalled();
      expect(vm.model.events.size).toBe(1);
    });

P
Phil Hughes 已提交
127
    it('updates state when model content changed', done => {
P
Phil Hughes 已提交
128 129 130 131 132 133 134 135 136 137
      vm.model.setValue('testing 123');

      setTimeout(() => {
        expect(vm.file.content).toBe('testing 123');

        done();
      });
    });
  });
});