debian-mirror-gitlab/spec/helpers/merge_requests_helper_spec.rb

238 lines
8.2 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-10-24 18:46:33 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe MergeRequestsHelper, feature_category: :code_review_workflow do
2023-07-09 08:55:56 +05:30
include Users::CalloutsHelper
include ApplicationHelper
include PageLayoutHelper
include ProjectsHelper
2018-03-17 18:26:18 +05:30
include ProjectForksHelper
2023-06-20 00:43:36 +05:30
include IconsHelper
2018-03-17 18:26:18 +05:30
2023-07-09 08:55:56 +05:30
let_it_be(:current_user) { create(:user) }
2015-12-23 02:04:40 +05:30
describe '#format_mr_branch_names' do
describe 'within the same project' do
2015-10-24 18:46:33 +05:30
let(:merge_request) { create(:merge_request) }
2020-01-01 13:55:28 +05:30
2015-10-24 18:46:33 +05:30
subject { format_mr_branch_names(merge_request) }
it { is_expected.to eq([merge_request.source_branch, merge_request.target_branch]) }
end
2015-12-23 02:04:40 +05:30
describe 'within different projects' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2018-03-17 18:26:18 +05:30
let(:forked_project) { fork_project(project) }
let(:merge_request) { create(:merge_request, source_project: forked_project, target_project: project) }
2015-10-24 18:46:33 +05:30
subject { format_mr_branch_names(merge_request) }
2019-12-21 20:55:43 +05:30
2018-03-17 18:26:18 +05:30
let(:source_title) { "#{forked_project.full_path}:#{merge_request.source_branch}" }
2017-09-10 17:25:29 +05:30
let(:target_title) { "#{project.full_path}:#{merge_request.target_branch}" }
2015-10-24 18:46:33 +05:30
it { is_expected.to eq([source_title, target_title]) }
end
end
2018-03-17 18:26:18 +05:30
2023-07-09 08:55:56 +05:30
describe '#diffs_tab_pane_data' do
subject { diffs_tab_pane_data(project, merge_request, {}) }
context 'for endpoint_diff_for_path' do
context 'when sub-group project namespace' do
let_it_be(:group) { create(:group, :public) }
let_it_be(:subgroup) { create(:group, :private, parent: group) }
let_it_be(:project) { create(:project, :private, group: subgroup) }
let_it_be(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
it 'returns expected values' do
expect(
subject[:endpoint_diff_for_path]
).to include("#{project.full_path}/-/merge_requests/#{merge_request.iid}/diff_for_path.json")
end
end
end
end
2022-08-27 11:52:29 +05:30
describe '#merge_path_description' do
2023-07-09 08:55:56 +05:30
# Using let_it_be(:project) raises the following error, so we use need to use let(:project):
# ActiveRecord::InvalidForeignKey:
# PG::ForeignKeyViolation: ERROR: insert or update on table "fork_network_members" violates foreign key
# constraint "fk_rails_a40860a1ca"
# DETAIL: Key (fork_network_id)=(8) is not present in table "fork_networks".
let(:project) { create(:project) }
2022-08-27 11:52:29 +05:30
let(:forked_project) { fork_project(project) }
let(:merge_request_forked) { create(:merge_request, source_project: forked_project, target_project: project) }
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
where(:case_name, :mr, :with_arrow, :result) do
[
['forked with arrow', ref(:merge_request_forked), true, lazy do
"Project:Branches: #{
mr.source_project_path}:#{mr.source_branch} → #{
mr.target_project.full_path}:#{mr.target_branch}"
end],
['forked default', ref(:merge_request_forked), false, lazy do
"Project:Branches: #{
mr.source_project_path}:#{mr.source_branch} to #{
mr.target_project.full_path}:#{mr.target_branch}"
end],
['with arrow', ref(:merge_request), true, lazy { "Branches: #{mr.source_branch}#{mr.target_branch}" }],
['default', ref(:merge_request), false, lazy { "Branches: #{mr.source_branch} to #{mr.target_branch}" }]
]
end
with_them do
subject { merge_path_description(mr, with_arrow: with_arrow) }
it {
is_expected.to eq(result)
}
end
end
2018-03-17 18:26:18 +05:30
describe '#tab_link_for' do
let(:merge_request) { create(:merge_request, :simple) }
2020-11-24 15:15:51 +05:30
let(:options) { {} }
2018-03-17 18:26:18 +05:30
subject { tab_link_for(merge_request, :show, options) { 'Discussion' } }
describe 'supports the :force_link option' do
let(:options) { { force_link: true } }
it 'removes the data-toggle attributes' do
2018-11-08 19:23:39 +05:30
is_expected.not_to match(/data-toggle="tabvue"/)
2018-03-17 18:26:18 +05:30
end
end
end
2021-03-08 18:12:59 +05:30
describe '#user_merge_requests_counts' do
let(:user) do
double(
assigned_open_merge_requests_count: 1,
2022-08-27 11:52:29 +05:30
review_requested_open_merge_requests_count: 2
2021-03-08 18:12:59 +05:30
)
end
subject { helper.user_merge_requests_counts }
before do
allow(helper).to receive(:current_user).and_return(user)
end
2022-08-27 11:52:29 +05:30
it "returns assigned, review requested and total merge request counts" do
expect(subject).to eq(
assigned: user.assigned_open_merge_requests_count,
review_requested: user.review_requested_open_merge_requests_count,
total: user.assigned_open_merge_requests_count + user.review_requested_open_merge_requests_count
)
2021-03-08 18:12:59 +05:30
end
end
2021-04-17 20:07:23 +05:30
describe '#reviewers_label' do
let(:merge_request) { build_stubbed(:merge_request) }
let(:reviewer1) { build_stubbed(:user, name: 'Jane Doe') }
let(:reviewer2) { build_stubbed(:user, name: 'John Doe') }
before do
allow(merge_request).to receive(:reviewers).and_return(reviewers)
end
context 'when multiple reviewers exist' do
let(:reviewers) { [reviewer1, reviewer2] }
it 'returns reviewer label with reviewer names' do
expect(helper.reviewers_label(merge_request)).to eq("Reviewers: Jane Doe and John Doe")
end
it 'returns reviewer label only with include_value: false' do
expect(helper.reviewers_label(merge_request, include_value: false)).to eq("Reviewers")
end
context 'when the name contains a URL' do
let(:reviewers) { [build_stubbed(:user, name: 'www.gitlab.com')] }
it 'returns sanitized name' do
expect(helper.reviewers_label(merge_request)).to eq("Reviewer: www_gitlab_com")
end
end
end
context 'when one reviewer exists' do
let(:reviewers) { [reviewer1] }
it 'returns reviewer label with no names' do
expect(helper.reviewers_label(merge_request)).to eq("Reviewer: Jane Doe")
end
it 'returns reviewer label only with include_value: false' do
expect(helper.reviewers_label(merge_request, include_value: false)).to eq("Reviewer")
end
end
context 'when no reviewers exist' do
let(:reviewers) { [] }
it 'returns reviewer label with no names' do
expect(helper.reviewers_label(merge_request)).to eq("Reviewers: ")
end
2022-08-27 11:52:29 +05:30
2021-04-17 20:07:23 +05:30
it 'returns reviewer label only with include_value: false' do
expect(helper.reviewers_label(merge_request, include_value: false)).to eq("Reviewers")
end
end
end
2023-06-20 00:43:36 +05:30
describe '#merge_request_source_branch' do
2023-07-09 08:55:56 +05:30
branch_name = 'name<script>test</script>'
2023-07-07 10:43:13 +05:30
let(:project) { create(:project) }
2023-06-20 00:43:36 +05:30
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
2023-07-07 10:43:13 +05:30
let(:forked_project) { fork_project(project) }
let(:merge_request_forked) do
create(
:merge_request,
source_project: forked_project,
2023-07-09 08:55:56 +05:30
source_branch: branch_name,
2023-07-07 10:43:13 +05:30
target_project: project
)
end
2023-06-20 00:43:36 +05:30
context 'when merge request is a fork' do
subject { merge_request_source_branch(merge_request_forked) }
it 'does show the fork icon' do
expect(subject).to match(/fork/)
end
2023-07-07 10:43:13 +05:30
it 'escapes properly' do
2023-07-09 08:55:56 +05:30
expect(subject).to include(html_escape(branch_name))
2023-07-07 10:43:13 +05:30
end
2023-06-20 00:43:36 +05:30
end
context 'when merge request is not a fork' do
subject { merge_request_source_branch(merge_request) }
it 'does not show the fork icon' do
expect(subject).not_to match(/fork/)
end
end
end
2023-07-09 08:55:56 +05:30
describe '#tab_count_display' do
let(:merge_request) { create(:merge_request) }
context 'when merge request is preparing' do
before do
allow(merge_request).to receive(:preparing?).and_return(true)
end
it { expect(tab_count_display(merge_request, 0)).to eq('-') }
it { expect(tab_count_display(merge_request, '0')).to eq('-') }
end
context 'when merge request is prepared' do
it { expect(tab_count_display(merge_request, 10)).to eq(10) }
it { expect(tab_count_display(merge_request, '10')).to eq('10') }
end
end
2015-10-24 18:46:33 +05:30
end