diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /spec/lib/gitlab/search_context | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'spec/lib/gitlab/search_context')
-rw-r--r-- | spec/lib/gitlab/search_context/builder_spec.rb | 152 | ||||
-rw-r--r-- | spec/lib/gitlab/search_context/controller_concern_spec.rb | 82 |
2 files changed, 234 insertions, 0 deletions
diff --git a/spec/lib/gitlab/search_context/builder_spec.rb b/spec/lib/gitlab/search_context/builder_spec.rb new file mode 100644 index 00000000000..1707b54b273 --- /dev/null +++ b/spec/lib/gitlab/search_context/builder_spec.rb @@ -0,0 +1,152 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::SearchContext::Builder, type: :controller do + controller(ApplicationController) { } + + subject(:builder) { described_class.new(controller.view_context) } + + shared_examples "has a fluid interface" do + it { is_expected.to be_instance_of(described_class) } + end + + def expected_project_metadata(project) + return {} if project.nil? + + a_hash_including(project_path: project.path, + name: project.name, + issues_path: a_string_including("/issues"), + mr_path: a_string_including("/merge_requests"), + issues_disabled: !project.issues_enabled?) + end + + def expected_group_metadata(group) + return {} if group.nil? + + a_hash_including(group_path: group.path, + name: group.name, + issues_path: a_string_including("/issues"), + mr_path: a_string_including("/merge_requests")) + end + + def expected_search_url(project, group) + if project + search_path(project_id: project.id) + elsif group + search_path(group_id: group.id) + else + search_path + end + end + + def be_search_context(project: nil, group: nil, snippets: [], ref: nil) + group = project ? project.group : group + snippets.compact! + ref = ref + + have_attributes( + project: project, + group: group, + ref: ref, + snippets: snippets, + project_metadata: expected_project_metadata(project), + group_metadata: expected_group_metadata(group), + search_url: expected_search_url(project, group) + ) + end + + describe '#with_project' do + let(:project) { create(:project) } + + subject { builder.with_project(project) } + + it_behaves_like "has a fluid interface" + + describe '#build!' do + subject(:context) { builder.with_project(project).build! } + + context 'when a project is not owned by a group' do + it { is_expected.to be_for_project } + it { is_expected.to be_search_context(project: project) } + end + + context 'when a project is owned by a group' do + let(:project) { create(:project, group: create(:group)) } + + it 'delegates to `#with_group`' do + expect(builder).to receive(:with_group).with(project.group) + expect(context).to be + end + + it { is_expected.to be_search_context(project: project, group: project.group) } + end + end + end + + describe '#with_snippet' do + context 'when there is a single snippet' do + let(:snippet) { create(:snippet) } + + subject { builder.with_snippet(snippet) } + + it_behaves_like "has a fluid interface" + + describe '#build!' do + subject(:context) { builder.with_snippet(snippet).build! } + + it { is_expected.to be_for_snippet } + it { is_expected.to be_search_context(snippets: [snippet]) } + end + end + + context 'when there are multiple snippets' do + let(:snippets) { create_list(:snippet, 3) } + + describe '#build!' do + subject(:context) do + snippets.each(&builder.method(:with_snippet)) + builder.build! + end + + it { is_expected.to be_for_snippet } + it { is_expected.to be_search_context(snippets: snippets) } + end + end + end + + describe '#with_group' do + let(:group) { create(:group) } + + subject { builder.with_group(group) } + + it_behaves_like "has a fluid interface" + + describe '#build!' do + subject(:context) { builder.with_group(group).build! } + + it { is_expected.to be_for_group } + it { is_expected.to be_search_context(group: group) } + end + end + + describe '#with_ref' do + let(:ref) { Gitlab::Git::EMPTY_TREE_ID } + + subject { builder.with_ref(ref) } + + it_behaves_like "has a fluid interface" + + describe '#build!' do + subject(:context) { builder.with_ref(ref).build! } + + it { is_expected.to be_search_context(ref: ref) } + end + end + + describe '#build!' do + subject(:context) { builder.build! } + + it { is_expected.to be_a(Gitlab::SearchContext) } + end +end diff --git a/spec/lib/gitlab/search_context/controller_concern_spec.rb b/spec/lib/gitlab/search_context/controller_concern_spec.rb new file mode 100644 index 00000000000..16784cafb76 --- /dev/null +++ b/spec/lib/gitlab/search_context/controller_concern_spec.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::SearchContext::ControllerConcern, type: :controller do + controller(ApplicationController) do + include Gitlab::SearchContext::ControllerConcern + end + + let(:project) { nil } + let(:group) { nil } + let(:snippet) { nil } + let(:snippets) { [] } + let(:ref) { nil } + + let(:builder) { Gitlab::SearchContext::Builder.new(controller.view_context) } + + subject(:search_context) { controller.search_context } + + def weak_assign(ivar, value) + return if value.nil? + + controller.instance_variable_set(ivar.to_sym, value) + end + + before do + weak_assign(:@project, project) + weak_assign(:@group, group) + weak_assign(:@ref, ref) + weak_assign(:@snippet, snippet) + weak_assign(:@snippets, snippets) + + allow(Gitlab::SearchContext::Builder).to receive(:new).and_return(builder) + end + + shared_examples 'has the proper context' do + it :aggregate_failures do + expected_group = project ? project.group : group + expected_snippets = [snippet, *snippets].compact + + expect(builder).to receive(:with_project).with(project).and_call_original if project + expect(builder).to receive(:with_group).with(expected_group).and_call_original if expected_group + expect(builder).to receive(:with_ref).with(ref).and_call_original if ref + expected_snippets.each do |snippet| + expect(builder).to receive(:with_snippet).with(snippet).and_call_original + end + + is_expected.to be_a(Gitlab::SearchContext) + end + end + + context 'exposing @project' do + let(:project) { create(:project) } + + it_behaves_like 'has the proper context' + + context 'when the project is owned by a group' do + let(:project) { create(:project, group: create(:group)) } + + it_behaves_like 'has the proper context' + end + end + + context 'exposing @group' do + let(:group) { create(:group) } + + it_behaves_like 'has the proper context' + end + + context 'exposing @snippet, @snippets' do + let(:snippet) { create(:snippet) } + let(:snippets) { create_list(:snippet, 3) } + + it_behaves_like 'has the proper context' + end + + context 'exposing @ref' do + let(:ref) { Gitlab::Git::EMPTY_TREE_ID } + + it_behaves_like 'has the proper context' + end +end |