summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/components/content_editor_spec.js
blob: 73fcfeab8bcd0f3994180b389a4b742bc562d458 (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
import { EditorContent } from '@tiptap/vue-2';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ContentEditor from '~/content_editor/components/content_editor.vue';
import ContentEditorAlert from '~/content_editor/components/content_editor_alert.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 LoadingIndicator from '~/content_editor/components/loading_indicator.vue';
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 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_alert component', () => {
    createWrapper();

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

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

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

  it('renders formatting bubble menu', () => {
    createWrapper();

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