summaryrefslogtreecommitdiff
path: root/spec/features/issues/filtered_search/filter_issues_spec.rb
blob: 2f880c926e7efdb492547d8215f133e62a8de7fd (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
require 'spec_helper'

describe 'Filter issues', js: true, feature: true do
  include Devise::Test::IntegrationHelpers
  include FilteredSearchHelpers
  include WaitForAjax

  let!(:group) { create(:group) }
  let!(:project) { create(:project, group: group) }
  let!(:user) { create(:user, username: 'joe') }
  let!(:user2) { create(:user, username: 'jane') }
  let!(:label) { create(:label, project: project) }
  let!(:wontfix) { create(:label, project: project, title: "Won't fix") }

  let!(:bug_label) { create(:label, project: project, title: 'bug') }
  let!(:caps_sensitive_label) { create(:label, project: project, title: 'CAPS_sensitive') }
  let!(:milestone) { create(:milestone, title: "8", project: project, start_date: 2.days.ago) }
  let!(:multiple_words_label) { create(:label, project: project, title: "Two words") }

  let!(:closed_issue) { create(:issue, title: 'bug that is closed', project: project, state: :closed) }

  def expect_no_issues_list
    page.within '.issues-list' do
      expect(page).not_to have_selector('.issue')
    end
  end

  def expect_issues_list_count(open_count, closed_count = 0)
    all_count = open_count + closed_count

    expect(page).to have_issuable_counts(open: open_count, closed: closed_count, all: all_count)
    page.within '.issues-list' do
      expect(page).to have_selector('.issue', count: open_count)
    end
  end

  def select_search_at_index(pos)
    evaluate_script("el = document.querySelector('.filtered-search'); el.focus(); el.setSelectionRange(#{pos}, #{pos});")
  end

  before do
    project.team << [user, :master]
    project.team << [user2, :master]
    group.add_developer(user)
    group.add_developer(user2)

    sign_in(user)

    create(:issue, project: project)
    create(:issue, project: project, title: "Bug report 1")
    create(:issue, project: project, title: "Bug report 2")
    create(:issue, project: project, title: "issue with 'single quotes'")
    create(:issue, project: project, title: "issue with \"double quotes\"")
    create(:issue, project: project, title: "issue with !@\#{$%^&*()-+")
    create(:issue, project: project, title: "issue by assignee", milestone: milestone, author: user, assignee: user)
    create(:issue, project: project, title: "issue by assignee with searchTerm", milestone: milestone, author: user, assignee: user)

    issue = create(:issue,
      title: "Bug 2",
      project: project,
      milestone: milestone,
      author: user,
      assignee: user)
    issue.labels << bug_label

    issue_with_caps_label = create(:issue,
      title: "issue by assignee with searchTerm and label",
      project: project,
      milestone: milestone,
      author: user,
      assignee: user)
    issue_with_caps_label.labels << caps_sensitive_label

    issue_with_everything = create(:issue,
      title: "Bug report foo was possible",
      project: project,
      milestone: milestone,
      author: user,
      assignee: user)
    issue_with_everything.labels << bug_label
    issue_with_everything.labels << caps_sensitive_label

    multiple_words_label_issue = create(:issue, title: "Issue with multiple words label", project: project)
    multiple_words_label_issue.labels << multiple_words_label

    future_milestone = create(:milestone, title: "future", project: project, due_date: Time.now + 1.month)

    create(:issue,
      title: "Issue with future milestone",
      milestone: future_milestone,
      project: project)

    visit namespace_project_issues_path(project.namespace, project)
  end

  describe 'filter issues by author' do
    context 'only author' do
      it 'filters issues by searched author' do
        input_filtered_search("author:@#{user.username}")

        expect_tokens([{ name: 'author', value: user.username }])
        expect_issues_list_count(5)
        expect_filtered_search_input_empty
      end

      it 'filters issues by invalid author' do
        skip('to be tested, issue #26546')
      end

      it 'filters issues by multiple authors' do
        skip('to be tested, issue #26546')
      end
    end

    context 'author with other filters' do
      let(:search_term) { 'issue' }

      it 'filters issues by searched author and text' do
        input_filtered_search("author:@#{user.username} #{search_term}")

        expect_tokens([{ name: 'author', value: user.username }])
        expect_issues_list_count(3)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched author, assignee and text' do
        input_filtered_search("author:@#{user.username} assignee:@#{user.username} #{search_term}")

        expect_tokens([
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username }
        ])
        expect_issues_list_count(3)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched author, assignee, label, and text' do
        input_filtered_search("author:@#{user.username} assignee:@#{user.username} label:~#{caps_sensitive_label.title} #{search_term}")

        expect_tokens([
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username },
          { name: 'label', value: caps_sensitive_label.title }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched author, assignee, label, milestone and text' do
        input_filtered_search("author:@#{user.username} assignee:@#{user.username} label:~#{caps_sensitive_label.title} milestone:%#{milestone.title} #{search_term}")

        expect_tokens([
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username },
          { name: 'label', value: caps_sensitive_label.title },
          { name: 'milestone', value: milestone.title }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end
    end

    it 'sorting' do
      skip('to be tested, issue #26546')
    end
  end

  describe 'filter issues by assignee' do
    context 'only assignee' do
      it 'filters issues by searched assignee' do
        input_filtered_search("assignee:@#{user.username}")

        expect_tokens([{ name: 'assignee', value: user.username }])
        expect_issues_list_count(5)
        expect_filtered_search_input_empty
      end

      it 'filters issues by no assignee' do
        input_filtered_search('assignee:none')

        expect_tokens([{ name: 'assignee', value: 'none' }])
        expect_issues_list_count(8, 1)
        expect_filtered_search_input_empty
      end

      it 'filters issues by invalid assignee' do
        skip('to be tested, issue #26546')
      end

      it 'filters issues by multiple assignees' do
        skip('to be tested, issue #26546')
      end
    end

    context 'assignee with other filters' do
      let(:search_term) { 'searchTerm' }

      it 'filters issues by searched assignee and text' do
        input_filtered_search("assignee:@#{user.username} #{search_term}")

        expect_tokens([{ name: 'assignee', value: user.username }])
        expect_issues_list_count(2)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched assignee, author and text' do
        input_filtered_search("assignee:@#{user.username} author:@#{user.username} #{search_term}")

        expect_tokens([
          { name: 'assignee', value: user.username },
          { name: 'author', value: user.username }
        ])
        expect_issues_list_count(2)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched assignee, author, label, text' do
        input_filtered_search("assignee:@#{user.username} author:@#{user.username} label:~#{caps_sensitive_label.title} #{search_term}")

        expect_tokens([
          { name: 'assignee', value: user.username },
          { name: 'author', value: user.username },
          { name: 'label', value: caps_sensitive_label.title }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched assignee, author, label, milestone and text' do
        input_filtered_search("assignee:@#{user.username} author:@#{user.username} label:~#{caps_sensitive_label.title} milestone:%#{milestone.title} #{search_term}")

        expect_tokens([
          { name: 'assignee', value: user.username },
          { name: 'author', value: user.username },
          { name: 'label', value: caps_sensitive_label.title },
          { name: 'milestone', value: milestone.title }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end
    end

    context 'sorting' do
      it 'sorts' do
        skip('to be tested, issue #26546')
      end
    end
  end

  describe 'filter issues by label' do
    let(:search_term) { 'bug' }

    context 'only label' do
      it 'filters issues by searched label' do
        input_filtered_search("label:~#{bug_label.title}")

        expect_tokens([{ name: 'label', value: bug_label.title }])
        expect_issues_list_count(2)
        expect_filtered_search_input_empty
      end

      it 'filters issues by no label' do
        input_filtered_search('label:none')

        expect_tokens([{ name: 'label', value: 'none' }])
        expect_issues_list_count(9, 1)
        expect_filtered_search_input_empty
      end

      it 'filters issues by invalid label' do
        skip('to be tested, issue #26546')
      end

      it 'filters issues by multiple labels' do
        input_filtered_search("label:~#{bug_label.title} label:~#{caps_sensitive_label.title}")

        expect_tokens([
          { name: 'label', value: bug_label.title },
          { name: 'label', value: caps_sensitive_label.title }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input_empty
      end

      it 'filters issues by label containing special characters' do
        special_label = create(:label, project: project, title: '!@#{$%^&*()-+[]<>?/:{}|\}')
        special_issue = create(:issue, title: "Issue with special character label", project: project)
        special_issue.labels << special_label

        input_filtered_search("label:~#{special_label.title}")
        expect_tokens([{ name: 'label', value: special_label.title }])
        expect_issues_list_count(1)
        expect_filtered_search_input_empty
      end

      it 'does not show issues' do
        new_label = create(:label, project: project, title: 'new_label')

        input_filtered_search("label:~#{new_label.title}")

        expect_tokens([{ name: 'label', value: new_label.title }])
        expect_no_issues_list()
        expect_filtered_search_input_empty
      end
    end

    context 'label with multiple words' do
      it 'special characters' do
        special_multiple_label = create(:label, project: project, title: "Utmost |mp0rt@nce")
        special_multiple_issue = create(:issue, title: "Issue with special character multiple words label", project: project)
        special_multiple_issue.labels << special_multiple_label

        input_filtered_search("label:~'#{special_multiple_label.title}'")

        # filtered search defaults quotations to double quotes
        expect_tokens([{ name: 'label', value: "\"#{special_multiple_label.title}\"" }])
        expect_issues_list_count(1)

        expect_filtered_search_input_empty
      end

      it 'single quotes' do
        input_filtered_search("label:~'#{multiple_words_label.title}'")

        expect_tokens([{ name: 'label', value: "\"#{multiple_words_label.title}\"" }])
        expect_issues_list_count(1)
        expect_filtered_search_input_empty
      end

      it 'double quotes' do
        input_filtered_search("label:~\"#{multiple_words_label.title}\"")

        expect_tokens([{ name: 'label', value: "\"#{multiple_words_label.title}\"" }])
        expect_issues_list_count(1)
        expect_filtered_search_input_empty
      end

      it 'single quotes containing double quotes' do
        double_quotes_label = create(:label, project: project, title: 'won"t fix')
        double_quotes_label_issue = create(:issue, title: "Issue with double quotes label", project: project)
        double_quotes_label_issue.labels << double_quotes_label

        input_filtered_search("label:~'#{double_quotes_label.title}'")

        expect_tokens([{ name: 'label', value: "'#{double_quotes_label.title}'" }])
        expect_issues_list_count(1)
        expect_filtered_search_input_empty
      end

      it 'double quotes containing single quotes' do
        single_quotes_label = create(:label, project: project, title: "won't fix")
        single_quotes_label_issue = create(:issue, title: "Issue with single quotes label", project: project)
        single_quotes_label_issue.labels << single_quotes_label

        input_filtered_search("label:~\"#{single_quotes_label.title}\"")

        expect_tokens([{ name: 'label', value: "\"#{single_quotes_label.title}\"" }])
        expect_issues_list_count(1)
        expect_filtered_search_input_empty
      end
    end

    context 'label with other filters' do
      it 'filters issues by searched label and text' do
        input_filtered_search("label:~#{caps_sensitive_label.title} #{search_term}")

        expect_tokens([{ name: 'label', value: caps_sensitive_label.title }])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched label, author and text' do
        input_filtered_search("label:~#{caps_sensitive_label.title} author:@#{user.username} #{search_term}")

        expect_tokens([
          { name: 'label', value: caps_sensitive_label.title },
          { name: 'author', value: user.username }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched label, author, assignee and text' do
        input_filtered_search("label:~#{caps_sensitive_label.title} author:@#{user.username} assignee:@#{user.username} #{search_term}")

        expect_tokens([
          { name: 'label', value: caps_sensitive_label.title },
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched label, author, assignee, milestone and text' do
        input_filtered_search("label:~#{caps_sensitive_label.title} author:@#{user.username} assignee:@#{user.username} milestone:%#{milestone.title} #{search_term}")

        expect_tokens([
          { name: 'label', value: caps_sensitive_label.title },
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username },
          { name: 'milestone', value: milestone.title }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end
    end

    context 'multiple labels with other filters' do
      it 'filters issues by searched label, label2, and text' do
        input_filtered_search("label:~#{bug_label.title} label:~#{caps_sensitive_label.title} #{search_term}")

        expect_tokens([
          { name: 'label', value: bug_label.title },
          { name: 'label', value: caps_sensitive_label.title }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched label, label2, author and text' do
        input_filtered_search("label:~#{bug_label.title} label:~#{caps_sensitive_label.title} author:@#{user.username} #{search_term}")

        expect_tokens([
          { name: 'label', value: bug_label.title },
          { name: 'label', value: caps_sensitive_label.title },
          { name: 'author', value: user.username }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched label, label2, author, assignee and text' do
        input_filtered_search("label:~#{bug_label.title} label:~#{caps_sensitive_label.title} author:@#{user.username} assignee:@#{user.username} #{search_term}")

        expect_tokens([
          { name: 'label', value: bug_label.title },
          { name: 'label', value: caps_sensitive_label.title },
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched label, label2, author, assignee, milestone and text' do
        input_filtered_search("label:~#{bug_label.title} label:~#{caps_sensitive_label.title} author:@#{user.username} assignee:@#{user.username} milestone:%#{milestone.title} #{search_term}")

        expect_tokens([
          { name: 'label', value: bug_label.title },
          { name: 'label', value: caps_sensitive_label.title },
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username },
          { name: 'milestone', value: milestone.title }
        ])
        expect_issues_list_count(1)
        expect_filtered_search_input(search_term)
      end
    end

    context 'issue label clicked' do
      before do
        find('.issues-list .issue .issue-info a .label', text: multiple_words_label.title).click
      end

      it 'filters' do
        expect_issues_list_count(1)
      end

      it 'displays in search bar' do
        expect_tokens([{ name: 'label', value: "\"#{multiple_words_label.title}\"" }])
        expect_filtered_search_input_empty
      end
    end

    context 'sorting' do
      it 'sorts' do
        skip('to be tested, issue #26546')
      end
    end
  end

  describe 'filter issues by milestone' do
    context 'only milestone' do
      it 'filters issues by searched milestone' do
        input_filtered_search("milestone:%#{milestone.title}")

        expect_tokens([{ name: 'milestone', value: milestone.title }])
        expect_issues_list_count(5)
        expect_filtered_search_input_empty
      end

      it 'filters issues by no milestone' do
        input_filtered_search("milestone:none")

        expect_tokens([{ name: 'milestone', value: 'none' }])
        expect_issues_list_count(7, 1)
        expect_filtered_search_input_empty
      end

      it 'filters issues by upcoming milestones' do
        input_filtered_search("milestone:upcoming")

        expect_tokens([{ name: 'milestone', value: 'upcoming' }])
        expect_issues_list_count(1)
        expect_filtered_search_input_empty
      end

      it 'filters issues by started milestones' do
        input_filtered_search("milestone:started")

        expect_tokens([{ name: 'milestone', value: 'started' }])
        expect_issues_list_count(5)
        expect_filtered_search_input_empty
      end

      it 'filters issues by invalid milestones' do
        skip('to be tested, issue #26546')
      end

      it 'filters issues by multiple milestones' do
        skip('to be tested, issue #26546')
      end

      it 'filters issues by milestone containing special characters' do
        special_milestone = create(:milestone, title: '!@\#{$%^&*()}', project: project)
        create(:issue, title: "Issue with special character milestone", project: project, milestone: special_milestone)

        input_filtered_search("milestone:%#{special_milestone.title}")

        expect_tokens([{ name: 'milestone', value: special_milestone.title }])
        expect_issues_list_count(1)
        expect_filtered_search_input_empty
      end

      it 'does not show issues' do
        new_milestone = create(:milestone, title: "new", project: project)

        input_filtered_search("milestone:%#{new_milestone.title}")

        expect_tokens([{ name: 'milestone', value: new_milestone.title }])
        expect_no_issues_list()
        expect_filtered_search_input_empty
      end
    end

    context 'milestone with other filters' do
      let(:search_term) { 'bug' }

      it 'filters issues by searched milestone and text' do
        input_filtered_search("milestone:%#{milestone.title} #{search_term}")

        expect_tokens([{ name: 'milestone', value: milestone.title }])
        expect_issues_list_count(2)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched milestone, author and text' do
        input_filtered_search("milestone:%#{milestone.title} author:@#{user.username} #{search_term}")

        expect_tokens([
          { name: 'milestone', value: milestone.title },
          { name: 'author', value: user.username }
        ])
        expect_issues_list_count(2)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched milestone, author, assignee and text' do
        input_filtered_search("milestone:%#{milestone.title} author:@#{user.username} assignee:@#{user.username} #{search_term}")

        expect_tokens([
          { name: 'milestone', value: milestone.title },
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username }
        ])
        expect_issues_list_count(2)
        expect_filtered_search_input(search_term)
      end

      it 'filters issues by searched milestone, author, assignee, label and text' do
        input_filtered_search("milestone:%#{milestone.title} author:@#{user.username} assignee:@#{user.username} label:~#{bug_label.title} #{search_term}")

        expect_tokens([
          { name: 'milestone', value: milestone.title },
          { name: 'author', value: user.username },
          { name: 'assignee', value: user.username },
          { name: 'label', value: bug_label.title }
        ])
        expect_issues_list_count(2)
        expect_filtered_search_input(search_term)
      end
    end

    context 'sorting' do
      it 'sorts' do
        skip('to be tested, issue #26546')
      end
    end
  end

  describe 'filter issues by text' do
    context 'only text' do
      it 'filters issues by searched text' do
        search = 'Bug'
        input_filtered_search(search)

        expect_issues_list_count(4, 1)
        expect_filtered_search_input(search)
      end

      it 'filters issues by multiple searched text' do
        search = 'Bug report'
        input_filtered_search(search)

        expect_issues_list_count(3)
        expect_filtered_search_input(search)
      end

      it 'filters issues by case insensitive searched text' do
        search = 'bug report'
        input_filtered_search(search)

        expect_issues_list_count(3)
        expect_filtered_search_input(search)
      end

      it 'filters issues by searched text containing single quotes' do
        search = '\'single quotes\''
        input_filtered_search(search)

        expect_issues_list_count(1)
        expect_filtered_search_input(search)
      end

      it 'filters issues by searched text containing double quotes' do
        search = '"double quotes"'
        input_filtered_search(search)

        expect_issues_list_count(1)
        expect_filtered_search_input(search)
      end

      it 'filters issues by searched text containing special characters' do
        search = '!@#{$%^&*()-+'
        input_filtered_search(search)

        expect_issues_list_count(1)
        expect_filtered_search_input(search)
      end

      it 'does not show any issues' do
        search = 'testing'
        input_filtered_search(search)

        expect_no_issues_list()
        expect_filtered_search_input(search)
      end
    end

    context 'searched text with other filters' do
      it 'filters issues by searched text and author' do
        # After searching, all search terms are placed at the end
        input_filtered_search("bug author:@#{user.username}")

        expect_issues_list_count(2)
        expect_filtered_search_input('bug')
      end

      it 'filters issues by searched text, author and more text' do
        input_filtered_search("bug author:@#{user.username} report")

        expect_issues_list_count(1)
        expect_filtered_search_input('bug report')
      end

      it 'filters issues by searched text, author and assignee' do
        input_filtered_search("bug author:@#{user.username} assignee:@#{user.username}")

        expect_issues_list_count(2)
        expect_filtered_search_input('bug')
      end

      it 'filters issues by searched text, author, more text and assignee' do
        input_filtered_search("bug author:@#{user.username} report assignee:@#{user.username}")

        expect_issues_list_count(1)
        expect_filtered_search_input('bug report')
      end

      it 'filters issues by searched text, author, more text, assignee and even more text' do
        input_filtered_search("bug author:@#{user.username} report assignee:@#{user.username} foo")

        expect_issues_list_count(1)
        expect_filtered_search_input('bug report foo')
      end

      it 'filters issues by searched text, author, assignee and label' do
        input_filtered_search("bug author:@#{user.username} assignee:@#{user.username} label:~#{bug_label.title}")

        expect_issues_list_count(2)
        expect_filtered_search_input('bug')
      end

      it 'filters issues by searched text, author, text, assignee, text, label and text' do
        input_filtered_search("bug author:@#{user.username} assignee:@#{user.username} report label:~#{bug_label.title} foo")

        expect_issues_list_count(1)
        expect_filtered_search_input('bug report foo')
      end

      it 'filters issues by searched text, author, assignee, label and milestone' do
        input_filtered_search("bug author:@#{user.username} assignee:@#{user.username} label:~#{bug_label.title} milestone:%#{milestone.title}")

        expect_issues_list_count(2)
        expect_filtered_search_input('bug')
      end

      it 'filters issues by searched text, author, text, assignee, text, label, text, milestone and text' do
        input_filtered_search("bug author:@#{user.username} assignee:@#{user.username} report label:~#{bug_label.title} milestone:%#{milestone.title} foo")

        expect_issues_list_count(1)
        expect_filtered_search_input('bug report foo')
      end

      it 'filters issues by searched text, author, assignee, multiple labels and milestone' do
        input_filtered_search("bug author:@#{user.username} assignee:@#{user.username} label:~#{bug_label.title} label:~#{caps_sensitive_label.title} milestone:%#{milestone.title}")

        expect_issues_list_count(1)
        expect_filtered_search_input('bug')
      end

      it 'filters issues by searched text, author, text, assignee, text, label1, text, label2, text, milestone and text' do
        input_filtered_search("bug author:@#{user.username} assignee:@#{user.username} report label:~#{bug_label.title} label:~#{caps_sensitive_label.title} milestone:%#{milestone.title} foo")

        expect_issues_list_count(1)
        expect_filtered_search_input('bug report foo')
      end
    end

    context 'sorting' do
      it 'sorts by oldest updated' do
        create(:issue,
          title: '3 days ago',
          project: project,
          author: user,
          created_at: 3.days.ago,
          updated_at: 3.days.ago)

        old_issue = create(:issue,
          title: '5 days ago',
          project: project,
          author: user,
          created_at: 5.days.ago,
          updated_at: 5.days.ago)

        input_filtered_search('days ago')

        expect_issues_list_count(2)

        sort_toggle = find('.filtered-search-container .dropdown-toggle')
        sort_toggle.click

        find('.filtered-search-container .dropdown-menu li a', text: 'Oldest updated').click
        wait_for_ajax

        expect(find('.issues-list .issue:first-of-type .issue-title-text a')).to have_content(old_issue.title)
      end
    end
  end

  describe 'retains filter when switching issue states' do
    before do
      input_filtered_search('bug')

      # This ensures that the search is performed
      expect_issues_list_count(4, 1)
    end

    it 'open state' do
      find('.issues-state-filters a', text: 'Closed').click
      wait_for_ajax

      find('.issues-state-filters a', text: 'Open').click
      wait_for_ajax

      expect(page).to have_selector('.issues-list .issue', count: 4)
    end

    it 'closed state' do
      find('.issues-state-filters a', text: 'Closed').click
      wait_for_ajax

      expect(page).to have_selector('.issues-list .issue', count: 1)
      expect(find('.issues-list .issue:first-of-type .issue-title-text a')).to have_content(closed_issue.title)
    end

    it 'all state' do
      find('.issues-state-filters a', text: 'All').click
      wait_for_ajax

      expect(page).to have_selector('.issues-list .issue', count: 5)
    end
  end

  describe 'RSS feeds' do
    it 'updates atom feed link for project issues' do
      visit namespace_project_issues_path(project.namespace, project, milestone_title: milestone.title, assignee_id: user.id)
      link = find_link('Subscribe')
      params = CGI.parse(URI.parse(link[:href]).query)
      auto_discovery_link = find('link[type="application/atom+xml"]', visible: false)
      auto_discovery_params = CGI.parse(URI.parse(auto_discovery_link[:href]).query)

      expect(params).to include('private_token' => [user.private_token])
      expect(params).to include('milestone_title' => [milestone.title])
      expect(params).to include('assignee_id' => [user.id.to_s])
      expect(auto_discovery_params).to include('private_token' => [user.private_token])
      expect(auto_discovery_params).to include('milestone_title' => [milestone.title])
      expect(auto_discovery_params).to include('assignee_id' => [user.id.to_s])
    end

    it 'updates atom feed link for group issues' do
      visit issues_group_path(group, milestone_title: milestone.title, assignee_id: user.id)
      link = find('.nav-controls a', text: 'Subscribe')
      params = CGI.parse(URI.parse(link[:href]).query)
      auto_discovery_link = find('link[type="application/atom+xml"]', visible: false)
      auto_discovery_params = CGI.parse(URI.parse(auto_discovery_link[:href]).query)

      expect(params).to include('private_token' => [user.private_token])
      expect(params).to include('milestone_title' => [milestone.title])
      expect(params).to include('assignee_id' => [user.id.to_s])
      expect(auto_discovery_params).to include('private_token' => [user.private_token])
      expect(auto_discovery_params).to include('milestone_title' => [milestone.title])
      expect(auto_discovery_params).to include('assignee_id' => [user.id.to_s])
    end
  end

  context 'URL has a trailing slash' do
    before do
      visit "#{namespace_project_issues_path(project.namespace, project)}/"
    end

    it 'milestone dropdown loads milestones' do
      input_filtered_search("milestone:", submit: false)

      within('#js-dropdown-milestone') do
        expect(page).to have_selector('.filter-dropdown .filter-dropdown-item', count: 2)
      end
    end

    it 'label dropdown load labels' do
      input_filtered_search("label:", submit: false)

      within('#js-dropdown-label') do
        expect(page).to have_selector('.filter-dropdown .filter-dropdown-item', count: 5)
      end
    end
  end
end