Commit 8cb327ea authored by Dallas Reedy's avatar Dallas Reedy

Move more logic out of the view & back into the helper

Reduce the logic necessary for the view down to two helpers:

1. show_trial_status_widget?(group)
1. trial_status_widget_experiment_enabled?(group) ← has a side effect

Also fixed the `new_subscriptions_path` to use the root_group rather
than the group.
parent 5264399c
...@@ -12,4 +12,22 @@ module TrialStatusWidgetHelper ...@@ -12,4 +12,22 @@ module TrialStatusWidgetHelper
def plan_title_for_group(group) def plan_title_for_group(group)
group.gitlab_subscription&.plan_title group.gitlab_subscription&.plan_title
end end
def show_trial_status_widget?(group)
return false unless billing_plans_and_trials_available?
eligible_for_trial_status_widget?(group)
end
# Note: This method has a side-effect in that it records the given group as a
# participant in the experiment (if the experiment is active at all) in the
# `experiment_subjects` table.
def trial_status_widget_experiment_enabled?(group)
experiment_key = :show_trial_status_in_sidebar
# Record the top-level group as a Growth::Conversion experiment participant
record_experiment_group(experiment_key, group)
experiment_enabled?(experiment_key, subject: group)
end
end end
-# Return early if this app instance does not have namespace plans enabled
- return unless billing_plans_and_trials_available?
-# Only top-level groups can have trials & plans -# Only top-level groups can have trials & plans
- root_group = group.root_ancestor - root_group = group.root_ancestor
-# Return without rendering if the top-level group & current user are not - return unless show_trial_status_widget?(root_group)
-# eligible to see the trial status widget - return unless trial_status_widget_experiment_enabled?(root_group)
- return unless eligible_for_trial_status_widget?(root_group)
-# Growth::Conversion experiment-related stuff...
- experiment_key = :show_trial_status_in_sidebar
-# * Record the top-level group as a Growth::Conversion experiment participant
- record_experiment_group(experiment_key, root_group)
-# * But return without rendering if the group is not receiving the candidate
-# variant experience
- 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: { container_id: 'trial-status-sidebar-widget',
...@@ -26,6 +15,6 @@ ...@@ -26,6 +15,6 @@
group_name: root_group.name, group_name: root_group.name,
plan_name: plan_title_for_group(root_group), plan_name: plan_title_for_group(root_group),
plans_href: group_billings_path(root_group), plans_href: group_billings_path(root_group),
purchase_href: new_subscriptions_path(namespace_id: group.id, plan_id: '2c92a0fc5a83f01d015aa6db83c45aac'), purchase_href: new_subscriptions_path(namespace_id: root_group.id, plan_id: '2c92a0fc5a83f01d015aa6db83c45aac'),
target_id: 'trial-status-sidebar-widget', target_id: 'trial-status-sidebar-widget',
trial_end_date: root_group.trial_ends_on } } trial_end_date: root_group.trial_ends_on } }
...@@ -69,4 +69,62 @@ RSpec.describe TrialStatusWidgetHelper do ...@@ -69,4 +69,62 @@ RSpec.describe TrialStatusWidgetHelper do
it { is_expected.to eq(title) } it { is_expected.to eq(title) }
end end
end end
describe '#show_trial_status_widget?' do
let(:group) { instance_double(Group) }
before do
allow(helper).to receive(:billing_plans_and_trials_available?).and_return(trials_available)
allow(helper).to receive(:eligible_for_trial_status_widget?).with(group).and_return(eligible_for_widget)
end
subject { helper.show_trial_status_widget?(group) }
where(:trials_available, :eligible_for_widget, :result) do
true | true | true
true | false | false
false | true | false
false | false | false
end
with_them do
it { is_expected.to eq(result) }
end
end
describe '#trial_status_widget_experiment_enabled?' do
let(:experiment_key) { :show_trial_status_in_sidebar }
let(:group) { instance_double(Group) }
before do
allow(helper).to receive(:experiment_enabled?).with(experiment_key, subject: group).and_return(experiment_enabled)
allow(helper).to receive(:record_experiment_group)
end
subject { helper.trial_status_widget_experiment_enabled?(group) }
context 'when the experiment is not enabled for the given group' do
let(:experiment_enabled) { false }
it { is_expected.to be_falsey }
it 'records the group as an experiment participant' do
expect(helper).to receive(:record_experiment_group).with(experiment_key, group)
subject
end
end
context 'when the experiment is enabled for the given group' do
let(:experiment_enabled) { true }
it { is_expected.to be_truthy }
it 'records the group as an experiment participant' do
expect(helper).to receive(:record_experiment_group).with(experiment_key, group)
subject
end
end
end
end end
...@@ -12,19 +12,16 @@ RSpec.describe 'layouts/nav/sidebar/_group' do ...@@ -12,19 +12,16 @@ RSpec.describe 'layouts/nav/sidebar/_group' do
let(:user) { create(:user) } let(:user) { create(:user) }
describe 'trial status widget', :aggregate_failures do describe 'trial status widget', :aggregate_failures do
using RSpec::Parameterized::TableSyntax
let!(:gitlab_subscription) { create(:gitlab_subscription, :active_trial, namespace: group) } let!(:gitlab_subscription) { create(:gitlab_subscription, :active_trial, namespace: group) }
let(:trials_available) { false } let(:show_widget) { false }
let(:experiment_enabled) { false } let(:experiment_enabled) { false }
let(:eligible_for_widget) { false }
before do before do
allow(view).to receive(:billing_plans_and_trials_available?).and_return(trials_available) allow(view).to receive(:show_trial_status_widget?).and_return(show_widget)
allow(view).to receive(:eligible_for_trial_status_widget?).and_return(eligible_for_widget) allow(view).to receive(:trial_status_widget_experiment_enabled?).and_return(experiment_enabled)
allow(view).to receive(:record_experiment_group)
allow(view).to receive(:experiment_enabled?).and_return(experiment_enabled)
render render
end end
...@@ -51,30 +48,15 @@ RSpec.describe 'layouts/nav/sidebar/_group' do ...@@ -51,30 +48,15 @@ RSpec.describe 'layouts/nav/sidebar/_group' do
end end
end end
context 'when billing plans & trials are not available' do where :show_widget, :experiment_enabled, :examples_to_include do
include_examples 'does not render' true | true | 'does render'
true | false | 'does not render'
false | true | 'does not render'
false | false | 'does not render'
end end
context 'when billing plans & trials are available' do with_them do
let(:trials_available) { true } include_examples(params[:examples_to_include])
context 'but the group and/or user are not eligible to see the widget' do
include_examples 'does not render'
end
context 'and the group and/or user are eligible to see the widget' do
let(:eligible_for_widget) { true }
context 'but the experiment is not enabled for the group' do
include_examples 'does not render'
end
context 'and the experiment is enabled for the group' do
let(:experiment_enabled) { true }
include_examples 'does render'
end
end
end end
end 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