summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/components/bubble_menus/link_spec.js
blob: ba6d8da9584f4d505376942d82657efe8174a2ec (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
import { GlLink, GlForm } from '@gitlab/ui';
import { BubbleMenu } from '@tiptap/vue-2';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import LinkBubbleMenu from '~/content_editor/components/bubble_menus/link.vue';
import eventHubFactory from '~/helpers/event_hub_factory';
import Link from '~/content_editor/extensions/link';
import { createTestEditor, emitEditorEvent } from '../../test_utils';

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

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

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

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

  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();
    buildWrapper();

    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();

    await emitEditorEvent({ event: 'transaction', tiptapEditor });

    bubbleMenu = wrapper.findComponent(BubbleMenu);
  });

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

  it('renders bubble menu component', async () => {
    expect(bubbleMenu.props('editor')).toBe(tiptapEditor);
    expect(bubbleMenu.classes()).toEqual(['gl-shadow', 'gl-rounded-base', 'gl-bg-white']);
  });

  it('shows a clickable link to the URL in the link node', async () => {
    const link = wrapper.findComponent(GlLink);
    expect(link.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(link.text()).toBe('uploads/my_file.pdf');
  });

  describe('copy button', () => {
    it('copies the canonical link to clipboard', async () => {
      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 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 emitEditorEvent({ event: 'transaction', tiptapEditor });
    });

    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 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);
    });

    it('shows the copy/edit/remove link buttons again if selection changes to another non-link and then back again to a link', async () => {
      expectLinkButtonsToExist(false);

      tiptapEditor.commands.setTextSelection(3);
      await emitEditorEvent({ event: 'transaction', tiptapEditor });

      tiptapEditor.commands.setTextSelection(14);
      await emitEditorEvent({ event: 'transaction', tiptapEditor });

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

    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" canonicalsrc="https://google.com">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');
      });
    });
  });
});