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

link_bubble_menu_spec.js « bubble_menus « components « content_editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9aa9c6483f45282243e7484314da6ada1fdb7b42 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { GlLink, GlForm } from '@gitlab/ui';
import { nextTick } from 'vue';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import LinkBubbleMenu from '~/content_editor/components/bubble_menus/link_bubble_menu.vue';
import EditorStateObserver from '~/content_editor/components/editor_state_observer.vue';
import eventHubFactory from '~/helpers/event_hub_factory';
import BubbleMenu from '~/content_editor/components/bubble_menus/bubble_menu.vue';
import { stubComponent } from 'helpers/stub_component';
import Link from '~/content_editor/extensions/link';
import { createTestEditor } from '../../test_utils';

const createFakeEvent = () => ({ preventDefault: jest.fn(), stopPropagation: jest.fn() });

describe('content_editor/components/bubble_menus/link_bubble_menu', () => {
  let wrapper;
  let tiptapEditor;
  let contentEditor;
  let eventHub;

  const buildEditor = () => {
    tiptapEditor = createTestEditor({ extensions: [Link] });
    contentEditor = { resolveUrl: jest.fn() };
    eventHub = eventHubFactory();
  };

  const buildWrapper = () => {
    wrapper = mountExtended(LinkBubbleMenu, {
      provide: {
        tiptapEditor,
        contentEditor,
        eventHub,
      },
      stubs: {
        BubbleMenu: stubComponent(BubbleMenu),
      },
    });
  };

  const showMenu = () => {
    wrapper.findComponent(BubbleMenu).vm.$emit('show');
    return nextTick();
  };

  const buildWrapperAndDisplayMenu = () => {
    buildWrapper();

    return showMenu();
  };

  const findBubbleMenu = () => wrapper.findComponent(BubbleMenu);
  const findLink = () => wrapper.findComponent(GlLink);
  const findEditorStateObserver = () => wrapper.findComponent(EditorStateObserver);
  const findEditLinkButton = () => wrapper.findByTestId('edit-link');

  const expectLinkButtonsToExist = (exist = true) => {
    expect(wrapper.findComponent(GlLink).exists()).toBe(exist);
    expect(wrapper.findByTestId('copy-link-url').exists()).toBe(exist);
    expect(wrapper.findByTestId('edit-link').exists()).toBe(exist);
    expect(wrapper.findByTestId('remove-link').exists()).toBe(exist);
  };

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

    tiptapEditor
      .chain()
      .insertContent(
        'Download <a href="/path/to/project/-/wikis/uploads/my_file.pdf" data-canonical-src="uploads/my_file.pdf" title="Click here to download">PDF File</a>',
      )
      .setTextSelection(14) // put cursor in the middle of the link
      .run();
  });

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

  it('renders bubble menu component', async () => {
    await buildWrapperAndDisplayMenu();

    expect(findBubbleMenu().classes()).toEqual(['gl-shadow', 'gl-rounded-base', 'gl-bg-white']);
  });

  it('shows a clickable link to the URL in the link node', async () => {
    await buildWrapperAndDisplayMenu();

    expect(findLink().attributes()).toEqual(
      expect.objectContaining({
        href: '/path/to/project/-/wikis/uploads/my_file.pdf',
        'aria-label': 'uploads/my_file.pdf',
        title: 'uploads/my_file.pdf',
        target: '_blank',
      }),
    );
    expect(findLink().text()).toBe('uploads/my_file.pdf');
  });

  it('updates the bubble menu state when @selectionUpdate event is triggered', async () => {
    const linkUrl = 'https://gitlab.com';

    await buildWrapperAndDisplayMenu();

    expect(findLink().attributes()).toEqual(
      expect.objectContaining({
        href: '/path/to/project/-/wikis/uploads/my_file.pdf',
      }),
    );

    tiptapEditor
      .chain()
      .setContent(
        `Link to <a href="${linkUrl}" data-canonical-src="${linkUrl}" title="Click here to download">GitLab</a>`,
      )
      .setTextSelection(11)
      .run();

    findEditorStateObserver().vm.$emit('selectionUpdate');

    await nextTick();

    expect(findLink().attributes()).toEqual(
      expect.objectContaining({
        href: linkUrl,
      }),
    );
  });

  describe('when the selection changes within the same link', () => {
    it('does not update the bubble menu state', async () => {
      await buildWrapperAndDisplayMenu();

      await findEditLinkButton().trigger('click');

      expect(wrapper.findComponent(GlForm).exists()).toBe(true);

      tiptapEditor.commands.setTextSelection(13);

      findEditorStateObserver().vm.$emit('selectionUpdate');

      await nextTick();

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

  it('cleans bubble menu state when hidden event is triggered', async () => {
    await buildWrapperAndDisplayMenu();

    expect(findLink().attributes()).toEqual(
      expect.objectContaining({
        href: '/path/to/project/-/wikis/uploads/my_file.pdf',
      }),
    );

    findBubbleMenu().vm.$emit('hidden');

    await nextTick();

    expect(findLink().attributes()).toEqual(
      expect.objectContaining({
        href: '#',
      }),
    );
    expect(findLink().text()).toEqual('');
  });

  describe('copy button', () => {
    it('copies the canonical link to clipboard', async () => {
      await buildWrapperAndDisplayMenu();

      jest.spyOn(navigator.clipboard, 'writeText');

      await wrapper.findByTestId('copy-link-url').vm.$emit('click');

      expect(navigator.clipboard.writeText).toHaveBeenCalledWith('uploads/my_file.pdf');
    });
  });

  describe('remove link button', () => {
    it('removes the link', async () => {
      await buildWrapperAndDisplayMenu();
      await wrapper.findByTestId('remove-link').vm.$emit('click');

      expect(tiptapEditor.getHTML()).toBe('<p>Download PDF File</p>');
    });
  });

  describe('for a placeholder link', () => {
    beforeEach(async () => {
      tiptapEditor
        .chain()
        .clearContent()
        .insertContent('Dummy link')
        .selectAll()
        .setLink({ href: '' })
        .setTextSelection(4)
        .run();

      await buildWrapperAndDisplayMenu();
    });

    it('directly opens the edit form for a placeholder link', async () => {
      expectLinkButtonsToExist(false);

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

    it('removes the link on clicking apply (if no change)', async () => {
      await wrapper.findComponent(GlForm).vm.$emit('submit', createFakeEvent());

      expect(tiptapEditor.getHTML()).toBe('<p>Dummy link</p>');
    });

    it('removes the link on clicking cancel', async () => {
      await wrapper.findByTestId('cancel-link').vm.$emit('click');

      expect(tiptapEditor.getHTML()).toBe('<p>Dummy link</p>');
    });
  });

  describe('edit button', () => {
    let linkHrefInput;
    let linkTitleInput;

    beforeEach(async () => {
      await buildWrapperAndDisplayMenu();
      await wrapper.findByTestId('edit-link').vm.$emit('click');

      linkHrefInput = wrapper.findByTestId('link-href');
      linkTitleInput = wrapper.findByTestId('link-title');
    });

    it('hides the link and copy/edit/remove link buttons', async () => {
      expectLinkButtonsToExist(false);
    });

    it('shows a form to edit the link', () => {
      expect(wrapper.findComponent(GlForm).exists()).toBe(true);

      expect(linkHrefInput.element.value).toBe('uploads/my_file.pdf');
      expect(linkTitleInput.element.value).toBe('Click here to download');
    });

    it('extends selection to select the entire link', () => {
      const { from, to } = tiptapEditor.state.selection;

      expect(from).toBe(10);
      expect(to).toBe(18);
    });

    describe('after making changes in the form and clicking apply', () => {
      beforeEach(async () => {
        linkHrefInput.setValue('https://google.com');
        linkTitleInput.setValue('Search Google');

        contentEditor.resolveUrl.mockResolvedValue('https://google.com');

        await wrapper.findComponent(GlForm).vm.$emit('submit', createFakeEvent());
      });

      it('updates prosemirror doc with new link', async () => {
        expect(tiptapEditor.getHTML()).toBe(
          '<p>Download <a target="_blank" rel="noopener noreferrer nofollow" href="https://google.com" title="Search Google">PDF File</a></p>',
        );
      });

      it('updates the link in the bubble menu', () => {
        const link = wrapper.findComponent(GlLink);
        expect(link.attributes()).toEqual(
          expect.objectContaining({
            href: 'https://google.com',
            'aria-label': 'https://google.com',
            title: 'https://google.com',
            target: '_blank',
          }),
        );
        expect(link.text()).toBe('https://google.com');
      });
    });

    describe('after making changes in the form and clicking cancel', () => {
      beforeEach(async () => {
        linkHrefInput.setValue('https://google.com');
        linkTitleInput.setValue('Search Google');

        await wrapper.findByTestId('cancel-link').vm.$emit('click');
      });

      it('hides the form and shows the copy/edit/remove link buttons', () => {
        expectLinkButtonsToExist();
      });

      it('resets the form with old values of the link from prosemirror', async () => {
        // click edit once again to show the form back
        await wrapper.findByTestId('edit-link').vm.$emit('click');

        linkHrefInput = wrapper.findByTestId('link-href');
        linkTitleInput = wrapper.findByTestId('link-title');

        expect(linkHrefInput.element.value).toBe('uploads/my_file.pdf');
        expect(linkTitleInput.element.value).toBe('Click here to download');
      });
    });
  });
});