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

content_editor_integration_spec.js « content_editor « frontend_integration « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8419c7aae634d0bc36b3ae2fca17288bbe9dd0ba (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
186
187
188
189
190
191
192
193
194
import { nextTick } from 'vue';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import { ContentEditor } from '~/content_editor';
import waitForPromises from 'helpers/wait_for_promises';

/**
 * This spec exercises some workflows in the Content Editor without mocking
 * any component.
 *
 */
describe('content_editor', () => {
  let wrapper;
  let renderMarkdown;

  const buildWrapper = ({ markdown = '', listeners = {} } = {}) => {
    wrapper = mountExtended(ContentEditor, {
      propsData: {
        renderMarkdown,
        uploadsPath: '/',
        markdown,
      },
      listeners: {
        ...listeners,
      },
      mocks: {
        $apollo: {
          queries: {
            currentUser: {
              loading: false,
            },
          },
        },
      },
    });
  };

  const waitUntilContentIsLoaded = async () => {
    await waitForPromises();
    await nextTick();
  };

  const mockRenderMarkdownResponse = (response) => {
    renderMarkdown.mockImplementation((markdown) => (markdown ? response : null));
  };

  beforeEach(() => {
    renderMarkdown = jest.fn();
  });

  describe('when loading initial content', () => {
    describe('when the initial content is empty', () => {
      it('still hides the loading indicator', async () => {
        mockRenderMarkdownResponse('');

        buildWrapper();

        await waitUntilContentIsLoaded();

        expect(wrapper.findByTestId('content-editor-loading-indicator').exists()).toBe(false);
      });
    });

    describe('when the initial content is not empty', () => {
      const initialContent = '<strong>bold text</strong> and <em>italic text</em>';
      beforeEach(async () => {
        mockRenderMarkdownResponse(initialContent);

        buildWrapper({
          markdown: '**bold text**',
        });

        await waitUntilContentIsLoaded();
      });
      it('hides the loading indicator', () => {
        expect(wrapper.findByTestId('content-editor-loading-indicator').exists()).toBe(false);
      });

      it('displays the initial content', () => {
        expect(wrapper.html()).toContain(initialContent);
      });
    });
  });

  describe('when preserveUnchangedMarkdown feature flag is enabled', () => {
    beforeEach(() => {
      gon.features = { preserveUnchangedMarkdown: true };
    });
    afterEach(() => {
      gon.features = { preserveUnchangedMarkdown: false };
    });

    it('processes and renders footnote ids alongside the footnote definition', async () => {
      buildWrapper({
        markdown: `
This reference tag is a mix of letters and numbers [^footnote].

[^footnote]: This is another footnote.
        `,
      });

      await waitUntilContentIsLoaded();

      expect(wrapper.text()).toContain('footnote: This is another footnote');
    });

    it('processes and displays reference definitions', async () => {
      buildWrapper({
        markdown: `
[GitLab][gitlab]

[gitlab]: https://gitlab.com
        `,
      });

      await waitUntilContentIsLoaded();

      expect(wrapper.find('pre').text()).toContain('[gitlab]: https://gitlab.com');
    });
  });

  it('renders table of contents', async () => {
    renderMarkdown.mockResolvedValueOnce(`
<ul class="section-nav">
</ul>
<h1 dir="auto" data-sourcepos="3:1-3:11">
  Heading 1
</h1>
<h2 dir="auto" data-sourcepos="5:1-5:12">
  Heading 2
</h2>
    `);

    buildWrapper({
      markdown: `
[TOC]

# Heading 1

## Heading 2
      `,
    });

    await waitUntilContentIsLoaded();

    expect(wrapper.findByTestId('table-of-contents').text()).toContain('Heading 1');
    expect(wrapper.findByTestId('table-of-contents').text()).toContain('Heading 2');
  });

  describe('when pasting content', () => {
    const buildClipboardData = (data = {}) => ({
      clipboardData: {
        getData(mimeType) {
          return data[mimeType];
        },
        types: Object.keys(data),
      },
    });

    describe('when the clipboard does not contain text/html data', () => {
      it('processes the clipboard content as markdown', async () => {
        const processedMarkdown = '<strong>bold text</strong>';

        buildWrapper();

        await waitUntilContentIsLoaded();

        mockRenderMarkdownResponse(processedMarkdown);

        wrapper.find('[contenteditable]').trigger(
          'paste',
          buildClipboardData({
            'text/plain': '**bold text**',
          }),
        );

        await waitUntilContentIsLoaded();

        expect(wrapper.find('[contenteditable]').html()).toContain(processedMarkdown);
      });
    });
  });

  it('bubbles up the keydown event captured by ProseMirror', async () => {
    const keydownHandler = jest.fn();

    buildWrapper({ listeners: { keydown: keydownHandler } });

    await waitUntilContentIsLoaded();

    wrapper.find('[contenteditable]').trigger('keydown', {});

    expect(wrapper.emitted('keydown')).toHaveLength(1);
  });
});