Commit 43ea422c authored by minahilnichols's avatar minahilnichols

Review changes and specs clean up

parent 27739ce0
...@@ -5,16 +5,13 @@ import RegistrationForm from 'ee/registrations/components/company_form.vue'; ...@@ -5,16 +5,13 @@ import RegistrationForm from 'ee/registrations/components/company_form.vue';
export default () => { export default () => {
const el = document.querySelector('#js-company-registration-form'); const el = document.querySelector('#js-company-registration-form');
const { submitPath, trial, role, jtbd, comment } = el.dataset; const { submitPath, trial } = el.dataset;
return new Vue({ return new Vue({
el, el,
apolloProvider, apolloProvider,
provide: { provide: {
submitPath, submitPath,
role,
jtbd,
comment,
}, },
render(createElement) { render(createElement) {
return createElement(RegistrationForm, { return createElement(RegistrationForm, {
......
...@@ -28,7 +28,7 @@ export default { ...@@ -28,7 +28,7 @@ export default {
CountryOrRegionSelector, CountryOrRegionSelector,
RegistrationTrialToggle, RegistrationTrialToggle,
}, },
inject: ['submitPath', 'role', 'jtbd', 'comment'], inject: ['submitPath'],
props: { props: {
trial: { trial: {
type: Boolean, type: Boolean,
...@@ -86,9 +86,6 @@ export default { ...@@ -86,9 +86,6 @@ export default {
<template> <template>
<gl-form :action="submitPath" method="post"> <gl-form :action="submitPath" method="post">
<input :value="$options.csrf.token" type="hidden" name="authenticity_token" /> <input :value="$options.csrf.token" type="hidden" name="authenticity_token" />
<input :value="role" type="hidden" name="role" data-testid="role" />
<input :value="jtbd" type="hidden" name="jtbd" data-testid="jtbd" />
<input :value="comment" type="hidden" name="comment" data-testid="comment" />
<gl-form-text class="gl-font-base gl-text-gray-400 gl-pb-3">{{ descriptionText }}</gl-form-text> <gl-form-text class="gl-font-base gl-text-gray-400 gl-pb-3">{{ descriptionText }}</gl-form-text>
<div class="gl-display-flex gl-flex-direction-column gl-sm-flex-direction-row gl-mt-5"> <div class="gl-display-flex gl-flex-direction-column gl-sm-flex-direction-row gl-mt-5">
<gl-form-group <gl-form-group
......
...@@ -12,15 +12,13 @@ module Registrations ...@@ -12,15 +12,13 @@ module Registrations
end end
def create def create
result = GitlabSubscriptions::CreateTrialOrLeadService.new.execute( result = GitlabSubscriptions::CreateTrialOrLeadService.new(user: current_user, params: permitted_params).execute
user: current_user,
params: permitted_params
)
if result[:success] if result.success?
redirect_to new_users_sign_up_groups_project_path(redirect_param) redirect_to new_users_sign_up_groups_project_path(redirect_param)
else else
render :new flash.now[:alert] = result[:message]
render :new, status: result.http_status
end end
end end
......
...@@ -26,12 +26,9 @@ module EE ...@@ -26,12 +26,9 @@ module EE
end end
def create_company_form_data def create_company_form_data
submit_params = glm_params.merge(params.slice(:trial, :role, :jtbd, :comment).to_unsafe_h.symbolize_keys)
{ {
submit_path: users_sign_up_company_path(glm_params), submit_path: users_sign_up_company_path(submit_params)
trial: params[:trial],
role: params[:role],
jtbd: params[:jtbd],
comment: params[:comment]
} }
end end
......
# frozen_string_literal: true # frozen_string_literal: true
module GitlabSubscriptions module GitlabSubscriptions
class CreateTrialOrLeadService class CreateTrialOrLeadService
def execute(user:, params:) def initialize(user:, params:)
params = params.merge(hardcoded_values).merge(user_values(user)) @params = params.merge(hardcoded_values).merge(user_values(user))
response = if Gitlab::Utils.to_boolean(params[:trial])
client.generate_trial(trial_user: params)
else
client.generate_hand_raise_lead(params)
end end
if response[:success] def execute
ServiceResponse.success generate_response
else result
ServiceResponse.error(message: response.dig(:data, :errors))
end
end end
private private
attr_reader :response, :params
def hardcoded_values def hardcoded_values
{ {
provider: 'gitlab', provider: 'gitlab',
...@@ -39,6 +33,26 @@ module GitlabSubscriptions ...@@ -39,6 +33,26 @@ module GitlabSubscriptions
} }
end end
def result
if response[:success]
ServiceResponse.success
else
ServiceResponse.error(message: response.dig(:data, :errors), http_status: :unprocessable_entity)
end
end
def generate_response
@response = if trial?
client.generate_trial(trial_user: params)
else
client.generate_hand_raise_lead(params)
end
end
def trial?
Gitlab::Utils.to_boolean(params[:trial])
end
def client def client
Gitlab::SubscriptionPortal::Client Gitlab::SubscriptionPortal::Client
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Registrations::CompanyController do RSpec.describe Registrations::CompanyController do
let_it_be(:user) { create(:user, email_opted_in: true, last_name: 'Doe') } let_it_be(:user) { create(:user) }
let(:logged_in) { true } let(:logged_in) { true }
...@@ -70,15 +70,17 @@ RSpec.describe Registrations::CompanyController do ...@@ -70,15 +70,17 @@ RSpec.describe Registrations::CompanyController do
end end
with_them do with_them do
it 'creates trial or lead and redirects to the corect path' do it 'creates trial or lead and redirects to the correct path' do
expect_next_instance_of(GitlabSubscriptions::CreateTrialOrLeadService) do |service| expect_next_instance_of(
expect(service).to receive(:execute).with({ GitlabSubscriptions::CreateTrialOrLeadService,
user: user, user: user,
params: ActionController::Parameters.new(params.merge({ trial: trial })).permit! params: ActionController::Parameters.new(params.merge({ trial: trial })).permit!
}).and_return({ success: true }) ) do |service|
expect(service).to receive(:execute).and_return(ServiceResponse.success)
end end
post :create, params: params.merge({ trial: trial }) post :create, params: params.merge({ trial: trial })
expect(response).to have_gitlab_http_status(:redirect) expect(response).to have_gitlab_http_status(:redirect)
expect(response).to redirect_to(new_users_sign_up_groups_project_path(redirect_query)) expect(response).to redirect_to(new_users_sign_up_groups_project_path(redirect_query))
end end
...@@ -95,6 +97,7 @@ RSpec.describe Registrations::CompanyController do ...@@ -95,6 +97,7 @@ RSpec.describe Registrations::CompanyController do
end end
post :create, params: params.merge({ trial: trial }) post :create, params: params.merge({ trial: trial })
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:new) expect(response).to render_template(:new)
end end
......
...@@ -19,6 +19,7 @@ RSpec.describe 'Company Information', :js do ...@@ -19,6 +19,7 @@ RSpec.describe 'Company Information', :js do
context 'send company information to create lead' do context 'send company information to create lead' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
let(:params) do let(:params) do
{ {
company_name: 'GitLab', company_name: 'GitLab',
...@@ -27,16 +28,17 @@ RSpec.describe 'Company Information', :js do ...@@ -27,16 +28,17 @@ RSpec.describe 'Company Information', :js do
country: 'US', country: 'US',
state: 'CA', state: 'CA',
website_url: 'gitlab.com', website_url: 'gitlab.com',
role: '', trial: 'false'
jtbd: '',
comment: ''
} }
end end
where(trial: %w[true false]) where(:service_response, :current_path) do
ServiceResponse.success | new_users_sign_up_groups_project_path
ServiceResponse.error(message: 'failed') | users_sign_up_company_path
end
with_them do with_them do
it 'proceeds to next step' do it 'redirects to correct path' do
fill_in 'company_name', with: 'GitLab' fill_in 'company_name', with: 'GitLab'
select '1 - 99', from: 'company_size' select '1 - 99', from: 'company_size'
select 'United States of America', from: 'country' select 'United States of America', from: 'country'
...@@ -44,18 +46,17 @@ RSpec.describe 'Company Information', :js do ...@@ -44,18 +46,17 @@ RSpec.describe 'Company Information', :js do
fill_in 'website_url', with: 'gitlab.com' fill_in 'website_url', with: 'gitlab.com'
fill_in 'phone_number', with: '+1 23 456-78-90' fill_in 'phone_number', with: '+1 23 456-78-90'
# defaults to trial off, click to turn on expect_next_instance_of(
click_button class: 'gl-toggle' if Gitlab::Utils.to_boolean(trial) GitlabSubscriptions::CreateTrialOrLeadService,
expect_next_instance_of(GitlabSubscriptions::CreateTrialOrLeadService) do |service|
expect(service).to receive(:execute).with({
user: user, user: user,
params: ActionController::Parameters.new(params.merge({ trial: trial })).permit! params: ActionController::Parameters.new(params).permit!
}).and_return({ success: true }) ) do |service|
expect(service).to receive(:execute).and_return(service_response)
end end
click_button 'Continue' click_button 'Continue'
expect(page).to have_current_path(new_users_sign_up_groups_project_path, ignore_query: true)
expect(page).to have_current_path(current_path, ignore_query: true)
end end
end end
end end
......
...@@ -18,9 +18,6 @@ describe('RegistrationForm', () => { ...@@ -18,9 +18,6 @@ describe('RegistrationForm', () => {
localVue, localVue,
provide: { provide: {
submitPath: SUBMIT_PATH, submitPath: SUBMIT_PATH,
role: 'Software Engineer',
jtbd: 'Jobs to be done',
comment: 'A comment',
}, },
propsData: { trial: true }, propsData: { trial: true },
}); });
...@@ -71,15 +68,6 @@ describe('RegistrationForm', () => { ...@@ -71,15 +68,6 @@ describe('RegistrationForm', () => {
`('has the correct form input in the form content', ({ testid }) => { `('has the correct form input in the form content', ({ testid }) => {
expect(findFormInput(testid).exists()).toBe(true); expect(findFormInput(testid).exists()).toBe(true);
}); });
it.each`
testid | value
${'role'} | ${'Software Engineer'}
${'jtbd'} | ${'Jobs to be done'}
${'comment'} | ${'A comment'}
`('has the hidden injected value for $testid', ({ testid, value }) => {
expect(findFormInput(testid).attributes('value')).toBe(value);
});
}); });
describe('submitting', () => { describe('submitting', () => {
......
...@@ -57,19 +57,35 @@ RSpec.describe EE::TrialHelper do ...@@ -57,19 +57,35 @@ RSpec.describe EE::TrialHelper do
end end
describe '#create_company_form_data' do describe '#create_company_form_data' do
let(:user) do let(:extra_params) do
double('User', first_name: '_first_name_', last_name: '_last_name_') {
role: '_params_role_',
jtbd: '_params_jtbd_',
comment: '_params_comment_'
}
end
let(:params) do
ActionController::Parameters.new(extra_params)
end end
before do before do
allow(helper).to receive(:current_user).and_return(user) allow(helper).to receive(:params).and_return(params)
end end
it 'provides expected form data' do it 'provides expected form data' do
keys = [:submit_path, :trial, :role, :jtbd, :comment] keys = [:submit_path]
expect(helper.create_company_form_data.keys.map(&:to_sym)).to match_array(keys) expect(helper.create_company_form_data.keys.map(&:to_sym)).to match_array(keys)
end end
it 'allows overriding data with params' do
submit_path = {
submit_path: '/users/sign_up/company?comment=_params_comment_&jtbd=_params_jtbd_&role=_params_role_'
}
expect(helper.create_company_form_data).to match(submit_path)
end
end end
describe '#should_ask_company_question?' do describe '#should_ask_company_question?' do
......
...@@ -7,6 +7,7 @@ RSpec.describe GitlabSubscriptions::CreateTrialOrLeadService do ...@@ -7,6 +7,7 @@ RSpec.describe GitlabSubscriptions::CreateTrialOrLeadService do
describe '#execute' do describe '#execute' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:trial, :service) do where(:trial, :service) do
'true' | :generate_trial 'true' | :generate_trial
'false' | :generate_hand_raise_lead 'false' | :generate_hand_raise_lead
...@@ -16,7 +17,8 @@ RSpec.describe GitlabSubscriptions::CreateTrialOrLeadService do ...@@ -16,7 +17,8 @@ RSpec.describe GitlabSubscriptions::CreateTrialOrLeadService do
it 'successfully creates a trial or lead' do it 'successfully creates a trial or lead' do
allow(Gitlab::SubscriptionPortal::Client).to receive(service).and_return({ success: true }) allow(Gitlab::SubscriptionPortal::Client).to receive(service).and_return({ success: true })
result = described_class.new.execute(**{ user: user, params: { trial: trial } }) result = described_class.new(**{ user: user, params: { trial: trial } }).execute
expect(result.is_a?(ServiceResponse)).to be true expect(result.is_a?(ServiceResponse)).to be true
expect(result.success?).to be true expect(result.success?).to be true
end end
...@@ -24,7 +26,8 @@ RSpec.describe GitlabSubscriptions::CreateTrialOrLeadService do ...@@ -24,7 +26,8 @@ RSpec.describe GitlabSubscriptions::CreateTrialOrLeadService do
it 'error while creating trial or lead' do it 'error while creating trial or lead' do
allow(Gitlab::SubscriptionPortal::Client).to receive(service).and_return({ success: false }) allow(Gitlab::SubscriptionPortal::Client).to receive(service).and_return({ success: false })
result = described_class.new.execute(**{ user: user, params: { trial: trial } }) result = described_class.new(**{ user: user, params: { trial: trial } }).execute
expect(result.is_a?(ServiceResponse)).to be true expect(result.is_a?(ServiceResponse)).to be true
expect(result.success?).to be false expect(result.success?).to be false
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