summaryrefslogtreecommitdiff
path: root/spec/features/issues/gfm_autocomplete_spec.rb
blob: ff78b9e608f90fc0f014c04afb4fe32489bf645e (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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'GFM autocomplete', :js do
  let_it_be(:user_xss_title) { 'eve <img src=x onerror=alert(2)&lt;img src=x onerror=alert(1)&gt;' }
  let_it_be(:user_xss) { create(:user, name: user_xss_title, username: 'xss.user') }
  let_it_be(:user) { create(:user, name: '💃speciąl someone💃', username: 'someone.special') }
  let_it_be(:group) { create(:group, name: 'Ancestor') }
  let_it_be(:child_group) { create(:group, parent: group, name: 'My group') }
  let_it_be(:project) { create(:project, group: child_group) }
  let_it_be(:label) { create(:label, project: project, title: 'special+') }

  let(:issue) { create(:issue, project: project) }

  before_all do
    project.add_maintainer(user)
    project.add_maintainer(user_xss)
  end

  describe 'when tribute_autocomplete feature flag is off' do
    before do
      stub_feature_flags(tribute_autocomplete: false)

      sign_in(user)
      visit project_issue_path(project, issue)

      wait_for_requests
    end

    it 'updates issue description with GFM reference' do
      find('.js-issuable-edit').click

      wait_for_requests

      simulate_input('#issue-description', "@#{user.name[0...3]}")

      wait_for_requests

      find('.atwho-view .cur').click

      click_button 'Save changes'

      wait_for_requests

      expect(find('.description')).to have_content(user.to_reference)
    end

    it 'opens quick action autocomplete when updating description' do
      find('.js-issuable-edit').click

      find('#issue-description').native.send_keys('/')

      expect(page).to have_selector('.atwho-container')
    end

    it 'opens autocomplete menu when field starts with text' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('@')
      end

      expect(page).to have_selector('.atwho-container')
    end

    it 'opens autocomplete menu for Issues when field starts with text with item escaping HTML characters' do
      issue_xss_title = 'This will execute alert<img src=x onerror=alert(2)&lt;img src=x onerror=alert(1)&gt;'
      create(:issue, project: project, title: issue_xss_title)

      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('#')
      end

      wait_for_requests

      expect(page).to have_selector('.atwho-container')

      page.within '.atwho-container #at-view-issues' do
        expect(page.all('li').first.text).to include(issue_xss_title)
      end
    end

    it 'opens autocomplete menu for Username when field starts with text with item escaping HTML characters' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('@ev')
      end

      wait_for_requests

      expect(page).to have_selector('.atwho-container')

      page.within '.atwho-container #at-view-users' do
        expect(find('li').text).to have_content(user_xss.username)
      end
    end

    it 'opens autocomplete menu for Milestone when field starts with text with item escaping HTML characters' do
      milestone_xss_title = 'alert milestone &lt;img src=x onerror="alert(\'Hello xss\');" a'
      create(:milestone, project: project, title: milestone_xss_title)

      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('%')
      end

      wait_for_requests

      expect(page).to have_selector('.atwho-container')

      page.within '.atwho-container #at-view-milestones' do
        expect(find('li').text).to have_content('alert milestone')
      end
    end

    it 'doesnt open autocomplete menu character is prefixed with text' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('testing')
        find('#note-body').native.send_keys('@')
      end

      expect(page).not_to have_selector('.atwho-view')
    end

    it 'doesnt select the first item for non-assignee dropdowns' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys(':')
      end

      expect(page).to have_selector('.atwho-container')

      wait_for_requests

      expect(find('#at-view-58')).not_to have_selector('.cur:first-of-type')
    end

    it 'does not open autocomplete menu when ":" is prefixed by a number and letters' do
      note = find('#note-body')

      # Number.
      page.within '.timeline-content-form' do
        note.native.send_keys('7:')
      end

      expect(page).not_to have_selector('.atwho-view')

      # ASCII letter.
      page.within '.timeline-content-form' do
        note.set('')
        note.native.send_keys('w:')
      end

      expect(page).not_to have_selector('.atwho-view')

      # Non-ASCII letter.
      page.within '.timeline-content-form' do
        note.set('')
        note.native.send_keys('Ё:')
      end

      expect(page).not_to have_selector('.atwho-view')
    end

    it 'selects the first item for assignee dropdowns' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('@')
      end

      expect(page).to have_selector('.atwho-container')

      wait_for_requests

      expect(find('#at-view-users')).to have_selector('.cur:first-of-type')
    end

    it 'includes items for assignee dropdowns with non-ASCII characters in name' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('')
        simulate_input('#note-body', "@#{user.name[0...8]}")
      end

      expect(page).to have_selector('.atwho-container')

      wait_for_requests

      expect(find('#at-view-users')).to have_content(user.name)
    end

    it 'selects the first item for non-assignee dropdowns if a query is entered' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys(':1')
      end

      expect(page).to have_selector('.atwho-container')

      wait_for_requests

      expect(find('#at-view-58')).to have_selector('.cur:first-of-type')
    end

    context 'if a selected value has special characters' do
      it 'wraps the result in double quotes' do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys('')
          simulate_input('#note-body', "~#{label.title[0]}")
        end

        label_item = find('.atwho-view li', text: label.title)

        expect_to_wrap(true, label_item, note, label.title)
      end

      it "shows dropdown after a new line" do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('test')
          note.native.send_keys(:enter)
          note.native.send_keys(:enter)
          note.native.send_keys('@')
        end

        expect(page).to have_selector('.atwho-container')
      end

      it "does not show dropdown when preceded with a special character" do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys("@")
        end

        expect(page).to have_selector('.atwho-container')

        page.within '.timeline-content-form' do
          note.native.send_keys("@")
        end

        expect(page).to have_selector('.atwho-container', visible: false)
      end

      it "does not throw an error if no labels exist" do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('~')
        end

        expect(page).to have_selector('.atwho-container', visible: false)
      end

      it 'doesn\'t wrap for assignee values' do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys("@#{user.username[0]}")
        end

        user_item = find('.atwho-view li', text: user.username)

        expect_to_wrap(false, user_item, note, user.username)
      end

      it 'doesn\'t wrap for emoji values' do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys(":cartwheel_")
        end

        emoji_item = find('.atwho-view li', text: 'cartwheel_tone1')

        expect_to_wrap(false, emoji_item, note, 'cartwheel_tone1')
      end

      it 'doesn\'t open autocomplete after non-word character' do
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys("@#{user.username[0..2]}!")
        end

        expect(page).not_to have_selector('.atwho-view')
      end

      it 'doesn\'t open autocomplete if there is no space before' do
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys("hello:#{user.username[0..2]}")
        end

        expect(page).not_to have_selector('.atwho-view')
      end

      it 'triggers autocomplete after selecting a quick action' do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('/as')
        end

        find('.atwho-view li', text: '/assign')
        note.native.send_keys(:tab)

        user_item = find('.atwho-view li', text: user.username)
        expect(user_item).to have_content(user.username)
      end
    end

    context 'assignees' do
      let(:issue_assignee) { create(:issue, project: project) }
      let(:unassigned_user) { create(:user) }

      before do
        issue_assignee.update(assignees: [user])

        project.add_maintainer(unassigned_user)
      end

      it 'lists users who are currently not assigned to the issue when using /assign' do
        visit project_issue_path(project, issue_assignee)

        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('/as')
        end

        find('.atwho-view li', text: '/assign')
        note.native.send_keys(:tab)

        wait_for_requests

        expect(find('#at-view-users .atwho-view-ul')).not_to have_content(user.username)
        expect(find('#at-view-users .atwho-view-ul')).to have_content(unassigned_user.username)
      end

      it 'shows dropdown on new issue form' do
        visit new_project_issue_path(project)

        textarea = find('#issue_description')
        textarea.native.send_keys('/ass')
        find('.atwho-view li', text: '/assign')
        textarea.native.send_keys(:tab)

        expect(find('#at-view-users .atwho-view-ul')).to have_content(unassigned_user.username)
        expect(find('#at-view-users .atwho-view-ul')).to have_content(user.username)
      end
    end

    context 'labels' do
      it 'opens autocomplete menu for Labels when field starts with text with item escaping HTML characters' do
        label_xss_title = 'alert label &lt;img src=x onerror="alert(\'Hello xss\');" a'
        create(:label, project: project, title: label_xss_title)

        note = find('#note-body')

        # It should show all the labels on "~".
        type(note, '~')

        wait_for_requests

        page.within '.atwho-container #at-view-labels' do
          expect(find('.atwho-view-ul').text).to have_content('alert label')
        end
      end

      it 'allows colons when autocompleting scoped labels' do
        create(:label, project: project, title: 'scoped:label')

        note = find('#note-body')
        type(note, '~scoped:')

        wait_for_requests

        page.within '.atwho-container #at-view-labels' do
          expect(find('.atwho-view-ul').text).to have_content('scoped:label')
        end
      end

      it 'allows colons when autocompleting scoped labels with double colons' do
        create(:label, project: project, title: 'scoped::label')

        note = find('#note-body')
        type(note, '~scoped::')

        wait_for_requests

        page.within '.atwho-container #at-view-labels' do
          expect(find('.atwho-view-ul').text).to have_content('scoped::label')
        end
      end

      it 'allows spaces when autocompleting multi-word labels' do
        create(:label, project: project, title: 'Accepting merge requests')

        note = find('#note-body')
        type(note, '~Accepting merge')

        wait_for_requests

        page.within '.atwho-container #at-view-labels' do
          expect(find('.atwho-view-ul').text).to have_content('Accepting merge requests')
        end
      end

      it 'only autocompletes the latest label' do
        create(:label, project: project, title: 'Accepting merge requests')
        create(:label, project: project, title: 'Accepting job applicants')

        note = find('#note-body')
        type(note, '~Accepting merge requests foo bar ~Accepting job')

        wait_for_requests

        page.within '.atwho-container #at-view-labels' do
          expect(find('.atwho-view-ul').text).to have_content('Accepting job applicants')
        end
      end

      it 'does not autocomplete labels if no tilde is typed' do
        create(:label, project: project, title: 'Accepting merge requests')

        note = find('#note-body')
        type(note, 'Accepting merge')

        wait_for_requests

        expect(page).not_to have_css('.atwho-container #at-view-labels')
      end
    end

    shared_examples 'autocomplete suggestions' do
      it 'suggests objects correctly' do
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys(object.class.reference_prefix)
        end

        page.within '.atwho-container' do
          expect(page).to have_content(object.title)

          find('ul li').click
        end

        expect(find('.new-note #note-body').value).to include(expected_body)
      end
    end

    context 'issues' do
      let(:object) { issue }
      let(:expected_body) { object.to_reference }

      it_behaves_like 'autocomplete suggestions'
    end

    context 'merge requests' do
      let(:object) { create(:merge_request, source_project: project) }
      let(:expected_body) { object.to_reference }

      it_behaves_like 'autocomplete suggestions'
    end

    context 'project snippets' do
      let!(:object) { create(:project_snippet, project: project, title: 'code snippet') }
      let(:expected_body) { object.to_reference }

      it_behaves_like 'autocomplete suggestions'
    end

    context 'label' do
      let!(:object) { label }
      let(:expected_body) { object.title }

      it_behaves_like 'autocomplete suggestions'
    end

    context 'milestone' do
      let!(:object) { create(:milestone, project: project) }
      let(:expected_body) { object.to_reference }

      it_behaves_like 'autocomplete suggestions'
    end
  end

  describe 'when tribute_autocomplete feature flag is on' do
    before do
      stub_feature_flags(tribute_autocomplete: true)

      sign_in(user)
      visit project_issue_path(project, issue)

      wait_for_requests
    end

    it 'updates issue description with GFM reference' do
      find('.js-issuable-edit').click

      wait_for_requests

      simulate_input('#issue-description', "@#{user.name[0...3]}")

      wait_for_requests

      find('.tribute-container .highlight', visible: true).click

      click_button 'Save changes'

      wait_for_requests

      expect(find('.description')).to have_content(user.to_reference)
    end

    it 'opens autocomplete menu when field starts with text' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('@')
      end

      expect(page).to have_selector('.tribute-container', visible: true)
    end

    it 'opens autocomplete menu for Issues when field starts with text with item escaping HTML characters' do
      issue_xss_title = 'This will execute alert<img src=x onerror=alert(2)&lt;img src=x onerror=alert(1)&gt;'
      create(:issue, project: project, title: issue_xss_title)

      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('#')
      end

      wait_for_requests

      expect(page).to have_selector('.tribute-container', visible: true)

      page.within '.tribute-container ul' do
        expect(page.all('li').first.text).to include(issue_xss_title)
      end
    end

    it 'opens autocomplete menu for Username when field starts with text with item escaping HTML characters' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('@ev')
      end

      wait_for_requests

      expect(page).to have_selector('.tribute-container', visible: true)

      expect(find('.tribute-container ul', visible: true)).to have_text(user_xss.username)
    end

    it 'selects the first item for assignee dropdowns' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('@')
      end

      expect(page).to have_selector('.tribute-container', visible: true)

      wait_for_requests

      expect(find('.tribute-container ul', visible: true)).to have_selector('.highlight:first-of-type')
    end

    it 'includes items for assignee dropdowns with non-ASCII characters in name' do
      page.within '.timeline-content-form' do
        find('#note-body').native.send_keys('')
        simulate_input('#note-body', "@#{user.name[0...8]}")
      end

      expect(page).to have_selector('.tribute-container', visible: true)

      wait_for_requests

      expect(find('.tribute-container ul', visible: true)).to have_content(user.name)
    end

    context 'when autocompleting for groups' do
      it 'shows the group when searching for the name of the group' do
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys('@mygroup')
        end

        expect(find('.tribute-container ul', visible: true)).to have_text('My group')
      end

      it 'does not show the group when searching for the name of the parent of the group' do
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys('@ancestor')
        end

        expect(find('.tribute-container ul', visible: true)).not_to have_text('My group')
      end
    end

    context 'if a selected value has special characters' do
      it 'wraps the result in double quotes' do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys('')
          simulate_input('#note-body', "~#{label.title[0]}")
        end

        label_item = find('.tribute-container ul', text: label.title, visible: true)

        expect_to_wrap(true, label_item, note, label.title)
      end

      it "shows dropdown after a new line" do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('test')
          note.native.send_keys(:enter)
          note.native.send_keys(:enter)
          note.native.send_keys('@')
        end

        expect(page).to have_selector('.tribute-container', visible: true)
      end

      it "does not show dropdown when preceded with a special character" do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys("@")
        end

        expect(page).to have_selector('.tribute-container', visible: true)

        page.within '.timeline-content-form' do
          note.native.send_keys("@")
        end

        expect(page).not_to have_selector('.tribute-container')
      end

      it "does not throw an error if no labels exist" do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('~')
        end

        expect(page).to have_selector('.tribute-container', visible: false)
      end

      it 'doesn\'t wrap for assignee values' do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys("@#{user.username[0]}")
        end

        user_item = find('.tribute-container ul', text: user.username, visible: true)

        expect_to_wrap(false, user_item, note, user.username)
      end

      it 'doesn\'t open autocomplete after non-word character' do
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys("@#{user.username[0..2]}!")
        end

        expect(page).not_to have_selector('.tribute-container')
      end

      it 'triggers autocomplete after selecting a quick action' do
        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('/as')
        end

        find('.atwho-view li', text: '/assign')
        note.native.send_keys(:tab)
        note.native.send_keys(:right)

        wait_for_requests

        user_item = find('.tribute-container ul', text: user.username, visible: true)
        expect(user_item).to have_content(user.username)
      end
    end

    context 'assignees' do
      let(:issue_assignee) { create(:issue, project: project) }
      let(:unassigned_user) { create(:user) }

      before do
        issue_assignee.update(assignees: [user])

        project.add_maintainer(unassigned_user)
      end

      it 'lists users who are currently not assigned to the issue when using /assign' do
        visit project_issue_path(project, issue_assignee)

        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('/as')
        end

        find('.atwho-view li', text: '/assign')
        note.native.send_keys(:tab)
        note.native.send_keys(:right)

        wait_for_requests

        expect(find('.tribute-container ul', visible: true)).not_to have_content(user.username)
        expect(find('.tribute-container ul', visible: true)).to have_content(unassigned_user.username)
      end

      it 'lists users who are currently not assigned to the issue when using /assign on the second line' do
        visit project_issue_path(project, issue_assignee)

        note = find('#note-body')
        page.within '.timeline-content-form' do
          note.native.send_keys('/assign @user2')
          note.native.send_keys(:enter)
          note.native.send_keys('/assign @')
          note.native.send_keys(:right)
        end

        wait_for_requests

        expect(find('.tribute-container ul', visible: true)).not_to have_content(user.username)
        expect(find('.tribute-container ul', visible: true)).to have_content(unassigned_user.username)
      end
    end

    context 'labels' do
      it 'opens autocomplete menu for Labels when field starts with text with item escaping HTML characters' do
        label_xss_title = 'alert label &lt;img src=x onerror="alert(\'Hello xss\');" a'
        create(:label, project: project, title: label_xss_title)

        note = find('#note-body')

        # It should show all the labels on "~".
        type(note, '~')

        wait_for_requests

        expect(find('.tribute-container ul', visible: true).text).to have_content('alert label')
      end

      it 'allows colons when autocompleting scoped labels' do
        create(:label, project: project, title: 'scoped:label')

        note = find('#note-body')
        type(note, '~scoped:')

        wait_for_requests

        expect(find('.tribute-container ul', visible: true).text).to have_content('scoped:label')
      end

      it 'allows colons when autocompleting scoped labels with double colons' do
        create(:label, project: project, title: 'scoped::label')

        note = find('#note-body')
        type(note, '~scoped::')

        wait_for_requests

        expect(find('.tribute-container ul', visible: true).text).to have_content('scoped::label')
      end

      it 'autocompletes multi-word labels' do
        create(:label, project: project, title: 'Accepting merge requests')

        note = find('#note-body')
        type(note, '~Acceptingmerge')

        wait_for_requests

        expect(find('.tribute-container ul', visible: true).text).to have_content('Accepting merge requests')
      end

      it 'only autocompletes the latest label' do
        create(:label, project: project, title: 'documentation')
        create(:label, project: project, title: 'feature')

        note = find('#note-body')
        type(note, '~documentation foo bar ~feat')
        note.native.send_keys(:right)

        wait_for_requests

        expect(find('.tribute-container ul', visible: true).text).to have_content('feature')
        expect(find('.tribute-container ul', visible: true).text).not_to have_content('documentation')
      end

      it 'does not autocomplete labels if no tilde is typed' do
        create(:label, project: project, title: 'documentation')

        note = find('#note-body')
        type(note, 'document')

        wait_for_requests

        expect(page).not_to have_selector('.tribute-container')
      end
    end

    shared_examples 'autocomplete suggestions' do
      it 'suggests objects correctly' do
        page.within '.timeline-content-form' do
          find('#note-body').native.send_keys(object.class.reference_prefix)
        end

        page.within '.tribute-container' do
          expect(page).to have_content(object.title)

          find('ul li').click
        end

        expect(find('.new-note #note-body').value).to include(expected_body)
      end
    end

    context 'merge requests' do
      let(:object) { create(:merge_request, source_project: project) }
      let(:expected_body) { object.to_reference }

      it_behaves_like 'autocomplete suggestions'
    end

    context 'when other notes are destroyed' do
      let!(:discussion) { create(:discussion_note_on_issue, noteable: issue, project: issue.project) }

      # This is meant to protect against this issue https://gitlab.com/gitlab-org/gitlab/-/issues/228729
      it 'keeps autocomplete key listeners' do
        visit project_issue_path(project, issue)
        note = find('#note-body')

        start_comment_with_emoji(note)

        start_and_cancel_discussion

        note.fill_in(with: '')
        start_comment_with_emoji(note)
        note.native.send_keys(:enter)

        expect(note.value).to eql('Hello :100: ')
      end

      def start_comment_with_emoji(note)
        note.native.send_keys('Hello :10')

        wait_for_requests

        find('.atwho-view li', text: '100')
      end

      def start_and_cancel_discussion
        click_button('Reply...')

        fill_in('note_note', with: 'Whoops!')

        page.accept_alert 'Are you sure you want to cancel creating this comment?' do
          click_button('Cancel')
        end

        wait_for_requests
      end
    end
  end

  private

  def expect_to_wrap(should_wrap, item, note, value)
    expect(item).to have_content(value)
    expect(item).not_to have_content("\"#{value}\"")

    item.click

    if should_wrap
      expect(note.value).to include("\"#{value}\"")
    else
      expect(note.value).not_to include("\"#{value}\"")
    end
  end

  def expect_labels(shown: nil, not_shown: nil)
    page.within('.atwho-container') do
      if shown
        expect(page).to have_selector('.atwho-view li', count: shown.size)
        shown.each { |label| expect(page).to have_content(label.title) }
      end

      if not_shown
        expect(page).not_to have_selector('.atwho-view li') unless shown
        not_shown.each { |label| expect(page).not_to have_content(label.title) }
      end
    end
  end

  # `note` is a textarea where the given text should be typed.
  # We don't want to find it each time this function gets called.
  def type(note, text)
    page.within('.timeline-content-form') do
      note.set('')
      note.native.send_keys(text)
    end
  end
end