summaryrefslogtreecommitdiff
path: root/spec/features/search/user_searches_for_code_spec.rb
blob: 14d67bac85fe95574aced500e707516892f35410 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'User searches for code', :js, :disable_rate_limiter, feature_category: :global_search do
  using RSpec::Parameterized::TableSyntax

  let_it_be(:user) { create(:user) }
  let_it_be_with_reload(:project) { create(:project, :repository, namespace: user.namespace) }

  where(search_page_vertical_nav_enabled: [true, false])
  with_them do
    context 'when signed in' do
      before do
        stub_feature_flags(search_page_vertical_nav: search_page_vertical_nav_enabled)
        project.add_maintainer(user)
        sign_in(user)
      end

      it 'finds a file' do
        visit(project_path(project))

        submit_search('application.js')
        select_search_scope('Code')

        expect(page).to have_selector('.results', text: 'application.js')
        expect(page).to have_selector('.file-content .code')
        expect(page).to have_selector("span.line[lang='javascript']")
        expect(page).to have_link('application.js', href: %r{master/files/js/application.js})
        expect(page).to have_button('Copy file path')
      end

      context 'when on a project page' do
        before do
          visit(search_path)
          find('[data-testid="project-filter"]').click

          wait_for_requests

          page.within('[data-testid="project-filter"]') do
            click_on(project.name)
          end
        end

        include_examples 'top right search form'
        include_examples 'search timeouts', 'blobs' do
          let(:additional_params) { { project_id: project.id } }
        end

        it 'finds code and links to blob' do
          expected_result = 'Update capybara, rspec-rails, poltergeist to recent versions'

          fill_in('dashboard_search', with: 'rspec')
          find('.gl-search-box-by-click-search-button').click

          expect(page).to have_selector('.results', text: expected_result)

          find("#blob-L3").click
          expect(current_url).to match(%r{blob/master/.gitignore#L3})
        end

        it 'finds code and links to blame' do
          expected_result = 'Update capybara, rspec-rails, poltergeist to recent versions'

          fill_in('dashboard_search', with: 'rspec')
          find('.gl-search-box-by-click-search-button').click

          expect(page).to have_selector('.results', text: expected_result)

          find("#blame-L3").click
          expect(current_url).to match(%r{blame/master/.gitignore#L3})
        end

        it 'search multiple words with refs switching' do
          expected_result = 'Use `snake_case` for naming files'
          search = 'for naming files'

          fill_in('dashboard_search', with: search)
          find('.gl-search-box-by-click-search-button').click

          expect(page).to have_selector('.results', text: expected_result)

          find('.ref-selector').click
          wait_for_requests

          page.within('.ref-selector') do
            find('li', text: 'v1.0.0').click
          end

          expect(page).to have_selector('.results', text: expected_result)

          expect(find_field('dashboard_search').value).to eq(search)
          expect(find("#blob-L1502")[:href]).to match(%r{blob/v1.0.0/files/markdown/ruby-style-guide.md#L1502})
          expect(find("#blame-L1502")[:href]).to match(%r{blame/v1.0.0/files/markdown/ruby-style-guide.md#L1502})
        end
      end

      context 'when :new_header_search is true' do
        context 'search code within refs' do
          let(:ref_name) { 'v1.0.0' }

          before do
            # This feature is disabled by default in spec_helper.rb.
            # We missed a feature breaking bug, so to prevent this regression, testing both scenarios for this spec.
            # This can be removed as part of closing https://gitlab.com/gitlab-org/gitlab/-/issues/339348.
            stub_feature_flags(new_header_search: true)
            visit(project_tree_path(project, ref_name))

            submit_search('gitlab-grack')
            select_search_scope('Code')
          end

          it 'shows ref switcher in code result summary' do
            expect(find('.ref-selector')).to have_text(ref_name)
          end

          it 'persists branch name across search' do
            find('.gl-search-box-by-click-search-button').click
            expect(find('.ref-selector')).to have_text(ref_name)
          end

          #  this example is use to test the design that the refs is not
          #  only represent the branch as well as the tags.
          it 'ref switcher list all the branches and tags' do
            find('.ref-selector').click
            wait_for_requests

            page.within('.ref-selector') do
              expect(page).to have_selector('li', text: 'add-ipython-files')
              expect(page).to have_selector('li', text: 'v1.0.0')
            end
          end

          it 'search result changes when refs switched' do
            ref = 'master'
            expect(find('.results')).not_to have_content('path = gitlab-grack')

            find('.ref-selector').click
            wait_for_requests

            page.within('.ref-selector') do
              fill_in _('Search by Git revision'), with: ref
              wait_for_requests

              find('li', text: ref).click
            end

            expect(page).to have_selector('.results', text: 'path = gitlab-grack')
          end
        end
      end

      context 'when :new_header_search is false' do
        context 'search code within refs' do
          let(:ref_name) { 'v1.0.0' }

          before do
            # This feature is disabled by default in spec_helper.rb.
            # We missed a feature breaking bug, so to prevent this regression, testing both scenarios for this spec.
            # This can be removed as part of closing https://gitlab.com/gitlab-org/gitlab/-/issues/339348.
            stub_feature_flags(new_header_search: false)
            visit(project_tree_path(project, ref_name))

            submit_search('gitlab-grack')
            select_search_scope('Code')
          end

          it 'shows ref switcher in code result summary' do
            expect(find('.ref-selector')).to have_text(ref_name)
          end

          it 'persists branch name across search' do
            find('.gl-search-box-by-click-search-button').click
            expect(find('.ref-selector')).to have_text(ref_name)
          end

          #  this example is use to test the design that the refs is not
          #  only represent the branch as well as the tags.
          it 'ref switcher list all the branches and tags' do
            find('.ref-selector').click
            wait_for_requests

            page.within('.ref-selector') do
              expect(page).to have_selector('li', text: 'add-ipython-files')
              expect(page).to have_selector('li', text: 'v1.0.0')
            end
          end

          it 'search result changes when refs switched' do
            ref = 'master'
            expect(find('.results')).not_to have_content('path = gitlab-grack')

            find('.ref-selector').click
            wait_for_requests

            page.within('.ref-selector') do
              fill_in _('Search by Git revision'), with: ref
              wait_for_requests

              find('li', text: ref).click
            end

            expect(page).to have_selector('.results', text: 'path = gitlab-grack')
          end
        end
      end

      it 'no ref switcher shown in issue result summary' do
        issue = create(:issue, title: 'test', project: project)
        visit(project_tree_path(project))

        submit_search('test')
        select_search_scope('Code')

        expect(page).to have_selector('.ref-selector')

        select_search_scope('Issues')

        expect(find(:css, '.results')).to have_link(issue.title)
        expect(page).not_to have_selector('.ref-selector')
      end
    end

    context 'when signed out' do
      before do
        stub_feature_flags(search_page_vertical_nav: search_page_vertical_nav_enabled)
      end

      context 'when block_anonymous_global_searches is enabled' do
        it 'is redirected to login page' do
          visit(search_path)

          expect(page).to have_content('You must be logged in to search across all of GitLab')
        end
      end
    end
  end
end