Commit b44226c6 authored by David Fernandez's avatar David Fernandez

Merge branch...

Merge branch '323645-f-u-f-51003-reduce-clutter-complexity-in-the-view-using-helper-methods' into 'master'

Reduce clutter & complexity in the view using helper methods

See merge request gitlab-org/gitlab!55895
parents 0603678a 95165a57
# frozen_string_literal: true # frozen_string_literal: true
module TrialStatusWidgetHelper module TrialStatusWidgetHelper
def plan_title_for_group(group) def trial_status_popover_data_attrs(group)
group.gitlab_subscription&.plan_title base_attrs = trial_status_common_data_attrs(group)
base_attrs.merge(
group_name: group.name,
purchase_href: ultimate_subscription_path_for_group(group),
target_id: base_attrs[:container_id],
trial_end_date: group.trial_ends_on
)
end
def trial_status_widget_data_attrs(group)
trial_status_common_data_attrs(group).merge(
days_remaining: group.trial_days_remaining,
nav_icon_image_path: image_path('illustrations/golden_tanuki.svg'),
percentage_complete: group.trial_percentage_complete
)
end end
def show_trial_status_widget?(group) def show_trial_status_widget?(group)
...@@ -18,4 +32,25 @@ module TrialStatusWidgetHelper ...@@ -18,4 +32,25 @@ module TrialStatusWidgetHelper
def eligible_for_trial_status_widget?(group) def eligible_for_trial_status_widget?(group)
group.trial_active? && can?(current_user, :admin_namespace, group) group.trial_active? && can?(current_user, :admin_namespace, group)
end end
def trial_status_common_data_attrs(group)
{
container_id: 'trial-status-sidebar-widget',
plan_name: group.gitlab_subscription&.plan_title,
plans_href: group_billings_path(group)
}
end
def trial_status_widget_experiment_enabled?(group)
experiment_enabled?(:show_trial_status_in_sidebar, subject: group)
end
def ultimate_subscription_path_for_group(group)
# Hard-coding the plan_id to the Ultimate plan on production & staging
new_subscriptions_path(namespace_id: group.id, plan_id: '2c92a0fc5a83f01d015aa6db83c45aac')
end
def user_can_administer_group?(group)
can?(current_user, :admin_namespace, group)
end
end end
...@@ -8,17 +8,5 @@ ...@@ -8,17 +8,5 @@
- return unless experiment_enabled?(experiment_key, subject: root_group) - return unless experiment_enabled?(experiment_key, subject: root_group)
= nav_link do = nav_link do
#js-trial-status-widget{ data: { container_id: 'trial-status-sidebar-widget', #js-trial-status-widget{ data: trial_status_widget_data_attrs(root_group) }
days_remaining: root_group.trial_days_remaining, #js-trial-status-popover{ data: trial_status_popover_data_attrs(root_group) }
nav_icon_image_path: image_path('illustrations/golden_tanuki.svg'),
percentage_complete: root_group.trial_percentage_complete,
plan_name: plan_title_for_group(root_group),
plans_href: group_billings_path(root_group) } }
#js-trial-status-popover{ data: { container_id: 'trial-status-sidebar-widget',
group_name: root_group.name,
plan_name: plan_title_for_group(root_group),
plans_href: group_billings_path(root_group),
purchase_href: new_subscriptions_path(namespace_id: root_group.id, plan_id: '2c92a0fc5a83f01d015aa6db83c45aac'),
target_id: 'trial-status-sidebar-widget',
trial_end_date: root_group.trial_ends_on } }
...@@ -3,25 +3,64 @@ ...@@ -3,25 +3,64 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe TrialStatusWidgetHelper do RSpec.describe TrialStatusWidgetHelper do
describe '#plan_title_for_group' do describe 'data attributes for mounting Vue components' do
using RSpec::Parameterized::TableSyntax let(:subscription) { instance_double(GitlabSubscription, plan_title: 'Ultimate') }
let_it_be(:group) { create(:group) } let(:group) do
instance_double(Group,
id: 123,
name: 'Pants Group',
to_param: 'pants-group',
gitlab_subscription: subscription,
trial_days_remaining: 12,
trial_ends_on: Date.current.advance(days: 18),
trial_percentage_complete: 40
)
end
subject { helper.plan_title_for_group(group) } let(:shared_expected_attrs) do
{
container_id: 'trial-status-sidebar-widget',
plan_name: 'Ultimate',
plans_href: '/groups/pants-group/-/billings'
}
end
where(:plan, :title) do before do
:bronze | 'Bronze' travel_to Date.parse('2021-01-12')
:silver | 'Silver'
:gold | 'Gold'
:premium | 'Premium'
:ultimate | 'Ultimate'
end end
with_them do describe '#trial_status_popover_data_attrs' do
let!(:subscription) { build(:gitlab_subscription, plan, namespace: group) } subject(:data_attrs) { helper.trial_status_popover_data_attrs(group) }
it { is_expected.to eq(title) } it 'returns the needed data attributes for mounting the Vue component' do
expect(data_attrs).to match(
shared_expected_attrs.merge(
group_name: 'Pants Group',
purchase_href: '/-/subscriptions/new?namespace_id=123&plan_id=2c92a0fc5a83f01d015aa6db83c45aac',
target_id: shared_expected_attrs[:container_id],
trial_end_date: Date.parse('2021-01-30')
)
)
end
end
describe '#trial_status_widget_data_attrs' do
before do
allow(helper).to receive(:image_path).and_return('/image-path/for-file.svg')
end
subject(:data_attrs) { helper.trial_status_widget_data_attrs(group) }
it 'returns the needed data attributes for mounting the Vue component' do
expect(data_attrs).to match(
shared_expected_attrs.merge(
days_remaining: 12,
nav_icon_image_path: '/image-path/for-file.svg',
percentage_complete: 40
)
)
end
end end
end end
......
...@@ -14,8 +14,6 @@ RSpec.describe 'layouts/nav/sidebar/_group' do ...@@ -14,8 +14,6 @@ RSpec.describe 'layouts/nav/sidebar/_group' do
describe 'trial status widget', :aggregate_failures do describe 'trial status widget', :aggregate_failures do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
let!(:gitlab_subscription) { create(:gitlab_subscription, :active_trial, namespace: group) }
let(:experiment_key) { :show_trial_status_in_sidebar } let(:experiment_key) { :show_trial_status_in_sidebar }
let(:show_widget) { false } let(:show_widget) { false }
let(:experiment_enabled) { false } let(:experiment_enabled) { false }
...@@ -24,6 +22,9 @@ RSpec.describe 'layouts/nav/sidebar/_group' do ...@@ -24,6 +22,9 @@ RSpec.describe 'layouts/nav/sidebar/_group' do
allow(view).to receive(:show_trial_status_widget?).and_return(show_widget) allow(view).to receive(:show_trial_status_widget?).and_return(show_widget)
allow(view).to receive(:experiment_enabled?).with(experiment_key, subject: group).and_return(experiment_enabled) allow(view).to receive(:experiment_enabled?).with(experiment_key, subject: group).and_return(experiment_enabled)
allow(view).to receive(:record_experiment_group) allow(view).to receive(:record_experiment_group)
allow(view).to receive(:trial_status_widget_data_attrs)
allow(view).to receive(:trial_status_popover_data_attrs)
render
end end
subject do subject do
...@@ -43,13 +44,6 @@ RSpec.describe 'layouts/nav/sidebar/_group' do ...@@ -43,13 +44,6 @@ RSpec.describe 'layouts/nav/sidebar/_group' do
is_expected.to have_selector '#js-trial-status-widget' is_expected.to have_selector '#js-trial-status-widget'
is_expected.to have_selector '#js-trial-status-popover' is_expected.to have_selector '#js-trial-status-popover'
end end
it 'supplies the same popover-trigger id value to both initialization elements' do
expected_id = 'trial-status-sidebar-widget'
is_expected.to have_selector "[data-container-id=#{expected_id}]"
is_expected.to have_selector "[data-target-id=#{expected_id}]"
end
end end
shared_examples 'does record experiment subject' do shared_examples 'does record experiment subject' do
......
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