repo_editor_spec.js 8.8 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 { activityBarViews } from '~/ide/constants';
P
Phil Hughes 已提交
9
import { createComponentWithStore } from '../../helpers/vue_mount_component_helper';
10
import setTimeoutPromise from '../../helpers/set_timeout_promise_helper';
P
Phil Hughes 已提交
11 12 13 14 15
import { file, resetStore } from '../helpers';

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

P
Phil Hughes 已提交
16
  beforeEach(done => {
P
Phil Hughes 已提交
17 18 19 20 21 22 23 24 25 26
    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);
27
    Vue.set(vm.$store.state.entries, f.path, f);
P
Phil Hughes 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
    vm.monaco = true;

    vm.$mount();

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

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

    resetStore(vm.$store);

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

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

      done();
    });
  });

T
Tim Zallmann 已提交
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
  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', () => {
86 87
    let mock;

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

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

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

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

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

T
Tim Zallmann 已提交
110 111 112 113 114 115 116 117 118 119
    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();
      });
    });
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    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 已提交
140 141
  });

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

      vm.$nextTick(done);
    });

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

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

      vm.createEditorInstance();

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

        done();
      });
    });

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

      spyOn(vm.editor, 'createDiffInstance');

      vm.createEditorInstance();

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

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

    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 已提交
194 195 196 197 198 199 200 201 202 203
  });

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

      Editor.editorInstance.modelManager.dispose();

      vm.setupEditor();

204
      expect(vm.editor.createModel).toHaveBeenCalledWith(vm.file, null);
P
Phil Hughes 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217
      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);
    });

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    it('attaches model to merge request editor', () => {
      vm.$store.state.viewer = 'mrdiff';
      vm.file.mrChange = true;
      spyOn(vm.editor, 'attachMergeRequestModel');

      Editor.editorInstance.modelManager.dispose();

      vm.setupEditor();

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

    it('does not attach model to merge request editor when not a MR change', () => {
      vm.$store.state.viewer = 'mrdiff';
      vm.file.mrChange = false;
      spyOn(vm.editor, 'attachMergeRequestModel');

      Editor.editorInstance.modelManager.dispose();

      vm.setupEditor();

      expect(vm.editor.attachMergeRequestModel).not.toHaveBeenCalledWith(vm.model);
    });

P
Phil Hughes 已提交
242 243 244 245 246 247 248 249
    it('adds callback methods', () => {
      spyOn(vm.editor, 'onPositionChange').and.callThrough();

      Editor.editorInstance.modelManager.dispose();

      vm.setupEditor();

      expect(vm.editor.onPositionChange).toHaveBeenCalled();
250
      expect(vm.model.events.size).toBe(2);
P
Phil Hughes 已提交
251 252
    });

P
Phil Hughes 已提交
253
    it('updates state when model content changed', done => {
P
Phil Hughes 已提交
254 255 256 257 258 259 260 261
      vm.model.setValue('testing 123');

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

        done();
      });
    });
262 263 264 265 266 267 268 269 270 271 272 273 274 275

    it('sets head model as staged file', () => {
      spyOn(vm.editor, 'createModel').and.callThrough();

      Editor.editorInstance.modelManager.dispose();

      vm.$store.state.stagedFiles.push({ ...vm.file, key: 'staged' });
      vm.file.staged = true;
      vm.file.key = `unstaged-${vm.file.key}`;

      vm.setupEditor();

      expect(vm.editor.createModel).toHaveBeenCalledWith(vm.file, vm.$store.state.stagedFiles[0]);
    });
P
Phil Hughes 已提交
276
  });
277

P
Phil Hughes 已提交
278 279 280 281 282
  describe('editor updateDimensions', () => {
    beforeEach(() => {
      spyOn(vm.editor, 'updateDimensions').and.callThrough();
      spyOn(vm.editor, 'updateDiffView');
    });
283

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

P
Phil Hughes 已提交
287 288 289
      vm.$nextTick(() => {
        expect(vm.editor.updateDimensions).toHaveBeenCalled();
        expect(vm.editor.updateDiffView).toHaveBeenCalled();
290

P
Phil Hughes 已提交
291
        done();
292 293 294
      });
    });

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

P
Phil Hughes 已提交
298 299 300 301 302 303 304 305 306 307 308 309
      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);
310 311
    });

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

P
Phil Hughes 已提交
315 316 317
      vm.$nextTick(() => {
        expect(vm.editor.updateDimensions).not.toHaveBeenCalled();
        expect(vm.editor.updateDiffView).not.toHaveBeenCalled();
318

P
Phil Hughes 已提交
319 320
        done();
      });
321 322
    });
  });
P
Phil Hughes 已提交
323 324 325

  describe('show tabs', () => {
    it('shows tabs in edit mode', () => {
P
Phil Hughes 已提交
326
      expect(vm.$el.querySelector('.nav-links')).not.toBe(null);
P
Phil Hughes 已提交
327 328 329 330 331 332
    });

    it('hides tabs in review mode', done => {
      vm.$store.state.currentActivityView = activityBarViews.review;

      vm.$nextTick(() => {
P
Phil Hughes 已提交
333
        expect(vm.$el.querySelector('.nav-links')).toBe(null);
P
Phil Hughes 已提交
334 335 336 337 338 339 340 341 342

        done();
      });
    });

    it('hides tabs in commit mode', done => {
      vm.$store.state.currentActivityView = activityBarViews.commit;

      vm.$nextTick(() => {
P
Phil Hughes 已提交
343
        expect(vm.$el.querySelector('.nav-links')).toBe(null);
P
Phil Hughes 已提交
344 345 346 347 348

        done();
      });
    });
  });
P
Phil Hughes 已提交
349
});