# frozen_string_literal: true require 'spec_helper' # Persisting records is required because Event#target's AR scope. # We are trying hard to minimize record creations by: # * Using `let_it_be` # * Factory defaults via `create_default` + `factory_default: :keep` # # rubocop:disable RSpec/FactoryBot/AvoidCreate RSpec.describe EventsHelper, factory_default: :keep, feature_category: :user_profile do include Gitlab::Routing include Banzai::Filter::OutputSafety let_it_be(:project) { create_default(:project).freeze } let_it_be(:project_with_repo) { create(:project, :public, :repository).freeze } let_it_be(:user) { create_default(:user).freeze } describe '#link_to_author' do let(:user) { create(:user) } let(:event) { create(:event, author: user) } it 'returns a link to the author' do name = user.name expect(helper.link_to_author(event)).to eq(link_to(name, user_path(user.username), title: name, data: { user_id: user.id, username: user.username }, class: 'js-user-link')) end it 'returns the author name if the author is not present' do event.author = nil expect(helper.link_to_author(event)).to eq(escape_once(event.author_name)) end it 'returns "You" if the author is the current user' do allow(helper).to receive(:current_user).and_return(user) name = _('You') expect(helper.link_to_author(event, self_added: true)).to eq(link_to(name, user_path(user.username), title: name, data: { user_id: user.id, username: user.username }, class: 'js-user-link')) end end describe '#icon_for_profile_event' do let(:event) { build(:event, :joined) } let(:users_activity_page?) { true } before do allow(helper).to receive(:current_path?).and_call_original allow(helper).to receive(:current_path?).with('users#activity').and_return(users_activity_page?) end context 'when on users activity page' do it 'gives an icon with specialized classes' do result = helper.icon_for_profile_event(event) expect(result).to include('joined-icon') expect(result).to include('wiki page", "", title, "" ].join expect(helper.event_wiki_title_html(event)).to eq(html) end it 'produces a suitable title chunk on the user profile' do allow(helper).to receive(:user_profile_activity_classes).and_return( 'gl-font-weight-semibold gl-text-black-normal') html = [ "wiki page", "", title, "" ].join expect(helper.event_wiki_title_html(event)).to eq(html) end end describe '#event_note_target_url' do let_it_be(:event) { create(:event) } let(:project_base_url) { namespace_project_url(namespace_id: project.namespace, id: project) } subject { helper.event_note_target_url(event) } it 'returns a commit note url' do event.target = create(:note_on_commit, project: project_with_repo, note: '+1 from me') expect(subject).to eq("#{project_base_url}/-/commit/#{event.target.commit_id}#note_#{event.target.id}") end it 'returns a project snippet note url' do event.target = create(:note_on_project_snippet, note: 'keep going') expect(subject).to eq("#{project_snippet_url(event.note_target.project, event.note_target)}#note_#{event.target.id}") end it 'returns a personal snippet note url' do event.target = create(:note_on_personal_snippet, note: 'keep going') expect(subject).to eq("#{snippet_url(event.note_target)}#note_#{event.target.id}") end it 'returns a project issue url' do event.target = create(:note_on_issue, note: 'nice work') expect(subject).to eq("#{project_base_url}/-/issues/#{event.note_target.iid}#note_#{event.target.id}") end it 'returns a merge request url' do event.target = create(:note_on_merge_request, note: 'LGTM!') expect(subject).to eq("#{project_base_url}/-/merge_requests/#{event.note_target.iid}#note_#{event.target.id}") end context 'for design note events' do let(:event) { create(:event, :for_design) } it 'returns an appropriate URL' do iid = event.note_target.issue.iid filename = event.note_target.filename note_id = event.target.id expect(subject).to eq("#{project_base_url}/-/issues/#{iid}/designs/#{filename}#note_#{note_id}") end end end describe '#event_filter_visible' do include DesignManagementTestHelpers subject { helper.event_filter_visible(key) } before do enable_design_management allow(helper).to receive(:current_user).and_return(user) end def can_read_design_activity(object, ability) allow(Ability).to receive(:allowed?) .with(user, :read_design_activity, eq(object)) .and_return(ability) end context 'for :designs' do let(:key) { :designs } context 'without relevant instance variable' do it { is_expected.to be(true) } end context 'with assigned project' do before do assign(:project, project) end context 'with permission' do before do can_read_design_activity(project, true) end it { is_expected.to be(true) } end context 'without permission' do before do can_read_design_activity(project, false) end it { is_expected.to be(false) } end end context 'with projects assigned' do before do assign(:projects, Project.id_in(project)) end context 'with permission' do before do can_read_design_activity(project, true) end it { is_expected.to be(true) } end context 'with empty collection' do before do assign(:projects, Project.none) end it { is_expected.to be(false) } end context 'without permission' do before do can_read_design_activity(project, false) end it { is_expected.to be(false) } end end context 'with group assigned' do let_it_be(:group) { create(:group) } before do assign(:group, group) end context 'without projects in the group' do it { is_expected.to be(false) } end context 'with at least one project in the project' do let_it_be(:group_link) { create(:project_group_link, group: group) } context 'with permission' do before do can_read_design_activity(group, true) end it { is_expected.to be(true) } end context 'without permission' do before do can_read_design_activity(group, false) end it { is_expected.to be(false) } end end end end end describe '#user_profile_activity_classes' do let(:users_activity_page?) { true } before do allow(helper).to receive(:current_path?).and_call_original allow(helper).to receive(:current_path?).with('users#activity').and_return(users_activity_page?) end context 'when on the user activity page' do it 'returns the expected class names' do expect(helper.user_profile_activity_classes).to eq(' gl-font-weight-semibold gl-text-black-normal') end end context 'when not on the user activity page' do let(:users_activity_page?) { false } it 'returns an empty string' do expect(helper.user_profile_activity_classes).to eq('') end end end end # rubocop:enable RSpec/FactoryBot/AvoidCreate