Skip to content

Commit 44665eb

Browse files
committed
Pass form_name to analytics in draft_question inputs
Passes the form_name from the draft question to the analytics event for any input that includes a draft_question attribute. This fixes an issue where some inputs weren't including the form name in the analytics event.
1 parent 9b54956 commit 44665eb

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

app/input_objects/base_input.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ class BaseInput
88

99
def set_validation_error_logging_attributes
1010
CurrentLoggingAttributes.validation_errors = errors.map { |error| "#{error.attribute}: #{error.type}" } if errors.any?
11-
form_name = form.name if defined?(form) && form.present?
1211

1312
errors.each do |error|
1413
AnalyticsService.track_validation_errors(input_object_name: self.class.name, field: error.attribute, error_type: error.type, form_name:)
1514
end
1615
end
16+
17+
def form_name
18+
return form.name if defined?(form) && form.present?
19+
return draft_question.form_name if defined?(draft_question) && draft_question.present?
20+
21+
nil
22+
end
1723
end

spec/input_objects/base_input_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class TestInputWithForm < BaseInput
1414
validates :email, format: { with: /.*@.*/, message: "must be a valid email address" }
1515
end
1616

17+
class TestInputWithDraftQuestion < BaseInput
18+
attr_accessor :name, :email, :draft_question
19+
20+
validates :name, presence: true
21+
validates :email, format: { with: /.*@.*/, message: "must be a valid email address" }
22+
end
23+
1724
RSpec.describe BaseInput do
1825
describe "validation error logging" do
1926
let(:analytics_service) { class_double(AnalyticsService).as_stubbed_const }
@@ -96,6 +103,39 @@ class TestInputWithForm < BaseInput
96103
end
97104
end
98105
end
106+
107+
context "when the draft_question is defined in the input" do
108+
let(:input) { TestInputWithDraftQuestion.new }
109+
110+
context "when the draft_question is nil" do
111+
it "does not include a form name in the validation errors" do
112+
input.valid?
113+
114+
expect(analytics_service).to have_received(:track_validation_errors)
115+
.with(input_object_name: "TestInputWithDraftQuestion", form_name: nil, field: :name, error_type: :blank)
116+
117+
expect(analytics_service).to have_received(:track_validation_errors)
118+
.with(input_object_name: "TestInputWithDraftQuestion", form_name: nil, field: :email, error_type: :invalid)
119+
end
120+
end
121+
122+
context "when the draft_question is present" do
123+
let(:form_name) { "Apply for a juggling licence" }
124+
let(:form) { create :form, name: form_name }
125+
let(:draft_question) { build :draft_question, form_id: form.id }
126+
let(:input) { TestInputWithDraftQuestion.new(draft_question:) }
127+
128+
it "includes the form name in the validation errors" do
129+
input.valid?
130+
131+
expect(analytics_service).to have_received(:track_validation_errors)
132+
.with(input_object_name: "TestInputWithDraftQuestion", form_name:, field: :name, error_type: :blank)
133+
134+
expect(analytics_service).to have_received(:track_validation_errors)
135+
.with(input_object_name: "TestInputWithDraftQuestion", form_name:, field: :email, error_type: :invalid)
136+
end
137+
end
138+
end
99139
end
100140
end
101141
end

0 commit comments

Comments
 (0)