Skip to content

Commit bc56752

Browse files
committed
Improve checking of answer save messages
- The `expect(page).to have_text('Answered just now')` was could not be relied upon to perform the desired check for the saved answer. This is because 'Answered just now' is rendered for many answers on the page, rather than just the newly answered one. - This change also removes all `click_button 'Save'` executions. This code already appears to be absent from some saved answers. In addition, all of the saves seem to be auto-executed so this change makes the tests more consistent and removes redundancy. - Some refactoring has also been performed.
1 parent b17ba15 commit bc56752

1 file changed

Lines changed: 44 additions & 30 deletions

File tree

spec/features/questions/conditions_questions_spec.rb

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
describe 'conditions with action_type remove' do
4242
feature 'User answers a checkboxes question with a condition' do
4343
scenario 'User answers chooses checkbox option with a condition', :js do
44+
answer_id = @conditional_questions[:checkbox].id
4445
condition = create(:condition, question: @conditional_questions[:checkbox],
4546
option_list: [@conditional_questions[:checkbox].question_options[2].id],
4647
action_type: 'remove',
@@ -56,12 +57,11 @@
5657
go_to_write_plan_page_and_verify_answered
5758

5859
# Answer the checkbox_conditional_question.
59-
within("#answer-form-#{@conditional_questions[:checkbox].id}") do
60+
within("#answer-form-#{answer_id}") do
6061
check @conditional_questions[:checkbox].question_options[2].text
61-
click_button 'Save'
6262
end
6363

64-
expect(page).to have_text('Answered just now')
64+
check_answer_save_statuses(answer_id)
6565
# Expect 8 questions and answers that have ids in condition.remove_data to be removed, and 1 new answer added:
6666
# 24 -8 + 1 = 17 (Answers left)
6767
# 27 - 8 = 19 (Questions left)
@@ -70,18 +70,19 @@
7070
check_remove_data_effect_on_answer_form_selectors(condition.remove_data)
7171

7272
# Now uncheck checkbox_conditional_question answer.
73-
within("#answer-form-#{@conditional_questions[:checkbox].id}") do
73+
within("#answer-form-#{answer_id}") do
7474
uncheck @conditional_questions[:checkbox].question_options[2].text
75-
click_button 'Save'
7675
end
7776

77+
check_answer_save_statuses(answer_id)
7878
# Expect 27 questions to appear again, but the 8 answers that were removed should not be there.
7979
# Also 1 answer should be removed as we unchecked @conditional_questions[:checkbox].question_options[2].text
8080
# 17 (from previous check) - 1 = 16
8181
expect(page).to have_text('16/27 answered')
8282
end
8383

8484
scenario 'User answers chooses checkbox option without a condition', :js do
85+
answer_id = @conditional_questions[:checkbox].id
8586
create(:condition, question: @conditional_questions[:checkbox],
8687
option_list: [@conditional_questions[:checkbox].question_options[1].id],
8788
action_type: 'remove',
@@ -95,12 +96,11 @@
9596
go_to_write_plan_page_and_verify_answered
9697

9798
# Answer the checkbox_conditional_question
98-
within("#answer-form-#{@conditional_questions[:checkbox].id}") do
99+
within("#answer-form-#{answer_id}") do
99100
check @conditional_questions[:checkbox].question_options[0].text
100-
click_button 'Save'
101101
end
102102

103-
expect(page).to have_text('Answered just now')
103+
check_answer_save_statuses(answer_id)
104104

105105
# Check questions answered in progress bar.
106106
expect(page).to have_text('25/27 answered')
@@ -109,6 +109,7 @@
109109

110110
feature 'User answers a radiobutton question with a condition' do
111111
scenario 'User answers selects radiobutton option with a condition', :js do
112+
answer_id = @conditional_questions[:radiobutton].id
112113
condition = create(:condition, question: @conditional_questions[:radiobutton],
113114
option_list: [@conditional_questions[:radiobutton].question_options[2].id],
114115
action_type: 'remove',
@@ -124,12 +125,11 @@
124125
go_to_write_plan_page_and_verify_answered
125126

126127
# Answer the radiobutton_conditional_question.
127-
within("#answer-form-#{@conditional_questions[:radiobutton].id}") do
128+
within("#answer-form-#{answer_id}") do
128129
choose @conditional_questions[:radiobutton].question_options[2].text
129-
click_button 'Save'
130130
end
131131

132-
expect(page).to have_text('Answered just now')
132+
check_answer_save_statuses(answer_id)
133133

134134
# Check questions answered in progress bar.
135135
# Expect 8 questions and answers that have ids in condition.remove_data to be removed, and 1 new answer added:
@@ -141,11 +141,11 @@
141141

142142
# Now for radiobutton_conditional_question answer, there in no unchoose option,
143143
# so we switch options to a different option without any conditions.
144-
within("#answer-form-#{@conditional_questions[:radiobutton].id}") do
144+
within("#answer-form-#{answer_id}") do
145145
choose @conditional_questions[:radiobutton].question_options[0].text
146-
click_button 'Save'
147146
end
148147

148+
check_answer_save_statuses(answer_id)
149149
# Check questions answered in progress bar.
150150
# Expect 27 questions to appear again, but the 8 answers that were removed should not be there.
151151
# Also 1 answer should be removed as we unchecked @conditional_questions[:radiobutton].question_options[2].text
@@ -154,6 +154,7 @@
154154
end
155155

156156
scenario 'User answers selects radiobutton option without a condition', :js do
157+
answer_id = @conditional_questions[:radiobutton].id
157158
create(:condition, question: @conditional_questions[:radiobutton],
158159
option_list: [@conditional_questions[:radiobutton].question_options[1].id],
159160
action_type: 'remove',
@@ -167,12 +168,11 @@
167168
go_to_write_plan_page_and_verify_answered
168169

169170
# Answer the radiobutton_conditional_question.
170-
within("#answer-form-#{@conditional_questions[:radiobutton].id}") do
171+
within("#answer-form-#{answer_id}") do
171172
choose @conditional_questions[:radiobutton].question_options[0].text
172-
click_button 'Save'
173173
end
174174

175-
expect(page).to have_text('Answered just now')
175+
check_answer_save_statuses(answer_id)
176176

177177
# Check questions answered in progress bar.
178178
expect(page).to have_text('25/27 answered')
@@ -181,6 +181,7 @@
181181

182182
feature 'User answers a dropdown question with a condition' do
183183
scenario 'User answers chooses dropdown option with a condition', :js do
184+
answer_id = @conditional_questions[:dropdown].id
184185
condition = create(:condition, question: @conditional_questions[:dropdown],
185186
option_list: [@conditional_questions[:dropdown].question_options[2].id],
186187
action_type: 'remove',
@@ -196,12 +197,11 @@
196197
go_to_write_plan_page_and_verify_answered
197198

198199
# Answer the dropdown_conditional_question
199-
within("#answer-form-#{@conditional_questions[:dropdown].id}") do
200+
within("#answer-form-#{answer_id}") do
200201
select(@conditional_questions[:dropdown].question_options[2].text, from: 'answer_question_option_ids')
201-
click_button 'Save'
202202
end
203203

204-
expect(page).to have_text('Answered just now')
204+
check_answer_save_statuses(answer_id)
205205

206206
# Check questions answered in progress bar.
207207
# Expect 8 questions and answers that have ids in condition.remove_data to be removed, and 1 new answer added:
@@ -212,18 +212,19 @@
212212
check_remove_data_effect_on_answer_form_selectors(condition.remove_data)
213213

214214
# Now select another option for dropdown_conditional_question.
215-
within("#answer-form-#{@conditional_questions[:dropdown].id}") do
215+
within("#answer-form-#{answer_id}") do
216216
select(@conditional_questions[:dropdown].question_options[1].text, from: 'answer_question_option_ids')
217-
click_button 'Save'
218217
end
219218

219+
check_answer_save_statuses(answer_id)
220220
# Check questions answered in progress bar.
221221
# Expect 27 questions to appear again, but the 8 answers that were removed should not be there.
222222
# 17 (from previous check as we switched answer from same dropdown)
223223
expect(page).to have_text('17/27 answered')
224224
end
225225

226226
scenario 'User answers select dropdown option without a condition', :js do
227+
answer_id = @conditional_questions[:dropdown].id
227228
create(:condition, question: @conditional_questions[:dropdown],
228229
option_list: [@conditional_questions[:dropdown].question_options[1].id],
229230
action_type: 'remove',
@@ -237,12 +238,11 @@
237238
go_to_write_plan_page_and_verify_answered
238239

239240
# Answer the dropdown_conditional_question.
240-
within("#answer-form-#{@conditional_questions[:dropdown].id}") do
241+
within("#answer-form-#{answer_id}") do
241242
select(@conditional_questions[:dropdown].question_options[0].text, from: 'answer_question_option_ids')
242-
click_button 'Save'
243243
end
244244

245-
expect(page).to have_text('Answered just now')
245+
check_answer_save_statuses(answer_id)
246246

247247
# Check questions answered in progress bar.
248248
expect(page).to have_text('25/27 answered')
@@ -251,17 +251,18 @@
251251
end
252252
describe 'conditions with action_type add_webhook' do
253253
scenario 'User answers chooses checkbox option with a condition (with action_type: add_webhook)', :js do
254+
answer_id = @conditional_questions[:checkbox].id
254255
condition = create(:condition, :webhook, question: @conditional_questions[:checkbox],
255256
option_list: [@conditional_questions[:checkbox].question_options[2].id])
256257

257258
go_to_write_plan_page_and_verify_answered
258259

259260
# Answer the checkbox_conditional_question.
260-
within("#answer-form-#{@conditional_questions[:checkbox].id}") do
261+
within("#answer-form-#{answer_id}") do
261262
check @conditional_questions[:checkbox].question_options[2].text
262263
end
263264

264-
expect(page).to have_text('Answered just now')
265+
check_answer_save_statuses(answer_id)
265266

266267
# Check questions answered in progress bar.
267268
# Expect one extra answer to be added.
@@ -271,6 +272,7 @@
271272
end
272273

273274
scenario 'User answers chooses radiobutton option with a condition (with action_type: add_webhook)', :js do
275+
answer_id = @conditional_questions[:radiobutton].id
274276
condition = create(:condition,
275277
:webhook,
276278
question: @conditional_questions[:radiobutton],
@@ -280,11 +282,11 @@
280282

281283
# Now for radiobutton_conditional_question answer, there in no unchoose option,
282284
# so we switch options to a different option without any conditions.
283-
within("#answer-form-#{@conditional_questions[:radiobutton].id}") do
285+
within("#answer-form-#{answer_id}") do
284286
choose @conditional_questions[:radiobutton].question_options[0].text
285287
end
286288

287-
expect(page).to have_text('Answered just now')
289+
check_answer_save_statuses(answer_id)
288290

289291
# Check questions answered in progress bar.
290292
# Expect one extra answer to be added.
@@ -294,17 +296,18 @@
294296
end
295297

296298
scenario 'User answers chooses dropdown option with a condition (with action_type: add_webhook)', :js do
299+
answer_id = @conditional_questions[:dropdown].id
297300
condition = create(:condition, :webhook, question: @conditional_questions[:dropdown],
298301
option_list: [@conditional_questions[:dropdown].question_options[2].id])
299302

300303
go_to_write_plan_page_and_verify_answered
301304

302305
# Answer the dropdown_conditional_question
303-
within("#answer-form-#{@conditional_questions[:dropdown].id}") do
306+
within("#answer-form-#{answer_id}") do
304307
select(@conditional_questions[:dropdown].question_options[2].text, from: 'answer_question_option_ids')
305308
end
306309

307-
expect(page).to have_text('Answered just now')
310+
check_answer_save_statuses(answer_id)
308311

309312
# Check questions answered in progress bar.
310313
# Expect one extra answer to be added.
@@ -338,4 +341,15 @@ def check_remove_data_effect_on_answer_form_selectors(remove_data)
338341
end
339342
end
340343
end
344+
345+
# Checks for 'Saving' and 'Answered just now' messages
346+
def check_answer_save_statuses(answer_id)
347+
within("#answer-status-#{answer_id}") do
348+
saving_span = find('span.status[data-status="saving"]')
349+
expect(saving_span.text).to include('Saving')
350+
# We use `first()` because there are multiple span elements with `saved-at` status
351+
saved_span = first('span.status[data-status="saved-at"]')
352+
expect(saved_span.text).to include('Answered just now')
353+
end
354+
end
341355
end

0 commit comments

Comments
 (0)