Refactor tracking attributes method in sidebars

We're going to refactor sidebars, and we need this refactor
in order to use one method for each sidebar.
parent 0f43cd47
# frozen_string_literal: true
module SidebarsHelper
def sidebar_tracking_attributes_by_object(object)
case object
when Project
sidebar_project_tracking_attrs
when Group
sidebar_group_tracking_attrs
when User
sidebar_user_profile_tracking_attrs
else
{}
end
end
private
def sidebar_project_tracking_attrs
tracking_attrs('projects_side_navigation', 'render', 'projects_side_navigation')
end
def sidebar_group_tracking_attrs
tracking_attrs('groups_side_navigation', 'render', 'groups_side_navigation')
end
def sidebar_user_profile_tracking_attrs
tracking_attrs('user_side_navigation', 'render', 'user_side_navigation')
end
end
......@@ -3,7 +3,7 @@
- aside_title = @group.subgroup? ? _('Subgroup navigation') : _('Group navigation')
- overview_title = @group.subgroup? ? _('Subgroup overview') : _('Group overview')
%aside.nav-sidebar{ class: ("sidebar-collapsed-desktop" if collapsed_sidebar?), **tracking_attrs('groups_side_navigation', 'render', 'groups_side_navigation'), 'aria-label': aside_title }
%aside.nav-sidebar{ class: ("sidebar-collapsed-desktop" if collapsed_sidebar?), **sidebar_tracking_attributes_by_object(@group), 'aria-label': aside_title }
.nav-sidebar-inner-scroll
.context-header
= link_to group_path(@group), title: @group.name do
......
%aside.nav-sidebar{ class: ("sidebar-collapsed-desktop" if collapsed_sidebar?), **tracking_attrs('user_side_navigation', 'render', 'user_side_navigation'), 'aria-label': _('User settings') }
%aside.nav-sidebar{ class: ("sidebar-collapsed-desktop" if collapsed_sidebar?), **sidebar_tracking_attributes_by_object(current_user), 'aria-label': _('User settings') }
.nav-sidebar-inner-scroll
.context-header
= link_to profile_path, title: _('Profile Settings') do
......
%aside.nav-sidebar{ class: ("sidebar-collapsed-desktop" if collapsed_sidebar?), **tracking_attrs('projects_side_navigation', 'render', 'projects_side_navigation'), 'aria-label': _('Project navigation') }
%aside.nav-sidebar{ class: ("sidebar-collapsed-desktop" if collapsed_sidebar?), **sidebar_tracking_attributes_by_object(@project), 'aria-label': _('Project navigation') }
.nav-sidebar-inner-scroll
.context-header
= link_to project_path(@project), title: @project.name do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe SidebarsHelper do
describe '#sidebar_tracking_attributes_by_object' do
subject { helper.sidebar_tracking_attributes_by_object(object) }
before do
allow(helper).to receive(:tracking_enabled?).and_return(true)
end
context 'when object is a project' do
let(:object) { build(:project) }
it 'returns tracking attrs for project' do
expect(subject[:data]).to eq({ track_label: 'projects_side_navigation', track_property: 'projects_side_navigation', track_action: 'render' })
end
end
context 'when object is a group' do
let(:object) { build(:group) }
it 'returns tracking attrs for group' do
expect(subject[:data]).to eq({ track_label: 'groups_side_navigation', track_property: 'groups_side_navigation', track_action: 'render' })
end
end
context 'when object is a user' do
let(:object) { build(:user) }
it 'returns tracking attrs for user' do
expect(subject[:data]).to eq({ track_label: 'user_side_navigation', track_property: 'user_side_navigation', track_action: 'render' })
end
end
context 'when object is something else' do
let(:object) { build(:ci_pipeline) }
it 'returns no attributes' do
expect(subject).to eq({})
end
end
end
end
......@@ -24,3 +24,13 @@ RSpec.shared_examples 'page has active sub tab' do |title|
.to have_content(title)
end
end
RSpec.shared_examples 'sidebar includes snowplow attributes' do |track_action, track_label, track_property|
specify do
allow(view).to receive(:tracking_enabled?).and_return(true)
render
expect(rendered).to have_css(".nav-sidebar[data-track-action=\"#{track_action}\"][data-track-label=\"#{track_label}\"][data-track-property=\"#{track_property}\"]")
end
end
......@@ -10,4 +10,5 @@ RSpec.describe 'layouts/nav/sidebar/_group' do
end
it_behaves_like 'has nav sidebar'
it_behaves_like 'sidebar includes snowplow attributes', 'render', 'groups_side_navigation', 'groups_side_navigation'
end
......@@ -10,4 +10,5 @@ RSpec.describe 'layouts/nav/sidebar/_profile' do
end
it_behaves_like 'has nav sidebar'
it_behaves_like 'sidebar includes snowplow attributes', 'render', 'user_side_navigation', 'user_side_navigation'
end
......@@ -346,4 +346,6 @@ RSpec.describe 'layouts/nav/sidebar/_project' do
end
end
end
it_behaves_like 'sidebar includes snowplow attributes', 'render', 'projects_side_navigation', 'projects_side_navigation'
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment