repo_editor_spec.js 7.0 KB
Newer Older
P
Phil Hughes 已提交
1
import Vue from 'vue';
2 3
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
P
Phil Hughes 已提交
4 5 6 7
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 已提交
8
import { createComponentWithStore } from '../../helpers/vue_mount_component_helper';
9
import setTimeoutPromise from '../../helpers/set_timeout_promise_helper';
P
Phil Hughes 已提交
10 11 12 13 14
import { file, resetStore } from '../helpers';

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

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

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

    f.active = true;
    f.tempFile = true;
    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);

41
    Editor.editorInstance.dispose();
P
Phil Hughes 已提交
42 43
  });

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

      done();
    });
  });

T
Tim Zallmann 已提交
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
  it('renders only an edit tab', done => {
    Vue.nextTick(() => {
      const tabs = vm.$el.querySelectorAll('.ide-mode-tabs .nav-links li');
      expect(tabs.length).toBe(1);
      expect(tabs[0].textContent.trim()).toBe('Edit');

      done();
    });
  });

  describe('when file is markdown', () => {
    beforeEach(done => {
      vm.file.previewMode = {
        id: 'markdown',
        previewTitle: 'Preview Markdown',
      };

      vm.$nextTick(done);
    });

    it('renders an Edit and a Preview Tab', done => {
      Vue.nextTick(() => {
        const tabs = vm.$el.querySelectorAll('.ide-mode-tabs .nav-links li');
        expect(tabs.length).toBe(2);
        expect(tabs[0].textContent.trim()).toBe('Edit');
        expect(tabs[1].textContent.trim()).toBe('Preview Markdown');

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

  describe('when file is markdown and viewer mode is review', () => {
85 86
    let mock;

T
Tim Zallmann 已提交
87
    beforeEach(done => {
88 89 90
      mock = new MockAdapter(axios);

      vm.file.projectId = 'namespace/project';
T
Tim Zallmann 已提交
91 92 93 94
      vm.file.previewMode = {
        id: 'markdown',
        previewTitle: 'Preview Markdown',
      };
95
      vm.file.content = 'testing 123';
T
Tim Zallmann 已提交
96 97
      vm.$store.state.viewer = 'diff';

98
      mock.onPost(/(.*)\/preview_markdown/).reply(200, {
99 100 101
        body: '<p>testing 123</p>',
      });

T
Tim Zallmann 已提交
102 103 104
      vm.$nextTick(done);
    });

105 106 107 108
    afterEach(() => {
      mock.restore();
    });

T
Tim Zallmann 已提交
109 110 111 112 113 114 115 116 117 118
    it('renders an Edit and a Preview Tab', done => {
      Vue.nextTick(() => {
        const tabs = vm.$el.querySelectorAll('.ide-mode-tabs .nav-links li');
        expect(tabs.length).toBe(2);
        expect(tabs[0].textContent.trim()).toBe('Review');
        expect(tabs[1].textContent.trim()).toBe('Preview Markdown');

        done();
      });
    });
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    it('renders markdown for tempFile', done => {
      vm.file.tempFile = true;
      vm.file.path = `${vm.file.path}.md`;
      vm.$store.state.entries[vm.file.path] = vm.file;

      vm
        .$nextTick()
        .then(() => {
          vm.$el.querySelectorAll('.ide-mode-tabs .nav-links a')[1].click();
        })
        .then(setTimeoutPromise)
        .then(() => {
          expect(vm.$el.querySelector('.preview-container').innerHTML).toContain(
            '<p>testing 123</p>',
          );
        })
        .then(done)
        .catch(done.fail);
    });
T
Tim Zallmann 已提交
139 140
  });

P
Phil Hughes 已提交
141
  describe('when open file is binary and not raw', () => {
P
Phil Hughes 已提交
142
    beforeEach(done => {
P
Phil Hughes 已提交
143 144 145 146 147 148 149 150 151 152 153
      vm.file.binary = true;

      vm.$nextTick(done);
    });

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

  describe('createEditorInstance', () => {
P
Phil Hughes 已提交
154
    it('calls createInstance when viewer is editor', done => {
P
Phil Hughes 已提交
155 156 157 158 159 160 161 162 163 164 165
      spyOn(vm.editor, 'createInstance');

      vm.createEditorInstance();

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

        done();
      });
    });

P
Phil Hughes 已提交
166
    it('calls createDiffInstance when viewer is diff', done => {
P
Phil Hughes 已提交
167 168 169 170 171 172 173 174 175 176 177 178
      vm.$store.state.viewer = 'diff';

      spyOn(vm.editor, 'createDiffInstance');

      vm.createEditorInstance();

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

        done();
      });
    });
179 180 181 182 183 184 185 186 187 188 189 190 191 192

    it('calls createDiffInstance when viewer is a merge request diff', done => {
      vm.$store.state.viewer = 'mrdiff';

      spyOn(vm.editor, 'createDiffInstance');

      vm.createEditorInstance();

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

        done();
      });
    });
P
Phil Hughes 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  });

  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 已提交
228
    it('updates state when model content changed', done => {
P
Phil Hughes 已提交
229 230 231 232 233 234 235 236 237
      vm.model.setValue('testing 123');

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

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

P
Phil Hughes 已提交
239 240 241 242 243
  describe('editor updateDimensions', () => {
    beforeEach(() => {
      spyOn(vm.editor, 'updateDimensions').and.callThrough();
      spyOn(vm.editor, 'updateDiffView');
    });
244

P
Phil Hughes 已提交
245 246
    it('calls updateDimensions when rightPanelCollapsed is changed', done => {
      vm.$store.state.rightPanelCollapsed = true;
247

P
Phil Hughes 已提交
248 249 250
      vm.$nextTick(() => {
        expect(vm.editor.updateDimensions).toHaveBeenCalled();
        expect(vm.editor.updateDiffView).toHaveBeenCalled();
251

P
Phil Hughes 已提交
252
        done();
253 254 255
      });
    });

P
Phil Hughes 已提交
256 257
    it('calls updateDimensions when panelResizing is false', done => {
      vm.$store.state.panelResizing = true;
258

P
Phil Hughes 已提交
259 260 261 262 263 264 265 266 267 268 269 270
      vm
        .$nextTick()
        .then(() => {
          vm.$store.state.panelResizing = false;
        })
        .then(vm.$nextTick)
        .then(() => {
          expect(vm.editor.updateDimensions).toHaveBeenCalled();
          expect(vm.editor.updateDiffView).toHaveBeenCalled();
        })
        .then(done)
        .catch(done.fail);
271 272
    });

P
Phil Hughes 已提交
273 274
    it('does not call updateDimensions when panelResizing is true', done => {
      vm.$store.state.panelResizing = true;
275

P
Phil Hughes 已提交
276 277 278
      vm.$nextTick(() => {
        expect(vm.editor.updateDimensions).not.toHaveBeenCalled();
        expect(vm.editor.updateDiffView).not.toHaveBeenCalled();
279

P
Phil Hughes 已提交
280 281
        done();
      });
282 283
    });
  });
P
Phil Hughes 已提交
284
});