summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/components/content_editor_spec.js
blob: 3d1ef03083de82e18dc2d89308dfe6685840dac3 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { GlLoadingIcon } from '@gitlab/ui';
import { EditorContent } from '@tiptap/vue-2';
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ContentEditor from '~/content_editor/components/content_editor.vue';
import ContentEditorError from '~/content_editor/components/content_editor_error.vue';
import ContentEditorProvider from '~/content_editor/components/content_editor_provider.vue';
import EditorStateObserver from '~/content_editor/components/editor_state_observer.vue';
import FormattingBubbleMenu from '~/content_editor/components/formatting_bubble_menu.vue';
import TopToolbar from '~/content_editor/components/top_toolbar.vue';
import {
  LOADING_CONTENT_EVENT,
  LOADING_SUCCESS_EVENT,
  LOADING_ERROR_EVENT,
} from '~/content_editor/constants';
import { emitEditorEvent } from '../test_utils';

jest.mock('~/emoji');

describe('ContentEditor', () => {
  let wrapper;
  let contentEditor;
  let renderMarkdown;
  const uploadsPath = '/uploads';

  const findEditorElement = () => wrapper.findByTestId('content-editor');
  const findEditorContent = () => wrapper.findComponent(EditorContent);
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findBubbleMenu = () => wrapper.findComponent(FormattingBubbleMenu);

  const createWrapper = (propsData = {}) => {
    renderMarkdown = jest.fn();

    wrapper = shallowMountExtended(ContentEditor, {
      propsData: {
        renderMarkdown,
        uploadsPath,
        ...propsData,
      },
      stubs: {
        EditorStateObserver,
        ContentEditorProvider,
      },
      listeners: {
        initialized(editor) {
          contentEditor = editor;
        },
      },
    });
  };

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

  it('triggers initialized event and provides contentEditor instance as event data', () => {
    createWrapper();

    expect(contentEditor).not.toBeFalsy();
  });

  it('renders EditorContent component and provides tiptapEditor instance', () => {
    createWrapper();

    const editorContent = findEditorContent();

    expect(editorContent.props().editor).toBe(contentEditor.tiptapEditor);
    expect(editorContent.classes()).toContain('md');
  });

  it('renders ContentEditorProvider component', () => {
    createWrapper();

    expect(wrapper.findComponent(ContentEditorProvider).exists()).toBe(true);
  });

  it('renders top toolbar component', () => {
    createWrapper();

    expect(wrapper.findComponent(TopToolbar).exists()).toBe(true);
  });

  it('adds is-focused class when focus event is emitted', async () => {
    createWrapper();

    await emitEditorEvent({ tiptapEditor: contentEditor.tiptapEditor, event: 'focus' });

    expect(findEditorElement().classes()).toContain('is-focused');
  });

  it('removes is-focused class when blur event is emitted', async () => {
    createWrapper();

    await emitEditorEvent({ tiptapEditor: contentEditor.tiptapEditor, event: 'focus' });
    await emitEditorEvent({ tiptapEditor: contentEditor.tiptapEditor, event: 'blur' });

    expect(findEditorElement().classes()).not.toContain('is-focused');
  });

  it('emits change event when document is updated', async () => {
    createWrapper();

    await emitEditorEvent({ tiptapEditor: contentEditor.tiptapEditor, event: 'update' });

    expect(wrapper.emitted('change')).toEqual([
      [
        {
          empty: contentEditor.empty,
        },
      ],
    ]);
  });

  it('renders content_editor_error component', () => {
    createWrapper();

    expect(wrapper.findComponent(ContentEditorError).exists()).toBe(true);
  });

  describe('when loading content', () => {
    beforeEach(async () => {
      createWrapper();

      contentEditor.emit(LOADING_CONTENT_EVENT);

      await nextTick();
    });

    it('displays loading indicator', () => {
      expect(findLoadingIcon().exists()).toBe(true);
    });

    it('hides EditorContent component', () => {
      expect(findEditorContent().exists()).toBe(false);
    });

    it('hides formatting bubble menu', () => {
      expect(findBubbleMenu().exists()).toBe(false);
    });
  });

  describe('when loading content succeeds', () => {
    beforeEach(async () => {
      createWrapper();

      contentEditor.emit(LOADING_CONTENT_EVENT);
      await nextTick();
      contentEditor.emit(LOADING_SUCCESS_EVENT);
      await nextTick();
    });

    it('hides loading indicator', () => {
      expect(findLoadingIcon().exists()).toBe(false);
    });

    it('displays EditorContent component', () => {
      expect(findEditorContent().exists()).toBe(true);
    });
  });

  describe('when loading content fails', () => {
    const error = 'error';

    beforeEach(async () => {
      createWrapper();

      contentEditor.emit(LOADING_CONTENT_EVENT);
      await nextTick();
      contentEditor.emit(LOADING_ERROR_EVENT, error);
      await nextTick();
    });

    it('hides loading indicator', () => {
      expect(findLoadingIcon().exists()).toBe(false);
    });

    it('displays EditorContent component', () => {
      expect(findEditorContent().exists()).toBe(true);
    });

    it('displays formatting bubble menu', () => {
      expect(findBubbleMenu().exists()).toBe(true);
    });
  });
});