Welcome to mirror list, hosted at ThFree Co, Russian Federation.

editor_lite_spec.js « editor « javascripts « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 154daccf82d89edafbe93d0867a099efed22a8f4 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { editor as monacoEditor, Uri } from 'monaco-editor';
import Editor from '~/editor/editor_lite';

describe('Base editor', () => {
  let editorEl;
  let editor;
  const blobContent = 'Foo Bar';
  const blobPath = 'test.md';
  const uri = new Uri('gitlab', false, blobPath);
  const fakeModel = { foo: 'bar' };

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

  afterEach(() => {
    editor.dispose();
    editorEl.remove();
  });

  it('initializes Editor with basic properties', () => {
    expect(editor).toBeDefined();
    expect(editor.editorEl).toBe(null);
    expect(editor.blobContent).toEqual('');
    expect(editor.blobPath).toEqual('');
  });

  it('removes `editor-loading` data attribute from the target DOM element', () => {
    editor.createInstance({ el: editorEl });

    expect(editorEl.dataset.editorLoading).toBeUndefined();
  });

  describe('instance of the Editor', () => {
    let modelSpy;
    let instanceSpy;
    let setModel;
    let dispose;

    beforeEach(() => {
      setModel = jasmine.createSpy();
      dispose = jasmine.createSpy();
      modelSpy = spyOn(monacoEditor, 'createModel').and.returnValue(fakeModel);
      instanceSpy = spyOn(monacoEditor, 'create').and.returnValue({
        setModel,
        dispose,
      });
    });

    it('does nothing if no dom element is supplied', () => {
      editor.createInstance();

      expect(editor.editorEl).toBe(null);
      expect(editor.blobContent).toEqual('');
      expect(editor.blobPath).toEqual('');

      expect(modelSpy).not.toHaveBeenCalled();
      expect(instanceSpy).not.toHaveBeenCalled();
      expect(setModel).not.toHaveBeenCalled();
    });

    it('creates model to be supplied to Monaco editor', () => {
      editor.createInstance({ el: editorEl, blobPath, blobContent });

      expect(modelSpy).toHaveBeenCalledWith(blobContent, undefined, uri);
      expect(setModel).toHaveBeenCalledWith(fakeModel);
    });

    it('initializes the instance on a supplied DOM node', () => {
      editor.createInstance({ el: editorEl });

      expect(editor.editorEl).not.toBe(null);
      expect(instanceSpy).toHaveBeenCalledWith(editorEl, jasmine.anything());
    });
  });

  describe('implementation', () => {
    beforeEach(() => {
      editor.createInstance({ el: editorEl, blobPath, blobContent });
    });

    afterEach(() => {
      editor.model.dispose();
    });

    it('correctly proxies value from the model', () => {
      expect(editor.getValue()).toEqual(blobContent);
    });

    it('is capable of changing the language of the model', () => {
      const blobRenamedPath = 'test.js';

      expect(editor.model.getLanguageIdentifier().language).toEqual('markdown');
      editor.updateModelLanguage(blobRenamedPath);

      expect(editor.model.getLanguageIdentifier().language).toEqual('javascript');
    });

    it('falls back to plaintext if there is no language associated with an extension', () => {
      const blobRenamedPath = 'test.myext';
      const spy = spyOn(console, 'error');

      editor.updateModelLanguage(blobRenamedPath);

      expect(spy).not.toHaveBeenCalled();
      expect(editor.model.getLanguageIdentifier().language).toEqual('plaintext');
    });
  });
});