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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
# frozen_string_literal: true
RSpec.shared_examples 'edits content using the content editor' do
include ContentEditorHelpers
let(:content_editor_testid) { '[data-testid="content-editor"] [contenteditable].ProseMirror' }
it 'saves page content in local storage if the user navigates away' do
switch_to_content_editor
expect(page).to have_css(content_editor_testid)
type_in_content_editor ' Typing text in the content editor'
wait_until_hidden_field_is_updated /Typing text in the content editor/
begin
refresh
rescue Selenium::WebDriver::Error::UnexpectedAlertOpenError
page.driver.browser.switch_to.alert.dismiss
end
expect(page).to have_text('Typing text in the content editor')
end
describe 'formatting bubble menu' do
it 'shows a formatting bubble menu for a regular paragraph and headings' do
switch_to_content_editor
expect(page).to have_css(content_editor_testid)
type_in_content_editor 'Typing text in the content editor'
type_in_content_editor [:shift, :left]
expect_formatting_menu_to_be_visible
type_in_content_editor [:right, :right, :enter, '## Heading']
expect_formatting_menu_to_be_hidden
type_in_content_editor [:shift, :left]
expect_formatting_menu_to_be_visible
end
end
describe 'creating and editing links' do
before do
switch_to_content_editor
end
context 'when clicking the link icon in the toolbar' do
it 'shows the link bubble menu' do
page.find('[data-testid="formatting-toolbar"] [data-testid="link"]').click
expect(page).to have_css('[data-testid="link-bubble-menu"]')
end
context 'if no text is selected' do
before do
page.find('[data-testid="formatting-toolbar"] [data-testid="link"]').click
end
it 'opens an empty inline modal to create a link' do
page.within '[data-testid="link-bubble-menu"]' do
expect(page).to have_field('link-text', with: '')
expect(page).to have_field('link-href', with: '')
end
end
context 'when the user clicks the apply button' do
it 'applies the changes to the document' do
page.within '[data-testid="link-bubble-menu"]' do
fill_in 'link-text', with: 'Link to GitLab home page'
fill_in 'link-href', with: 'https://gitlab.com'
click_button 'Apply'
end
page.within content_editor_testid do
expect(page).to have_css('a[href="https://gitlab.com"]')
expect(page).to have_text('Link to GitLab home page')
end
end
end
context 'when the user clicks the cancel button' do
it 'does not apply the changes to the document' do
page.within '[data-testid="link-bubble-menu"]' do
fill_in 'link-text', with: 'Link to GitLab home page'
fill_in 'link-href', with: 'https://gitlab.com'
click_button 'Cancel'
end
page.within content_editor_testid do
expect(page).not_to have_css('a')
end
end
end
end
context 'if text is selected' do
before do
type_in_content_editor 'The quick brown fox jumps over the lazy dog'
type_in_content_editor [:shift, :left]
type_in_content_editor [:shift, :left]
type_in_content_editor [:shift, :left]
page.find('[data-testid="formatting-toolbar"] [data-testid="link"]').click
end
it 'prefills inline modal to create a link' do
page.within '[data-testid="link-bubble-menu"]' do
expect(page).to have_field('link-text', with: 'dog')
expect(page).to have_field('link-href', with: '')
end
end
context 'when the user clicks the apply button' do
it 'applies the changes to the document' do
page.within '[data-testid="link-bubble-menu"]' do
fill_in 'link-text', with: 'new dog'
fill_in 'link-href', with: 'https://en.wikipedia.org/wiki/Shiba_Inu'
click_button 'Apply'
end
page.within content_editor_testid do
expect(page).to have_selector('a[href="https://en.wikipedia.org/wiki/Shiba_Inu"]',
text: 'new dog'
)
end
end
end
end
end
context 'if cursor is placed on an existing link' do
before do
type_in_content_editor 'Link to [GitLab home **page**](https://gitlab.com)'
type_in_content_editor :left
end
it 'prefills inline modal to edit the link' do
page.within '[data-testid="link-bubble-menu"]' do
page.find('[data-testid="edit-link"]').click
expect(page).to have_field('link-text', with: 'GitLab home page')
expect(page).to have_field('link-href', with: 'https://gitlab.com')
end
end
it 'updates the link attributes if text is not updated' do
page.within '[data-testid="link-bubble-menu"]' do
page.find('[data-testid="edit-link"]').click
fill_in 'link-href', with: 'https://about.gitlab.com'
click_button 'Apply'
end
page.within content_editor_testid do
expect(page).to have_selector('a[href="https://about.gitlab.com"]')
expect(page.find('a')).to have_text('GitLab home page')
expect(page).to have_selector('strong', text: 'page')
end
end
it 'updates the link attributes and text if text is updated' do
page.within '[data-testid="link-bubble-menu"]' do
page.find('[data-testid="edit-link"]').click
fill_in 'link-text', with: 'GitLab about page'
fill_in 'link-href', with: 'https://about.gitlab.com'
click_button 'Apply'
end
page.within content_editor_testid do
expect(page).to have_selector('a[href="https://about.gitlab.com"]',
text: 'GitLab about page'
)
expect(page).not_to have_selector('strong')
end
end
it 'does nothing if Cancel is clicked' do
page.within '[data-testid="link-bubble-menu"]' do
page.find('[data-testid="edit-link"]').click
click_button 'Cancel'
end
page.within content_editor_testid do
expect(page).to have_selector('a[href="https://gitlab.com"]',
text: 'GitLab home page'
)
expect(page).to have_selector('strong')
end
end
context 'when the user clicks the unlink button' do
it 'removes the link' do
page.within '[data-testid="link-bubble-menu"]' do
page.find('[data-testid="remove-link"]').click
end
page.within content_editor_testid do
expect(page).not_to have_selector('a')
expect(page).to have_selector('strong', text: 'page')
end
end
end
end
context 'when selection spans more than a link' do
before do
type_in_content_editor 'a [b **c**](https://gitlab.com)'
type_in_content_editor [:shift, :left]
type_in_content_editor [:shift, :left]
type_in_content_editor [:shift, :left]
type_in_content_editor [:shift, :left]
type_in_content_editor [:shift, :left]
page.find('[data-testid="formatting-toolbar"] [data-testid="link"]').click
end
it 'prefills inline modal with the entire selection' do
page.within '[data-testid="link-bubble-menu"]' do
expect(page).to have_field('link-text', with: 'a b c')
expect(page).to have_field('link-href', with: '')
end
end
it 'expands the link and updates the link attributes if text is not updated' do
page.within '[data-testid="link-bubble-menu"]' do
fill_in 'link-href', with: 'https://about.gitlab.com'
click_button 'Apply'
end
page.within content_editor_testid do
expect(page).to have_selector('a[href="https://about.gitlab.com"]')
expect(page.find('a')).to have_text('a b c')
expect(page).to have_selector('strong', text: 'c')
end
end
it 'expands the link, updates the link attributes and text if text is updated' do
page.within '[data-testid="link-bubble-menu"]' do
fill_in 'link-text', with: 'new text'
fill_in 'link-href', with: 'https://about.gitlab.com'
click_button 'Apply'
end
page.within content_editor_testid do
expect(page).to have_selector('a[href="https://about.gitlab.com"]',
text: 'new text'
)
expect(page).not_to have_selector('strong')
end
end
end
end
describe 'media elements bubble menu' do
before do
switch_to_content_editor
click_attachment_button
end
it 'displays correct media bubble menu for images', :js do
display_media_bubble_menu '[data-testid="content_editor_editablebox"] img[src]', 'dk.png'
expect_formatting_menu_to_be_hidden
expect_media_bubble_menu_to_be_visible
end
it 'displays correct media bubble menu for video', :js do
display_media_bubble_menu '[data-testid="content_editor_editablebox"] video', 'video_sample.mp4'
expect_formatting_menu_to_be_hidden
expect_media_bubble_menu_to_be_visible
end
end
describe 'code block' do
before do
visit(profile_preferences_path)
find('.syntax-theme').choose('Dark')
wait_for_requests
page.go_back
refresh
switch_to_content_editor
end
it 'applies theme classes to code blocks' do
expect(page).not_to have_css('.content-editor-code-block.code.highlight.dark')
type_in_content_editor [:enter, :enter]
type_in_content_editor '```js ' # trigger input rule
type_in_content_editor 'var a = 0'
expect(page).to have_css('.content-editor-code-block.code.highlight.dark')
end
end
describe 'code block bubble menu' do
before do
switch_to_content_editor
end
it 'shows a code block bubble menu for a code block' do
type_in_content_editor [:enter, :enter]
type_in_content_editor '```js ' # trigger input rule
type_in_content_editor 'var a = 0'
type_in_content_editor [:shift, :left]
expect_formatting_menu_to_be_hidden
expect(page).to have_css('[data-testid="code-block-bubble-menu"]')
end
it 'sets code block type to "javascript" for `js`' do
type_in_content_editor [:enter, :enter]
type_in_content_editor '```js '
type_in_content_editor 'var a = 0'
expect(find('[data-testid="code-block-bubble-menu"]')).to have_text('Javascript')
end
it 'sets code block type to "Custom (nomnoml)" for `nomnoml`' do
type_in_content_editor [:enter, :enter]
type_in_content_editor '```nomnoml '
type_in_content_editor 'test'
expect(find('[data-testid="code-block-bubble-menu"]')).to have_text('Custom (nomnoml)')
end
end
describe 'mermaid diagram' do
before do
switch_to_content_editor
type_in_content_editor [:enter, :enter]
type_in_content_editor '```mermaid '
type_in_content_editor ['graph TD;', :enter, ' JohnDoe12 --> HelloWorld34']
end
it 'renders and updates the diagram correctly in a sandboxed iframe' do
iframe = find(content_editor_testid).find('iframe')
expect(iframe['src']).to include('/-/sandbox/mermaid')
within_frame(iframe) do
expect(find('svg').text).to include('JohnDoe12')
expect(find('svg').text).to include('HelloWorld34')
end
expect(iframe['height'].to_i).to be > 100
find(content_editor_testid).send_keys [:enter, ' JaneDoe34 --> HelloWorld56']
within_frame(iframe) do
page.has_content?('JaneDoe34')
expect(find('svg').text).to include('JaneDoe34')
expect(find('svg').text).to include('HelloWorld56')
end
end
it 'toggles the diagram when preview button is clicked',
quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/397682' do
find('[data-testid="preview-diagram"]').click
expect(find(content_editor_testid)).not_to have_selector('iframe')
find('[data-testid="preview-diagram"]').click
iframe = find(content_editor_testid).find('iframe')
within_frame(iframe) do
expect(find('svg').text).to include('JohnDoe12')
expect(find('svg').text).to include('HelloWorld34')
end
end
end
describe 'autocomplete suggestions' do
let(:suggestions_dropdown) { '[data-testid="content-editor-suggestions-dropdown"]' }
before do
if defined?(project)
create(:issue, project: project, title: 'My Cool Linked Issue')
create(:merge_request, source_project: project, source_branch: 'branch-1', title: 'My Cool Merge Request')
create(:label, project: project, title: 'My Cool Label')
create(:milestone, project: project, title: 'My Cool Milestone')
project.add_maintainer(create(:user, name: 'abc123', username: 'abc123'))
else # group wikis
project = create(:project, group: group)
create(:issue, project: project, title: 'My Cool Linked Issue')
create(:merge_request, source_project: project, source_branch: 'branch-1', title: 'My Cool Merge Request')
create(:group_label, group: group, title: 'My Cool Label')
create(:milestone, group: group, title: 'My Cool Milestone')
project.add_maintainer(create(:user, name: 'abc123', username: 'abc123'))
end
switch_to_content_editor
type_in_content_editor :enter
end
it 'shows suggestions for members with descriptions' do
type_in_content_editor '@a'
expect(find(suggestions_dropdown)).to have_text('abc123')
expect(find(suggestions_dropdown)).to have_text('all')
expect(find(suggestions_dropdown)).to have_text('Group Members')
type_in_content_editor 'bc'
send_keys [:arrow_down, :enter]
expect(page).not_to have_css(suggestions_dropdown)
expect(page).to have_text('@abc123')
end
it 'shows suggestions for merge requests' do
type_in_content_editor '!'
expect(find(suggestions_dropdown)).to have_text('My Cool Merge Request')
send_keys :enter
expect(page).not_to have_css(suggestions_dropdown)
expect(page).to have_text('!1')
end
it 'shows suggestions for issues' do
type_in_content_editor '#'
expect(find(suggestions_dropdown)).to have_text('My Cool Linked Issue')
send_keys :enter
expect(page).not_to have_css(suggestions_dropdown)
expect(page).to have_text('#1')
end
it 'shows suggestions for milestones' do
type_in_content_editor '%'
expect(find(suggestions_dropdown)).to have_text('My Cool Milestone')
send_keys :enter
expect(page).not_to have_css(suggestions_dropdown)
expect(page).to have_text('%My Cool Milestone')
end
it 'shows suggestions for emojis' do
type_in_content_editor ':smile'
expect(find(suggestions_dropdown)).to have_text('🙂 slight_smile')
expect(find(suggestions_dropdown)).to have_text('😸 smile_cat')
send_keys :enter
expect(page).not_to have_css(suggestions_dropdown)
expect(page).to have_text('🙂')
end
it 'doesn\'t show suggestions dropdown if there are no suggestions to show' do
type_in_content_editor '%'
expect(find(suggestions_dropdown)).to have_text('My Cool Milestone')
type_in_content_editor 'x'
expect(page).not_to have_css(suggestions_dropdown)
end
it 'scrolls selected item into view when navigating with keyboard' do
type_in_content_editor ':'
expect(find(suggestions_dropdown)).to have_text('hundred points symbol')
expect(dropdown_scroll_top).to be 0
send_keys :arrow_up
expect(dropdown_scroll_top).to be > 100
end
def dropdown_scroll_top
evaluate_script("document.querySelector('#{suggestions_dropdown} .gl-dropdown-inner').scrollTop")
end
end
end
RSpec.shared_examples 'inserts diagrams.net diagram using the content editor' do
include ContentEditorHelpers
before do
switch_to_content_editor
click_attachment_button
end
it 'displays correct media bubble menu with edit diagram button' do
display_media_bubble_menu '[data-testid="content_editor_editablebox"] img[src]', 'diagram.drawio.svg'
expect_formatting_menu_to_be_hidden
expect_media_bubble_menu_to_be_visible
click_edit_diagram_button
expect_drawio_editor_is_opened
end
end
|