11require "rails_helper"
22
3- RSpec . describe "CaseContact form ContactTopicAnswers and notes" , :js , type : :system do
3+ RSpec . describe "CaseContact form ContactTopicAnswers and notes" , type : :system do
44 subject do
55 sign_in user
66 visit new_case_contact_path ( casa_case )
1212 let ( :user ) { volunteer }
1313
1414 let! ( :contact_type ) { create :contact_type , casa_org : }
15- let ( :topic_count ) { 2 }
16- let! ( :contact_topics ) { create_list :contact_topic , topic_count , casa_org : }
15+ let! ( :contact_topics ) { create_list :contact_topic , 2 , casa_org : }
1716 let ( :contact_topic_questions ) { contact_topics . map ( &:question ) }
1817 let ( :select_options ) { contact_topic_questions + [ "Select a discussion topic" ] }
1918
@@ -36,7 +35,7 @@ def notes_section
3635 expect ( notes_section ) . to have_select ( class : topic_select_class , with_options : contact_topic_questions )
3736 end
3837
39- it "adds contact answers for the topics" do
38+ it "adds contact answers for the topics" , :js do
4039 subject
4140 fill_in_contact_details ( contact_types : [ contact_type . name ] )
4241
@@ -65,22 +64,19 @@ def notes_section
6564 subject
6665 fill_in_contact_details ( contact_types : [ contact_type . name ] )
6766
68- expect do
69- using_wait_time 6 do # autosave debounce may be longer than capybara's wait time
70- answer_topic contact_topics . first . question , "First discussion topic answer."
71- within notes_section do
72- expect ( page ) . to have_text "Saved" # autosave success alert
73- expect ( page ) . to have_no_text "Saved" # wait for clearing of alert
74- end
75- answer_topic contact_topics . first . question , "Changing the first topic answer."
76- within notes_section { expect ( page ) . to have_text "Saved" }
77- end
67+ answer_topic contact_topics . first . question , "First discussion topic answer."
68+ within notes_section do
69+ expect ( page ) . to have_text "Saved" # autosave success alert
70+ expect ( page ) . to have_no_text "Saved" # wait for clearing of alert
71+ end
72+ answer_topic contact_topics . first . question , "Changing the first topic answer."
73+ within notes_section { expect ( page ) . to have_text "Saved" }
7874
79- click_on "Submit"
80- expect ( page ) . to have_content ( "Case contact successfully created." )
81- end . to change ( CaseContact . active , :count ) . by ( 1 )
82- . and change ( ContactTopicAnswer , :count ) . by ( 0 ) # answer already exists on page load
75+ click_on "Submit"
76+ expect ( page ) . to have_content ( "Case contact successfully created." )
8377
78+ expect ( CaseContact . active . count ) . to eq ( 1 )
79+ expect ( ContactTopicAnswer . count ) . to eq ( 1 )
8480 case_contact = CaseContact . active . last
8581 created_answer = ContactTopicAnswer . last
8682 expect ( created_answer . contact_topic ) . to eq ( contact_topics . first )
@@ -89,7 +85,7 @@ def notes_section
8985 expect ( case_contact . contact_topic_answers ) . to include created_answer
9086 end
9187
92- it "prevents adding more answers than topics" do
88+ it "prevents adding more answers than topics" , :js do
9389 subject
9490
9591 ( contact_topics . size - 1 ) . times do
@@ -99,7 +95,7 @@ def notes_section
9995 expect ( notes_section ) . to have_button ( "Add Another Discussion Topic" , disabled : true )
10096 end
10197
102- it "disables contact topics that are already selected" do
98+ it "disables contact topics that are already selected" , :js do
10399 subject
104100
105101 topic_one_question = contact_topics . first . question
@@ -115,42 +111,40 @@ def notes_section
115111 context "when casa org has no contact topics" do
116112 let ( :contact_topics ) { [ ] }
117113
118- it "displays a field for contact.notes" do
114+ it "displays a field for contact.notes" , :js do
119115 subject
120116 expect ( page ) . to have_no_button "Add Another Discussion Topic"
121117 expect ( notes_section ) . to have_field "Additional Notes"
122118
123119 fill_in_contact_details
124120 fill_in "Additional Notes" , with : "This is a note."
125121
126- expect do
127- click_on "Submit"
128- expect ( page ) . to have_content ( "Case contact successfully created." )
129- end . to change ( CaseContact . active , :count ) . by ( 1 )
122+ click_on "Submit"
123+ expect ( page ) . to have_content ( "Case contact successfully created." )
130124
131125 contact = CaseContact . active . last
126+ expect ( CaseContact . active . count ) . to eq ( 1 )
132127 expect ( contact . contact_topic_answers ) . to be_empty
133128 expect ( contact . notes ) . to eq "This is a note."
134129 end
135130
136- it "saves 'Additional Notes' answer as contact.notes" do
131+ it "saves 'Additional Notes' answer as contact.notes" , :js do
137132 subject
138133 fill_in_contact_details ( contact_types : [ contact_type . name ] )
139134
140135 fill_in "Additional Notes" , with : "This is a fake a topic answer."
141136
142- expect do
143- click_on "Submit"
144- expect ( page ) . to have_text ( "Case contact successfully created" )
145- end . to change ( CaseContact . active , :count ) . by ( 1 )
137+ click_on "Submit"
138+ expect ( page ) . to have_text ( "Case contact successfully created" )
146139
147140 contact = CaseContact . active . last
141+ expect ( CaseContact . active . count ) . to eq ( 1 )
148142 expect ( contact . contact_topic_answers ) . to be_empty
149143 expect ( contact . notes ) . to eq "This is a fake a topic answer."
150144 end
151145 end
152146
153- context "when editing existing an case contact" do
147+ context "when editing an existing case contact" do
154148 subject do
155149 sign_in user
156150 visit edit_case_contact_path ( case_contact )
@@ -181,46 +175,46 @@ def notes_section
181175 )
182176 end
183177
184- it "can remove an existing answer" do
178+ it "can remove an existing answer" , :js do
185179 subject
186180 fill_in_contact_details
187181
188182 expect ( notes_section ) . to have_select ( class : topic_select_class , count : 2 )
189183
190- expect {
191- accept_confirm do
192- notes_section . find_button ( text : "Delete" , match : :first ) . click
193- end
184+ accept_confirm do
185+ notes_section . find_button ( text : "Delete" , match : :first ) . click
186+ end
194187
195- expect ( notes_section ) . to have_select ( class : topic_select_class , count : 1 , visible : :all )
188+ expect ( notes_section ) . to have_select ( class : topic_select_class , count : 1 , visible : :all )
196189
197- click_on "Submit"
198- expect ( page ) . to have_content ( /Case contact .* was successfully updated./ )
199- }
200- . to change ( ContactTopicAnswer , :count ) . by ( -1 )
190+ click_on "Submit"
191+ expect ( page ) . to have_content ( /Case contact .* was successfully updated./ )
201192
202193 case_contact . reload
194+ expect ( ContactTopicAnswer . count ) . to eq ( 1 )
203195 expect ( case_contact . contact_topic_answers . size ) . to eq ( 1 )
204196 end
205197 end
206198
207- it "autosaves form with answer inputs" do
208- expect { subject } . to change ( CaseContact , :count ) . by ( 1 )
209- case_contact = CaseContact . last
210- expect ( case_contact . casa_case ) . to eq casa_case
199+ it "autosaves form with answer inputs" , :js do
200+ subject
201+
211202 fill_in_contact_details (
212203 contact_made : false , medium : "In Person" , occurred_on : 1 . day . ago . to_date , hours : 1 , minutes : 5
213204 )
214205
215- expect {
216- click_on "Add Another Discussion Topic"
217- answer_topic contact_topics . first . question , "Topic One answer."
218- within autosave_alert_div do
219- find ( autosave_alert_css , text : autosave_alert_text , wait : 3 )
220- end
221- }
222- . to change ( ContactTopicAnswer , :count ) . by ( 1 )
223- case_contact . reload
206+ click_on "Add Another Discussion Topic"
207+ answer_topic contact_topics . first . question , "Topic One answer."
208+ within autosave_alert_div do
209+ find ( autosave_alert_css , text : autosave_alert_text , wait : 3 )
210+ end
211+
212+ expect ( page ) . to have_content ( "Editing Existing Case Contact" )
213+
214+ expect ( CaseContact . count ) . to eq ( 1 )
215+ case_contact = CaseContact . last
216+ expect ( case_contact . casa_case ) . to eq casa_case
217+ expect ( ContactTopicAnswer . count ) . to eq ( 1 )
224218 expect ( case_contact . contact_topic_answers . size ) . to eq ( 1 )
225219 expect ( case_contact . contact_topic_answers . last . value ) . to eq "Topic One answer."
226220
0 commit comments