|
12 | 12 | @section1, @section2, @section3 = template.sections |
13 | 13 |
|
14 | 14 | # Different types of questions (than can have conditional options) |
15 | | - @conditional_questions = create_conditional_questions(5) |
| 15 | + @conditional_questions = create_conditional_questions(3) |
16 | 16 |
|
17 | 17 | # Questions that do not have conditional options for adding or removing |
18 | | - @non_conditional_questions = create_non_conditional_questions(7, 3) |
| 18 | + @non_conditional_questions = create_non_conditional_questions(3, 3) |
19 | 19 |
|
20 | 20 | @plan = create(:plan, :creator, template: template) |
21 | 21 | @user = @plan.owner |
|
41 | 41 | # NOTE: Checkboxes allow for multiple options to be selected. |
42 | 42 | context 'with conditional checkbox question' do |
43 | 43 | it 'handles single option (with condition) in option_list ' do |
| 44 | + non_conditional_question_index = 0 |
44 | 45 | condition = create(:condition, question: @conditional_questions[:checkbox], |
45 | 46 | option_list: [@conditional_questions[:checkbox].question_options[2].id], |
46 | 47 | action_type: 'remove', |
47 | | - remove_data: non_conditional_questions_ids_by_index(5)) |
| 48 | + remove_data: non_conditional_questions_ids_by_index(non_conditional_question_index)) |
48 | 49 |
|
49 | 50 | # We chose an option that is in the option_list of the condition defined above. Note that |
50 | 51 | # the text sent by UI is an empty string. |
|
64 | 65 | # Check hide/show questions lists sent to frontend. |
65 | 66 | check_question_ids_to_show_and_hide(json, condition.remove_data) |
66 | 67 |
|
67 | | - # Check Answers in database (persisted). Expect removed answers to be destroyed. |
68 | | - # Answers destroyed eare easier checked using array of ids rather than individually as in example |
69 | | - # expect(Answer.exists?(@answers[:textarea][5].id)).to be_falsey. |
70 | | - removed_answers = @answers.map { |_, answers| answers[5].id } |
| 68 | + # Verify that answers for the `removed_data` questions were deleted from the DB. |
| 69 | + # NOTE: `@answers` contains only answers to non-conditional questions. |
| 70 | + # So we use `non_conditional_question_index` to locate the corresponding answer |
| 71 | + # for each type of non-conditional question. |
| 72 | + removed_answers = @answers.map { |_, answers| answers[non_conditional_question_index].id } |
71 | 73 | expect(Answer.where(id: removed_answers).pluck(:id)).to be_empty |
72 | 74 | # Answers left |
73 | 75 | expect(Answer.where(id: @all_answers_ids).pluck(:id)).to match_array( |
|
78 | 80 | create(:condition, question: @conditional_questions[:checkbox], |
79 | 81 | option_list: [@conditional_questions[:checkbox].question_options[1].id], |
80 | 82 | action_type: 'remove', |
81 | | - remove_data: non_conditional_questions_ids_by_index(3)) |
| 83 | + remove_data: non_conditional_questions_ids_by_index(2)) |
82 | 84 |
|
83 | 85 | create(:condition, question: @conditional_questions[:checkbox], |
84 | | - option_list: [@conditional_questions[:checkbox].question_options[4].id], |
| 86 | + option_list: [@conditional_questions[:checkbox].question_options[2].id], |
85 | 87 | action_type: 'remove', |
86 | 88 | remove_data: non_conditional_questions_ids_by_index(0)) |
87 | 89 |
|
|
103 | 105 |
|
104 | 106 | it 'handles multiple options (some with conditions) in option_list' do |
105 | 107 | condition1 = create(:condition, question: @conditional_questions[:checkbox], |
106 | | - option_list: [@conditional_questions[:checkbox].question_options[2].id], |
| 108 | + option_list: [@conditional_questions[:checkbox].question_options[1].id], |
107 | 109 | action_type: 'remove', |
108 | 110 | remove_data: non_conditional_questions_ids_by_index(0)) |
109 | 111 |
|
110 | 112 | condition2 = create(:condition, question: @conditional_questions[:checkbox], |
111 | | - option_list: [@conditional_questions[:checkbox].question_options[4].id], |
| 113 | + option_list: [@conditional_questions[:checkbox].question_options[2].id], |
112 | 114 | action_type: 'remove', |
113 | | - remove_data: non_conditional_questions_ids_by_index(3)) |
| 115 | + remove_data: non_conditional_questions_ids_by_index(2)) |
114 | 116 |
|
115 | 117 | # We choose options that is in the option_list of the conditions defined above as well as an option |
116 | 118 | # with no condition defined. |
117 | 119 | args = { |
118 | | - question_option_ids: [@conditional_questions[:checkbox].question_options[1].id, |
119 | | - @conditional_questions[:checkbox].question_options[2].id, |
120 | | - @conditional_questions[:checkbox].question_options[4].id], |
| 120 | + question_option_ids: [@conditional_questions[:checkbox].question_options[0].id, |
| 121 | + @conditional_questions[:checkbox].question_options[1].id, |
| 122 | + @conditional_questions[:checkbox].question_options[2].id], |
121 | 123 | user_id: @user.id, |
122 | 124 | question_id: @conditional_questions[:checkbox].id, |
123 | 125 | plan_id: @plan.id, |
|
137 | 139 | condition = create(:condition, question: @conditional_questions[:radiobutton], |
138 | 140 | option_list: [@conditional_questions[:radiobutton].question_options[2].id], |
139 | 141 | action_type: 'remove', |
140 | | - remove_data: non_conditional_questions_ids_by_index(5)) |
| 142 | + remove_data: non_conditional_questions_ids_by_index(2)) |
141 | 143 |
|
142 | 144 | # We choose an option that is in the option_list of the condition defined above. |
143 | 145 | args = { |
|
158 | 160 | create(:condition, question: @conditional_questions[:radiobutton], |
159 | 161 | option_list: [@conditional_questions[:radiobutton].question_options[1].id], |
160 | 162 | action_type: 'remove', |
161 | | - remove_data: non_conditional_questions_ids_by_index(3)) |
| 163 | + remove_data: non_conditional_questions_ids_by_index(2)) |
162 | 164 |
|
163 | 165 | create(:condition, question: @conditional_questions[:radiobutton], |
164 | | - option_list: [@conditional_questions[:radiobutton].question_options[4].id], |
| 166 | + option_list: [@conditional_questions[:radiobutton].question_options[2].id], |
165 | 167 | action_type: 'remove', |
166 | 168 | remove_data: non_conditional_questions_ids_by_index(0)) |
167 | 169 |
|
|
188 | 190 | condition = create(:condition, question: @conditional_questions[:dropdown], |
189 | 191 | option_list: [@conditional_questions[:dropdown].question_options[2].id], |
190 | 192 | action_type: 'remove', |
191 | | - remove_data: non_conditional_questions_ids_by_index(5)) |
| 193 | + remove_data: non_conditional_questions_ids_by_index(2)) |
192 | 194 |
|
193 | 195 | # We chose an option that is in the option_list of the condition defined above. |
194 | 196 | args = { |
|
209 | 211 | create(:condition, question: @conditional_questions[:dropdown], |
210 | 212 | option_list: [@conditional_questions[:dropdown].question_options[1].id], |
211 | 213 | action_type: 'remove', |
212 | | - remove_data: non_conditional_questions_ids_by_index(3)) |
| 214 | + remove_data: non_conditional_questions_ids_by_index(2)) |
213 | 215 |
|
214 | 216 | create(:condition, question: @conditional_questions[:dropdown], |
215 | | - option_list: [@conditional_questions[:dropdown].question_options[4].id], |
| 217 | + option_list: [@conditional_questions[:dropdown].question_options[2].id], |
216 | 218 | action_type: 'remove', |
217 | 219 | remove_data: non_conditional_questions_ids_by_index(0)) |
218 | 220 |
|
|
276 | 278 | add_webhook_condition = create(:condition, |
277 | 279 | :webhook, |
278 | 280 | question: @conditional_questions[:checkbox], |
279 | | - option_list: [@conditional_questions[:checkbox].question_options[2].id]) |
| 281 | + option_list: [@conditional_questions[:checkbox].question_options[1].id]) |
280 | 282 |
|
281 | 283 | condition2 = create(:condition, question: @conditional_questions[:checkbox], |
282 | | - option_list: [@conditional_questions[:checkbox].question_options[4].id], |
| 284 | + option_list: [@conditional_questions[:checkbox].question_options[2].id], |
283 | 285 | action_type: 'remove', |
284 | | - remove_data: non_conditional_questions_ids_by_index(3)) |
| 286 | + remove_data: non_conditional_questions_ids_by_index(2)) |
285 | 287 |
|
286 | 288 | # We chose an option that is in the option_list of the condition defined above. Note that |
287 | 289 | # the text sent by UI is an empty string. |
288 | 290 | args = { |
289 | 291 | text: '', |
290 | | - question_option_ids: [@conditional_questions[:checkbox].question_options[2].id, |
291 | | - @conditional_questions[:checkbox].question_options[4].id, |
292 | | - @conditional_questions[:checkbox].question_options[1].id], |
| 292 | + question_option_ids: [@conditional_questions[:checkbox].question_options[0].id, |
| 293 | + @conditional_questions[:checkbox].question_options[1].id, |
| 294 | + @conditional_questions[:checkbox].question_options[2].id], |
293 | 295 | user_id: @user.id, |
294 | 296 | question_id: @conditional_questions[:checkbox].id, |
295 | 297 | plan_id: @plan.id, |
|
0 commit comments