debian-mirror-gitlab/spec/helpers/nav/new_dropdown_helper_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

343 lines
12 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Nav::NewDropdownHelper, feature_category: :navigation do
2021-09-04 01:27:46 +05:30
describe '#new_dropdown_view_model' do
2023-04-23 21:23:45 +05:30
let(:user) { build_stubbed(:user) }
2021-09-04 01:27:46 +05:30
let(:current_user) { user }
let(:current_project) { nil }
let(:current_group) { nil }
let(:with_can_create_project) { false }
let(:with_can_create_group) { false }
let(:with_can_create_snippet) { false }
2023-04-23 21:23:45 +05:30
subject(:view_model) { helper.new_dropdown_view_model(project: current_project, group: current_group) }
2021-09-04 01:27:46 +05:30
before do
allow(helper).to receive(:current_user) { current_user }
2023-04-23 21:23:45 +05:30
allow(helper).to receive(:can?).and_return(false)
2021-09-04 01:27:46 +05:30
allow(user).to receive(:can_create_project?) { with_can_create_project }
allow(user).to receive(:can_create_group?) { with_can_create_group }
allow(user).to receive(:can?).with(:create_snippet) { with_can_create_snippet }
end
2023-04-23 21:23:45 +05:30
shared_examples 'invite member item' do
2022-01-26 12:08:38 +05:30
it 'shows invite member link with emoji' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
title: expected_title,
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'invite',
title: 'Invite members',
2022-01-26 12:08:38 +05:30
emoji: 'shaking_hands',
2021-09-04 01:27:46 +05:30
href: expected_href,
data: {
2022-01-26 12:08:38 +05:30
track_action: 'click_link_invite_members',
2023-04-23 21:23:45 +05:30
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top'
2021-09-04 01:27:46 +05:30
}
)
)
)
end
end
it 'has title' do
2023-04-23 21:23:45 +05:30
expect(view_model[:title]).to eq('Create new...')
2021-09-04 01:27:46 +05:30
end
context 'when current_user is nil (anonymous)' do
let(:current_user) { nil }
2023-04-23 21:23:45 +05:30
it { is_expected.to be_nil }
2021-09-04 01:27:46 +05:30
end
context 'when group and project are nil' do
it 'has no menu sections' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq([])
2021-09-04 01:27:46 +05:30
end
context 'when can create project' do
let(:with_can_create_project) { true }
it 'has project menu item' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
2023-04-23 21:23:45 +05:30
title: _('In GitLab'),
2021-09-04 01:27:46 +05:30
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'general_new_project',
2021-09-30 23:02:18 +05:30
title: 'New project/repository',
2021-09-04 01:27:46 +05:30
href: '/projects/new',
2023-04-23 21:23:45 +05:30
data: {
track_action: 'click_link_new_project',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top',
qa_selector: 'global_new_project_link'
}
2021-09-04 01:27:46 +05:30
)
)
)
end
end
context 'when can create group' do
let(:with_can_create_group) { true }
it 'has group menu item' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
2023-04-23 21:23:45 +05:30
title: _('In GitLab'),
2021-09-04 01:27:46 +05:30
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'general_new_group',
title: 'New group',
href: '/groups/new',
2023-04-23 21:23:45 +05:30
data: {
track_action: 'click_link_new_group',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top',
qa_selector: 'global_new_group_link'
}
2021-09-04 01:27:46 +05:30
)
)
)
end
end
context 'when can create snippet' do
let(:with_can_create_snippet) { true }
it 'has new snippet menu item' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
2023-04-23 21:23:45 +05:30
title: _('In GitLab'),
2021-09-04 01:27:46 +05:30
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'general_new_snippet',
title: 'New snippet',
href: '/-/snippets/new',
2023-04-23 21:23:45 +05:30
data: {
track_action: 'click_link_new_snippet_parent',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top',
qa_selector: 'global_new_snippet_link'
}
2021-09-04 01:27:46 +05:30
)
)
)
end
end
end
context 'with persisted group' do
2023-04-23 21:23:45 +05:30
let(:group) { build_stubbed(:group) }
2021-09-04 01:27:46 +05:30
let(:current_group) { group }
let(:with_can_create_projects_in_group) { false }
let(:with_can_create_subgroup_in_group) { false }
let(:with_can_admin_in_group) { false }
before do
2023-04-23 21:23:45 +05:30
allow(group).to receive(:persisted?).and_return(true)
allow(helper)
.to receive(:can?).with(current_user, :create_projects, group) { with_can_create_projects_in_group }
allow(helper)
.to receive(:can?).with(current_user, :create_subgroup, group) { with_can_create_subgroup_in_group }
allow(helper)
.to receive(:can?).with(current_user, :admin_group_member, group) { with_can_admin_in_group }
2021-09-04 01:27:46 +05:30
end
it 'has no menu sections' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq([])
2021-09-04 01:27:46 +05:30
end
context 'when can create projects in group' do
let(:with_can_create_projects_in_group) { true }
it 'has new project menu item' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
2023-04-23 21:23:45 +05:30
title: 'In this group',
2021-09-04 01:27:46 +05:30
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'new_project',
2021-09-30 23:02:18 +05:30
title: 'New project/repository',
2021-09-04 01:27:46 +05:30
href: "/projects/new?namespace_id=#{group.id}",
2023-04-23 21:23:45 +05:30
data: {
track_action: 'click_link_new_project_group',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top'
}
2021-09-04 01:27:46 +05:30
)
)
)
end
end
context 'when can create subgroup' do
let(:with_can_create_subgroup_in_group) { true }
it 'has new subgroup menu item' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
2023-04-23 21:23:45 +05:30
title: 'In this group',
2021-09-04 01:27:46 +05:30
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'new_subgroup',
title: 'New subgroup',
2022-08-27 11:52:29 +05:30
href: "/groups/new?parent_id=#{group.id}#create-group-pane",
2023-04-23 21:23:45 +05:30
data: {
track_action: 'click_link_new_subgroup',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top'
}
2021-09-04 01:27:46 +05:30
)
)
)
end
end
context 'when can invite members' do
let(:with_can_admin_in_group) { true }
let(:with_invite_members_experiment) { true }
2023-04-23 21:23:45 +05:30
let(:expected_title) { 'In this group' }
2021-09-04 01:27:46 +05:30
let(:expected_href) { "/groups/#{group.full_path}/-/group_members" }
2023-04-23 21:23:45 +05:30
it_behaves_like 'invite member item'
2021-09-04 01:27:46 +05:30
end
end
context 'with persisted project' do
2023-04-23 21:23:45 +05:30
let(:project) { build_stubbed(:project) }
let(:merge_project) { build_stubbed(:project) }
2021-09-04 01:27:46 +05:30
let(:current_project) { project }
let(:with_show_new_issue_link) { false }
let(:with_merge_project) { nil }
let(:with_can_create_snippet_in_project) { false }
2021-10-27 15:23:28 +05:30
let(:with_can_admin_project_member) { false }
2021-09-04 01:27:46 +05:30
before do
allow(helper).to receive(:show_new_issue_link?).with(project) { with_show_new_issue_link }
allow(helper).to receive(:merge_request_source_project_for_project).with(project) { with_merge_project }
allow(helper).to receive(:can?).with(user, :create_snippet, project) { with_can_create_snippet_in_project }
2021-10-27 15:23:28 +05:30
allow(helper).to receive(:can_admin_project_member?) { with_can_admin_project_member }
2021-09-04 01:27:46 +05:30
end
it 'has no menu sections' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq([])
2021-09-04 01:27:46 +05:30
end
context 'with show_new_issue_link?' do
let(:with_show_new_issue_link) { true }
it 'shows new issue menu item' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
2023-04-23 21:23:45 +05:30
title: 'In this project',
2021-09-04 01:27:46 +05:30
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'new_issue',
title: 'New issue',
href: "/#{project.path_with_namespace}/-/issues/new",
2023-04-23 21:23:45 +05:30
data: {
track_action: 'click_link_new_issue',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top',
qa_selector: 'new_issue_link'
}
2021-09-04 01:27:46 +05:30
)
)
)
end
end
context 'with merge project' do
let(:with_merge_project) { merge_project }
it 'shows merge project' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
2023-04-23 21:23:45 +05:30
title: 'In this project',
2021-09-04 01:27:46 +05:30
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'new_mr',
title: 'New merge request',
href: "/#{merge_project.path_with_namespace}/-/merge_requests/new",
2023-04-23 21:23:45 +05:30
data: {
track_action: 'click_link_new_mr',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top'
}
2021-09-04 01:27:46 +05:30
)
)
)
end
end
context 'when can create snippet' do
let(:with_can_create_snippet_in_project) { true }
it 'shows new snippet' do
2023-04-23 21:23:45 +05:30
expect(view_model[:menu_sections]).to eq(
2021-09-04 01:27:46 +05:30
expected_menu_section(
2023-04-23 21:23:45 +05:30
title: 'In this project',
2021-09-04 01:27:46 +05:30
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'new_snippet',
title: 'New snippet',
href: "/#{project.path_with_namespace}/-/snippets/new",
2023-04-23 21:23:45 +05:30
data: {
track_action: 'click_link_new_snippet_project',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top'
}
2021-09-04 01:27:46 +05:30
)
)
)
end
end
context 'when invite members experiment' do
let(:with_invite_members_experiment) { true }
2021-10-27 15:23:28 +05:30
let(:with_can_admin_project_member) { true }
2023-04-23 21:23:45 +05:30
let(:expected_title) { 'In this project' }
2021-09-04 01:27:46 +05:30
let(:expected_href) { "/#{project.path_with_namespace}/-/project_members" }
2023-04-23 21:23:45 +05:30
it_behaves_like 'invite member item'
end
end
context 'with persisted group and project' do
let(:project) { build_stubbed(:project) }
let(:group) { build_stubbed(:group) }
let(:current_project) { project }
let(:current_group) { group }
before do
allow(helper).to receive(:show_new_issue_link?).with(project).and_return(true)
allow(helper).to receive(:can?).with(current_user, :create_projects, group).and_return(true)
end
it 'gives precedence to group over project' do
group_section = expected_menu_section(
title: 'In this group',
menu_item: ::Gitlab::Nav::TopNavMenuItem.build(
id: 'new_project',
title: 'New project/repository',
href: "/projects/new?namespace_id=#{group.id}",
data: {
track_action: 'click_link_new_project_group',
track_label: 'plus_menu_dropdown',
track_property: 'navigation_top'
}
)
)
expect(view_model[:menu_sections]).to eq(group_section)
2021-09-04 01:27:46 +05:30
end
end
2023-04-23 21:23:45 +05:30
def expected_menu_section(title:, menu_item:)
[
{
title: title,
menu_items: [menu_item]
}
]
end
2021-09-04 01:27:46 +05:30
end
end