summaryrefslogtreecommitdiff
path: root/spec/helpers/search_helper_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/helpers/search_helper_spec.rb')
-rw-r--r--spec/helpers/search_helper_spec.rb82
1 files changed, 82 insertions, 0 deletions
diff --git a/spec/helpers/search_helper_spec.rb b/spec/helpers/search_helper_spec.rb
index 699232e67b1..594c5c11994 100644
--- a/spec/helpers/search_helper_spec.rb
+++ b/spec/helpers/search_helper_spec.rb
@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe SearchHelper do
+ include MarkupHelper
+
# Override simple_sanitize for our testing purposes
def simple_sanitize(str)
str
@@ -71,6 +73,72 @@ RSpec.describe SearchHelper do
expect(result.keys).to match_array(%i[category id label url avatar_url])
end
+ it 'includes the first 5 of the users recent issues' do
+ recent_issues = instance_double(::Gitlab::Search::RecentIssues)
+ expect(::Gitlab::Search::RecentIssues).to receive(:new).with(user: user).and_return(recent_issues)
+ project1 = create(:project, :with_avatar, namespace: user.namespace)
+ project2 = create(:project, namespace: user.namespace)
+ issue1 = create(:issue, title: 'issue 1', project: project1)
+ issue2 = create(:issue, title: 'issue 2', project: project2)
+
+ other_issues = create_list(:issue, 5)
+
+ expect(recent_issues).to receive(:search).with('the search term').and_return(Issue.id_in_ordered([issue1.id, issue2.id, *other_issues.map(&:id)]))
+
+ results = search_autocomplete_opts("the search term")
+
+ expect(results.count).to eq(5)
+
+ expect(results[0]).to include({
+ category: 'Recent issues',
+ id: issue1.id,
+ label: 'issue 1',
+ url: Gitlab::Routing.url_helpers.project_issue_path(issue1.project, issue1),
+ avatar_url: project1.avatar_url
+ })
+
+ expect(results[1]).to include({
+ category: 'Recent issues',
+ id: issue2.id,
+ label: 'issue 2',
+ url: Gitlab::Routing.url_helpers.project_issue_path(issue2.project, issue2),
+ avatar_url: '' # This project didn't have an avatar so set this to ''
+ })
+ end
+
+ it 'includes the first 5 of the users recent merge requests' do
+ recent_merge_requests = instance_double(::Gitlab::Search::RecentMergeRequests)
+ expect(::Gitlab::Search::RecentMergeRequests).to receive(:new).with(user: user).and_return(recent_merge_requests)
+ project1 = create(:project, :with_avatar, namespace: user.namespace)
+ project2 = create(:project, namespace: user.namespace)
+ merge_request1 = create(:merge_request, :unique_branches, title: 'Merge request 1', target_project: project1, source_project: project1)
+ merge_request2 = create(:merge_request, :unique_branches, title: 'Merge request 2', target_project: project2, source_project: project2)
+
+ other_merge_requests = create_list(:merge_request, 5)
+
+ expect(recent_merge_requests).to receive(:search).with('the search term').and_return(MergeRequest.id_in_ordered([merge_request1.id, merge_request2.id, *other_merge_requests.map(&:id)]))
+
+ results = search_autocomplete_opts("the search term")
+
+ expect(results.count).to eq(5)
+
+ expect(results[0]).to include({
+ category: 'Recent merge requests',
+ id: merge_request1.id,
+ label: 'Merge request 1',
+ url: Gitlab::Routing.url_helpers.project_merge_request_path(merge_request1.project, merge_request1),
+ avatar_url: project1.avatar_url
+ })
+
+ expect(results[1]).to include({
+ category: 'Recent merge requests',
+ id: merge_request2.id,
+ label: 'Merge request 2',
+ url: Gitlab::Routing.url_helpers.project_merge_request_path(merge_request2.project, merge_request2),
+ avatar_url: '' # This project didn't have an avatar so set this to ''
+ })
+ end
+
it "does not include the public group" do
group = create(:group)
expect(search_autocomplete_opts(group.name).size).to eq(0)
@@ -228,6 +296,20 @@ RSpec.describe SearchHelper do
end
end
+ describe 'search_md_sanitize' do
+ it 'does not do extra sql queries for partial markdown rendering' do
+ @project = create(:project)
+
+ description = FFaker::Lorem.characters(210)
+ control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { search_md_sanitize(description) }.count
+
+ issues = create_list(:issue, 4, project: @project)
+
+ description_with_issues = description + ' ' + issues.map { |issue| "##{issue.iid}" }.join(' ')
+ expect { search_md_sanitize(description_with_issues) }.not_to exceed_all_query_limit(control_count)
+ end
+ end
+
describe 'search_filter_link' do
it 'renders a search filter link for the current scope' do
@scope = 'projects'