2323 create ( :role , :creator , :editor , :commenter , user : @user , plan : @plan )
2424
2525 @all_questions_ids = ( @conditional_questions . values + @non_conditional_questions . values . flatten ) . map ( &:id )
26+ @total_initial_questions = @all_questions_ids . count
2627
2728 # Answer the non-conditional questions
28- create_answers
29+ answers = create_answers
30+ @total_initial_answers = answers . values . flatten . count
2931
3032 sign_in ( @user )
3133
6264 end
6365
6466 check_answer_save_statuses ( answer_id )
65- # Expect 8 questions and answers that have ids in condition.remove_data to be removed, and 1 new answer added:
66- # 24 -8 + 1 = 17 (Answers left)
67- # 27 - 8 = 19 (Questions left)
68- expect ( page ) . to have_text ( '17/19 answered' )
69-
67+ check_question_and_answer_counts_for_plan ( condition . remove_data )
7068 check_remove_data_effect_on_answer_form_selectors ( condition . remove_data )
7169
7270 # Now uncheck checkbox_conditional_question answer.
7573 end
7674
7775 check_answer_save_statuses ( answer_id )
78- # Expect 27 questions to appear again, but the 8 answers that were removed should not be there.
79- # Also 1 answer should be removed as we unchecked @conditional_questions[:checkbox].question_options[2].text
80- # 17 ( from previous check) - 1 = 16
81- expect ( page ) . to have_text ( '16/27 answered' )
76+ num_questions , num_answers = question_and_answer_counts_for_plan
77+ # Unchecking the conditional question should unhide all of the `remove_data` questions
78+ # `- 1` from num_answers to account for now unchecked conditional question
79+ expect ( page ) . to have_text ( " #{ num_answers - 1 } / #{ num_questions } answered" )
8280 end
8381
8482 scenario 'User answers chooses checkbox option without a condition' , :js do
10199 end
102100
103101 check_answer_save_statuses ( answer_id )
104-
105- # Check questions answered in progress bar.
106- expect ( page ) . to have_text ( '25/27 answered' )
102+ check_question_and_answer_counts_for_plan
107103 end
108104 end
109105
130126 end
131127
132128 check_answer_save_statuses ( answer_id )
133-
134- # Check questions answered in progress bar.
135- # Expect 8 questions and answers that have ids in condition.remove_data to be removed, and 1 new answer added:
136- # 24 -8 + 1 = 17 (Answers left)
137- # 27 - 8 = 19 (Questions left)
138- expect ( page ) . to have_text ( '17/19 answered' )
139-
129+ check_question_and_answer_counts_for_plan ( condition . remove_data )
140130 check_remove_data_effect_on_answer_form_selectors ( condition . remove_data )
141131
142132 # Now for radiobutton_conditional_question answer, there in no unchoose option,
146136 end
147137
148138 check_answer_save_statuses ( answer_id )
149- # Check questions answered in progress bar.
150- # Expect 27 questions to appear again, but the 8 answers that were removed should not be there.
151- # Also 1 answer should be removed as we unchecked @conditional_questions[:radiobutton].question_options[2].text
152- # 17 (from previous check) - 1 = 16
153- expect ( page ) . to have_text ( '17/27 answered' )
139+ check_question_and_answer_counts_for_plan
154140 end
155141
156142 scenario 'User answers selects radiobutton option without a condition' , :js do
173159 end
174160
175161 check_answer_save_statuses ( answer_id )
176-
177- # Check questions answered in progress bar.
178- expect ( page ) . to have_text ( '25/27 answered' )
162+ check_question_and_answer_counts_for_plan
179163 end
180164 end
181165
202186 end
203187
204188 check_answer_save_statuses ( answer_id )
205-
206- # Check questions answered in progress bar.
207- # Expect 8 questions and answers that have ids in condition.remove_data to be removed, and 1 new answer added:
208- # 24 -8 + 1 = 17 (Answers left)
209- # 27 - 8 = 19 (Questions left)
210- expect ( page ) . to have_text ( '17/19 answered' )
211-
189+ check_question_and_answer_counts_for_plan ( condition . remove_data )
212190 check_remove_data_effect_on_answer_form_selectors ( condition . remove_data )
213191
214192 # Now select another option for dropdown_conditional_question.
217195 end
218196
219197 check_answer_save_statuses ( answer_id )
220- # Check questions answered in progress bar.
221- # Expect 27 questions to appear again, but the 8 answers that were removed should not be there.
222- # 17 (from previous check as we switched answer from same dropdown)
223- expect ( page ) . to have_text ( '17/27 answered' )
198+ check_question_and_answer_counts_for_plan
224199 end
225200
226201 scenario 'User answers select dropdown option without a condition' , :js do
243218 end
244219
245220 check_answer_save_statuses ( answer_id )
246-
247- # Check questions answered in progress bar.
248- expect ( page ) . to have_text ( '25/27 answered' )
221+ check_question_and_answer_counts_for_plan
249222 end
250223 end
251224 end
263236 end
264237
265238 check_answer_save_statuses ( answer_id )
266-
267- # Check questions answered in progress bar.
268- # Expect one extra answer to be added.
269- expect ( page ) . to have_text ( '25/27 answered' )
239+ check_question_and_answer_counts_for_plan
270240
271241 check_delivered_mail_for_webhook_data_and_question_data ( JSON . parse ( condition . webhook_data ) , :checkbox )
272242 end
287257 end
288258
289259 check_answer_save_statuses ( answer_id )
290-
291- # Check questions answered in progress bar.
292- # Expect one extra answer to be added.
293- expect ( page ) . to have_text ( '25/27 answered' )
260+ check_question_and_answer_counts_for_plan
294261
295262 check_delivered_mail_for_webhook_data_and_question_data ( JSON . parse ( condition . webhook_data ) , :radiobutton )
296263 end
308275 end
309276
310277 check_answer_save_statuses ( answer_id )
311-
312- # Check questions answered in progress bar.
313- # Expect one extra answer to be added.
314- expect ( page ) . to have_text ( '25/27 answered' )
278+ check_question_and_answer_counts_for_plan
315279
316280 check_delivered_mail_for_webhook_data_and_question_data ( JSON . parse ( condition . webhook_data ) , :dropdown )
317281 end
318282 end
319283
320284 private
321285
286+ def check_question_and_answer_counts_for_plan ( condition_remove_data = nil )
287+ if condition_remove_data
288+ check_condition_remove_data_effect_on_plan ( condition_remove_data . count )
289+ else
290+ # This is either a :webhook type conditional question, or a non-conditional question
291+ num_questions , num_answers = question_and_answer_counts_for_plan
292+ expect ( page ) . to have_text ( "#{ num_answers } /#{ num_questions } answered" )
293+ end
294+ end
295+
296+ def check_condition_remove_data_effect_on_plan ( num_removed_answers )
297+ num_questions , num_answers = question_and_answer_counts_for_plan
298+ # The number of plan questions has not changed in the db
299+ expect ( num_questions ) . to eql ( @total_initial_questions )
300+ # The number of plan answers in the db has changed:
301+ # - We subract num_removed_answers (i.e. `condition.remove_data.count`)
302+ # - We also `+ 1` to account for the answer saved for the conditional question in the process
303+ expected_num_answers = @total_initial_answers - num_removed_answers + 1
304+ expect ( num_answers ) . to eql ( expected_num_answers )
305+ # Check questions answered in progress bar:
306+ # - `@total_initial_questions - num_removed_answers` accounts for the now hidden (but not deleted) questions
307+ expect ( page ) . to have_text ( "#{ expected_num_answers } /#{ @total_initial_questions - num_removed_answers } answered" )
308+ end
309+
310+ def question_and_answer_counts_for_plan
311+ plan = Plan . includes ( :questions , :answers ) . first
312+ [ plan . questions . count , plan . answers . count ]
313+ end
314+
322315 def go_to_write_plan_page_and_verify_answered
323316 visit overview_plan_path ( @plan )
324317
@@ -328,8 +321,8 @@ def go_to_write_plan_page_and_verify_answered
328321 find ( 'a[data-toggle-direction=show]' ) . click
329322
330323 # Check questions answered in progress bar.
331- # 24 non-conditional questions in total answered.
332- expect ( page ) . to have_text ( '24/27 answered' )
324+ num_questions , num_answers = question_and_answer_counts_for_plan
325+ expect ( page ) . to have_text ( " #{ num_answers } / #{ num_questions } answered" )
333326 end
334327
335328 def check_remove_data_effect_on_answer_form_selectors ( remove_data )
0 commit comments