|
41 | 41 | # So all Conditions are created with option_list with a single option id.
|
42 | 42 |
|
43 | 43 | describe 'conditions with action_type remove' do
|
44 |
| - feature 'User answers a checkboxes question with a condition' do |
45 |
| - scenario 'User answers chooses checkbox option with a condition', :js do |
46 |
| - answer_id = @conditional_questions[:checkbox].id |
47 |
| - condition = create(:condition, question: @conditional_questions[:checkbox], |
48 |
| - option_list: [@conditional_questions[:checkbox].question_options[2].id], |
| 44 | + feature 'User answers a question with a condition' do |
| 45 | + scenario 'User answers chooses an option with a condition', :js do |
| 46 | + # Choose a conditional question at random (may be of type :checkbox, :radiobutton, or :dropdown) |
| 47 | + question_type = @conditional_questions.keys.sample |
| 48 | + conditional_question = @conditional_questions[question_type] |
| 49 | + conditional_question_remove_option = conditional_question.question_options[0] |
| 50 | + conditional_question_other_option = conditional_question.question_options[1] |
| 51 | + answer_id = conditional_question.id |
| 52 | + condition = create(:condition, question: conditional_question, |
| 53 | + option_list: [conditional_question_remove_option.id], |
49 | 54 | action_type: 'remove',
|
50 | 55 | remove_data: [@non_conditional_questions[:textarea][0].id,
|
51 | 56 | @non_conditional_questions[:textfield][1].id,
|
|
58 | 63 |
|
59 | 64 | go_to_write_plan_page_and_verify_answered
|
60 | 65 |
|
61 |
| - # Answer the checkbox_conditional_question. |
| 66 | + # Answer the conditional_question |
62 | 67 | within("#answer-form-#{answer_id}") do
|
63 |
| - check @conditional_questions[:checkbox].question_options[2].text |
| 68 | + answer_conditional_question(conditional_question_remove_option, question_type) |
64 | 69 | end
|
65 | 70 |
|
66 | 71 | check_answer_save_statuses(answer_id)
|
67 | 72 | check_question_and_answer_counts_for_plan(condition.remove_data)
|
68 | 73 | check_remove_data_effect_on_answer_form_selectors(condition.remove_data)
|
69 | 74 |
|
70 |
| - # Now uncheck checkbox_conditional_question answer. |
| 75 | + question_option = determine_question_option(conditional_question_remove_option, |
| 76 | + conditional_question_other_option, question_type) |
| 77 | + # If :checkbox, uncheck the previously checked option |
| 78 | + # Else, select a different :dropdown/:radiobutton option |
71 | 79 | within("#answer-form-#{answer_id}") do
|
72 |
| - uncheck @conditional_questions[:checkbox].question_options[2].text |
| 80 | + answer_conditional_question(question_option, question_type, 'uncheck') |
73 | 81 | end
|
74 | 82 |
|
75 | 83 | check_answer_save_statuses(answer_id)
|
76 | 84 | 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") |
80 |
| - end |
81 |
| - |
82 |
| - scenario 'User answers chooses checkbox option without a condition', :js do |
83 |
| - answer_id = @conditional_questions[:checkbox].id |
84 |
| - create(:condition, question: @conditional_questions[:checkbox], |
85 |
| - option_list: [@conditional_questions[:checkbox].question_options[1].id], |
86 |
| - action_type: 'remove', |
87 |
| - remove_data: non_conditional_questions_ids_by_index(2)) |
88 |
| - |
89 |
| - create(:condition, question: @conditional_questions[:checkbox], |
90 |
| - option_list: [@conditional_questions[:checkbox].question_options[2].id], |
91 |
| - action_type: 'remove', |
92 |
| - remove_data: non_conditional_questions_ids_by_index(0)) |
93 |
| - |
94 |
| - go_to_write_plan_page_and_verify_answered |
95 |
| - |
96 |
| - # Answer the checkbox_conditional_question |
97 |
| - within("#answer-form-#{answer_id}") do |
98 |
| - check @conditional_questions[:checkbox].question_options[0].text |
99 |
| - end |
100 |
| - |
101 |
| - check_answer_save_statuses(answer_id) |
102 |
| - check_question_and_answer_counts_for_plan |
103 |
| - end |
104 |
| - end |
105 |
| - |
106 |
| - feature 'User answers a radiobutton question with a condition' do |
107 |
| - scenario 'User answers selects radiobutton option with a condition', :js do |
108 |
| - answer_id = @conditional_questions[:radiobutton].id |
109 |
| - condition = create(:condition, question: @conditional_questions[:radiobutton], |
110 |
| - option_list: [@conditional_questions[:radiobutton].question_options[2].id], |
111 |
| - action_type: 'remove', |
112 |
| - remove_data: [@non_conditional_questions[:textarea][0].id, |
113 |
| - @non_conditional_questions[:textfield][1].id, |
114 |
| - @non_conditional_questions[:date][2].id, |
115 |
| - @non_conditional_questions[:rda_metadata][0].id, |
116 |
| - @non_conditional_questions[:checkbox][1].id, |
117 |
| - @non_conditional_questions[:radiobutton][2].id, |
118 |
| - @non_conditional_questions[:dropdown][0].id, |
119 |
| - @non_conditional_questions[:multiselectbox][1].id]) |
120 |
| - |
121 |
| - go_to_write_plan_page_and_verify_answered |
122 |
| - |
123 |
| - # Answer the radiobutton_conditional_question. |
124 |
| - within("#answer-form-#{answer_id}") do |
125 |
| - choose @conditional_questions[:radiobutton].question_options[2].text |
126 |
| - end |
127 |
| - |
128 |
| - check_answer_save_statuses(answer_id) |
129 |
| - check_question_and_answer_counts_for_plan(condition.remove_data) |
130 |
| - check_remove_data_effect_on_answer_form_selectors(condition.remove_data) |
131 |
| - |
132 |
| - # Now for radiobutton_conditional_question answer, there in no unchoose option, |
133 |
| - # so we switch options to a different option without any conditions. |
134 |
| - within("#answer-form-#{answer_id}") do |
135 |
| - choose @conditional_questions[:radiobutton].question_options[0].text |
136 |
| - end |
137 |
| - |
138 |
| - check_answer_save_statuses(answer_id) |
139 |
| - check_question_and_answer_counts_for_plan |
140 |
| - end |
141 |
| - |
142 |
| - scenario 'User answers selects radiobutton option without a condition', :js do |
143 |
| - answer_id = @conditional_questions[:radiobutton].id |
144 |
| - create(:condition, question: @conditional_questions[:radiobutton], |
145 |
| - option_list: [@conditional_questions[:radiobutton].question_options[1].id], |
146 |
| - action_type: 'remove', |
147 |
| - remove_data: non_conditional_questions_ids_by_index(2)) |
148 |
| - |
149 |
| - create(:condition, question: @conditional_questions[:radiobutton], |
150 |
| - option_list: [@conditional_questions[:radiobutton].question_options[2].id], |
151 |
| - action_type: 'remove', |
152 |
| - remove_data: non_conditional_questions_ids_by_index(0)) |
153 |
| - |
154 |
| - go_to_write_plan_page_and_verify_answered |
155 |
| - |
156 |
| - # Answer the radiobutton_conditional_question. |
157 |
| - within("#answer-form-#{answer_id}") do |
158 |
| - choose @conditional_questions[:radiobutton].question_options[0].text |
159 |
| - end |
160 |
| - |
161 |
| - check_answer_save_statuses(answer_id) |
162 |
| - check_question_and_answer_counts_for_plan |
163 |
| - end |
164 |
| - end |
165 | 85 |
|
166 |
| - feature 'User answers a dropdown question with a condition' do |
167 |
| - scenario 'User answers chooses dropdown option with a condition', :js do |
168 |
| - answer_id = @conditional_questions[:dropdown].id |
169 |
| - condition = create(:condition, question: @conditional_questions[:dropdown], |
170 |
| - option_list: [@conditional_questions[:dropdown].question_options[2].id], |
171 |
| - action_type: 'remove', |
172 |
| - remove_data: [@non_conditional_questions[:textarea][0].id, |
173 |
| - @non_conditional_questions[:textfield][1].id, |
174 |
| - @non_conditional_questions[:date][2].id, |
175 |
| - @non_conditional_questions[:rda_metadata][0].id, |
176 |
| - @non_conditional_questions[:checkbox][1].id, |
177 |
| - @non_conditional_questions[:radiobutton][2].id, |
178 |
| - @non_conditional_questions[:dropdown][0].id, |
179 |
| - @non_conditional_questions[:multiselectbox][1].id]) |
180 |
| - |
181 |
| - go_to_write_plan_page_and_verify_answered |
182 |
| - |
183 |
| - # Answer the dropdown_conditional_question |
184 |
| - within("#answer-form-#{answer_id}") do |
185 |
| - select(@conditional_questions[:dropdown].question_options[2].text, from: 'answer_question_option_ids') |
186 |
| - end |
187 |
| - |
188 |
| - check_answer_save_statuses(answer_id) |
189 |
| - check_question_and_answer_counts_for_plan(condition.remove_data) |
190 |
| - check_remove_data_effect_on_answer_form_selectors(condition.remove_data) |
191 |
| - |
192 |
| - # Now select another option for dropdown_conditional_question. |
193 |
| - within("#answer-form-#{answer_id}") do |
194 |
| - select(@conditional_questions[:dropdown].question_options[1].text, from: 'answer_question_option_ids') |
195 |
| - end |
196 |
| - |
197 |
| - check_answer_save_statuses(answer_id) |
198 |
| - check_question_and_answer_counts_for_plan |
| 86 | + # Undoing the conditional question should unhide all of the `remove_data` questions |
| 87 | + # `-= 1` is needed for :checkbox because unchecking removes an answer |
| 88 | + # (:dropdown and :radiobutton simply select a different answer) |
| 89 | + num_answers -= 1 if question_type == :checkbox |
| 90 | + expect(page).to have_text("#{num_answers}/#{num_questions} answered") |
199 | 91 | end
|
200 | 92 |
|
201 |
| - scenario 'User answers select dropdown option without a condition', :js do |
202 |
| - answer_id = @conditional_questions[:dropdown].id |
203 |
| - create(:condition, question: @conditional_questions[:dropdown], |
204 |
| - option_list: [@conditional_questions[:dropdown].question_options[1].id], |
| 93 | + scenario 'User answers chooses an option without a condition', :js do |
| 94 | + # Choose a conditional question at random (may be of type :checkbox, :radiobutton, or :dropdown) |
| 95 | + question_type = @conditional_questions.keys.sample |
| 96 | + conditional_question = @conditional_questions[question_type] |
| 97 | + conditional_question_other_option = conditional_question.question_options[0] |
| 98 | + answer_id = conditional_question.id |
| 99 | + create(:condition, question: conditional_question, |
| 100 | + option_list: [conditional_question.question_options[1].id], |
205 | 101 | action_type: 'remove',
|
206 | 102 | remove_data: non_conditional_questions_ids_by_index(2))
|
207 | 103 |
|
208 |
| - create(:condition, question: @conditional_questions[:dropdown], |
209 |
| - option_list: [@conditional_questions[:dropdown].question_options[2].id], |
| 104 | + create(:condition, question: conditional_question, |
| 105 | + option_list: [conditional_question.question_options[2].id], |
210 | 106 | action_type: 'remove',
|
211 | 107 | remove_data: non_conditional_questions_ids_by_index(0))
|
212 | 108 |
|
213 | 109 | go_to_write_plan_page_and_verify_answered
|
214 | 110 |
|
215 |
| - # Answer the dropdown_conditional_question. |
| 111 | + # Answer the conditional_question |
216 | 112 | within("#answer-form-#{answer_id}") do
|
217 |
| - select(@conditional_questions[:dropdown].question_options[0].text, from: 'answer_question_option_ids') |
| 113 | + answer_conditional_question(conditional_question_other_option, question_type) |
218 | 114 | end
|
219 | 115 |
|
220 | 116 | check_answer_save_statuses(answer_id)
|
221 | 117 | check_question_and_answer_counts_for_plan
|
222 | 118 | end
|
223 | 119 | end
|
224 | 120 | end
|
| 121 | + |
225 | 122 | describe 'conditions with action_type add_webhook' do
|
226 |
| - scenario 'User answers chooses checkbox option with a condition (with action_type: add_webhook)', :js do |
227 |
| - answer_id = @conditional_questions[:checkbox].id |
228 |
| - condition = create(:condition, :webhook, question: @conditional_questions[:checkbox], |
229 |
| - option_list: [@conditional_questions[:checkbox].question_options[2].id]) |
| 123 | + scenario 'User answers chooses an option with a condition (with action_type: add_webhook)', :js do |
| 124 | + # Choose a conditional question at random (may be of type :checkbox, :radiobutton, or :dropdown) |
| 125 | + question_type = @conditional_questions.keys.sample |
| 126 | + conditional_question = @conditional_questions[question_type] |
| 127 | + conditional_question_webhook_option = conditional_question.question_options[2] |
| 128 | + answer_id = conditional_question.id |
| 129 | + condition = create(:condition, :webhook, question: conditional_question, |
| 130 | + option_list: [conditional_question_webhook_option.id]) |
230 | 131 |
|
231 | 132 | go_to_write_plan_page_and_verify_answered
|
232 | 133 |
|
233 |
| - # Answer the checkbox_conditional_question. |
| 134 | + # Answer the conditional_question |
234 | 135 | within("#answer-form-#{answer_id}") do
|
235 |
| - check @conditional_questions[:checkbox].question_options[2].text |
| 136 | + answer_conditional_question(conditional_question_webhook_option, question_type) |
236 | 137 | end
|
237 | 138 |
|
238 | 139 | check_answer_save_statuses(answer_id)
|
239 | 140 | check_question_and_answer_counts_for_plan
|
240 | 141 |
|
241 | 142 | check_delivered_mail_for_webhook_data_and_question_data(JSON.parse(condition.webhook_data), :checkbox)
|
242 | 143 | end
|
243 |
| - |
244 |
| - scenario 'User answers chooses radiobutton option with a condition (with action_type: add_webhook)', :js do |
245 |
| - answer_id = @conditional_questions[:radiobutton].id |
246 |
| - condition = create(:condition, |
247 |
| - :webhook, |
248 |
| - question: @conditional_questions[:radiobutton], |
249 |
| - option_list: [@conditional_questions[:radiobutton].question_options[0].id]) |
250 |
| - |
251 |
| - go_to_write_plan_page_and_verify_answered |
252 |
| - |
253 |
| - # Now for radiobutton_conditional_question answer, there in no unchoose option, |
254 |
| - # so we switch options to a different option without any conditions. |
255 |
| - within("#answer-form-#{answer_id}") do |
256 |
| - choose @conditional_questions[:radiobutton].question_options[0].text |
257 |
| - end |
258 |
| - |
259 |
| - check_answer_save_statuses(answer_id) |
260 |
| - check_question_and_answer_counts_for_plan |
261 |
| - |
262 |
| - check_delivered_mail_for_webhook_data_and_question_data(JSON.parse(condition.webhook_data), :radiobutton) |
263 |
| - end |
264 |
| - |
265 |
| - scenario 'User answers chooses dropdown option with a condition (with action_type: add_webhook)', :js do |
266 |
| - answer_id = @conditional_questions[:dropdown].id |
267 |
| - condition = create(:condition, :webhook, question: @conditional_questions[:dropdown], |
268 |
| - option_list: [@conditional_questions[:dropdown].question_options[2].id]) |
269 |
| - |
270 |
| - go_to_write_plan_page_and_verify_answered |
271 |
| - |
272 |
| - # Answer the dropdown_conditional_question |
273 |
| - within("#answer-form-#{answer_id}") do |
274 |
| - select(@conditional_questions[:dropdown].question_options[2].text, from: 'answer_question_option_ids') |
275 |
| - end |
276 |
| - |
277 |
| - check_answer_save_statuses(answer_id) |
278 |
| - check_question_and_answer_counts_for_plan |
279 |
| - |
280 |
| - check_delivered_mail_for_webhook_data_and_question_data(JSON.parse(condition.webhook_data), :dropdown) |
281 |
| - end |
282 | 144 | end
|
283 | 145 |
|
284 | 146 | private
|
@@ -345,4 +207,28 @@ def check_answer_save_statuses(answer_id)
|
345 | 207 | expect(saved_span.text).to include('Answered just now')
|
346 | 208 | end
|
347 | 209 | end
|
| 210 | + |
| 211 | + def answer_conditional_question(question_option, question_type, check_type = 'check') |
| 212 | + case question_type |
| 213 | + when :checkbox |
| 214 | + # if it is a checkbox question, we need to know check_type as well ('check' vs 'uncheck') |
| 215 | + if check_type == 'check' |
| 216 | + check question_option.text |
| 217 | + else |
| 218 | + uncheck question_option.text |
| 219 | + end |
| 220 | + when :radiobutton |
| 221 | + choose question_option.text |
| 222 | + when :dropdown |
| 223 | + select(question_option.text, from: 'answer_question_option_ids') |
| 224 | + end |
| 225 | + end |
| 226 | + |
| 227 | + def determine_question_option(original_question, new_question, question_type) |
| 228 | + # If :checkbox question, we want to return the original question to be unchecked |
| 229 | + return original_question if question_type == :checkbox |
| 230 | + |
| 231 | + # Else we want to return a different question to be selected via :radiobutton or :dropdown |
| 232 | + new_question |
| 233 | + end |
348 | 234 | end
|
0 commit comments