summaryrefslogtreecommitdiff
path: root/spec/frontend/snippet/snippet_bundle_spec.js
blob: 12d20d5cd850c18e74c55b9b96d06ae73c78d99d (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
import Editor from '~/editor/editor_lite';
import { initEditor } from '~/snippet/snippet_bundle';
import { setHTMLFixture } from 'helpers/fixtures';

jest.mock('~/editor/editor_lite', () => jest.fn());

describe('Snippet editor', () => {
  describe('Monaco editor for Snippets', () => {
    let oldGon;
    let editorEl;
    let contentEl;
    let fileNameEl;
    let form;

    const mockName = 'foo.bar';
    const mockContent = 'Foo Bar';
    const updatedMockContent = 'New Foo Bar';

    const mockEditor = {
      createInstance: jest.fn(),
      updateModelLanguage: jest.fn(),
      getValue: jest.fn().mockReturnValueOnce(updatedMockContent),
    };
    Editor.mockImplementation(() => mockEditor);

    function setUpFixture(name, content) {
      setHTMLFixture(`
        <div class="snippet-form-holder">
          <form>
            <input class="js-snippet-file-name" type="text" value="${name}">
            <input class="snippet-file-content" type="hidden" value="${content}">
            <pre id="editor"></pre>
          </form>
        </div>
      `);
    }

    function bootstrap(name = '', content = '') {
      setUpFixture(name, content);
      editorEl = document.getElementById('editor');
      contentEl = document.querySelector('.snippet-file-content');
      fileNameEl = document.querySelector('.js-snippet-file-name');
      form = document.querySelector('.snippet-form-holder form');

      initEditor();
    }

    function createEvent(name) {
      return new Event(name, {
        view: window,
        bubbles: true,
        cancelable: true,
      });
    }

    beforeEach(() => {
      oldGon = window.gon;
      window.gon = { features: { monacoSnippets: true } };
      bootstrap(mockName, mockContent);
    });

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

    it('correctly initializes Editor', () => {
      expect(mockEditor.createInstance).toHaveBeenCalledWith({
        el: editorEl,
        blobPath: mockName,
        blobContent: mockContent,
      });
    });

    it('listens to file name changes and updates syntax highlighting of code', () => {
      expect(mockEditor.updateModelLanguage).not.toHaveBeenCalled();

      const event = createEvent('change');

      fileNameEl.value = updatedMockContent;
      fileNameEl.dispatchEvent(event);

      expect(mockEditor.updateModelLanguage).toHaveBeenCalledWith(updatedMockContent);
    });

    it('listens to form submit event and populates the hidden field with most recent version of the content', () => {
      expect(contentEl.value).toBe(mockContent);

      const event = createEvent('submit');

      form.dispatchEvent(event);
      expect(contentEl.value).toBe(updatedMockContent);
    });
  });
});