@@ -14,6 +14,13 @@ class TestInputWithForm < BaseInput
14
14
validates :email , format : { with : /.*@.*/ , message : "must be a valid email address" }
15
15
end
16
16
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
+
17
24
RSpec . describe BaseInput do
18
25
describe "validation error logging" do
19
26
let ( :analytics_service ) { class_double ( AnalyticsService ) . as_stubbed_const }
@@ -96,6 +103,39 @@ class TestInputWithForm < BaseInput
96
103
end
97
104
end
98
105
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
99
139
end
100
140
end
101
141
end
0 commit comments