summaryrefslogtreecommitdiff
path: root/spec/frontend/blob/components/blob_edit_content_spec.js
blob: dbed086a55254af3bca553de4e16082961b2b6c1 (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
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import BlobEditContent from '~/blob/components/blob_edit_content.vue';
import * as utils from '~/blob/utils';

jest.mock('~/editor/editor_lite');

describe('Blob Header Editing', () => {
  let wrapper;
  const onDidChangeModelContent = jest.fn();
  const updateModelLanguage = jest.fn();
  const getValue = jest.fn();
  const value = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  const fileName = 'lorem.txt';
  const fileGlobalId = 'snippet_777';

  function createComponent(props = {}) {
    wrapper = shallowMount(BlobEditContent, {
      propsData: {
        value,
        fileName,
        fileGlobalId,
        ...props,
      },
    });
  }

  beforeEach(() => {
    jest.spyOn(utils, 'initEditorLite').mockImplementation(() => ({
      onDidChangeModelContent,
      updateModelLanguage,
      getValue,
      dispose: jest.fn(),
    }));

    createComponent();
  });

  afterEach(() => {
    wrapper.destroy();
  });

  const triggerChangeContent = val => {
    getValue.mockReturnValue(val);
    const [cb] = onDidChangeModelContent.mock.calls[0];

    cb();

    jest.runOnlyPendingTimers();
  };

  describe('rendering', () => {
    it('matches the snapshot', () => {
      expect(wrapper.element).toMatchSnapshot();
    });

    it('renders content', () => {
      expect(wrapper.text()).toContain(value);
    });
  });

  describe('functionality', () => {
    it('does not fail without content', () => {
      const spy = jest.spyOn(global.console, 'error');
      createComponent({ value: undefined });

      expect(spy).not.toHaveBeenCalled();
      expect(wrapper.find('#editor').exists()).toBe(true);
    });

    it('initialises Editor Lite', () => {
      const el = wrapper.find({ ref: 'editor' }).element;
      expect(utils.initEditorLite).toHaveBeenCalledWith({
        el,
        blobPath: fileName,
        blobGlobalId: fileGlobalId,
        blobContent: value,
      });
    });

    it('reacts to the changes in fileName', () => {
      const newFileName = 'ipsum.txt';

      wrapper.setProps({
        fileName: newFileName,
      });

      return nextTick().then(() => {
        expect(updateModelLanguage).toHaveBeenCalledWith(newFileName);
      });
    });

    it('registers callback with editor onChangeContent', () => {
      expect(onDidChangeModelContent).toHaveBeenCalledWith(expect.any(Function));
    });

    it('emits input event when the blob content is changed', () => {
      expect(wrapper.emitted().input).toBeUndefined();

      triggerChangeContent(value);

      expect(wrapper.emitted().input).toEqual([[value]]);
    });
  });
});