|
3 | 3 | RSpec.describe Question::Selection, type: :model do |
4 | 4 | subject(:question) { build :selection, only_one_option:, selection_options:, is_optional:, question_text: } |
5 | 5 |
|
6 | | - # We use different values for name and value to make it clear which is which in the tests. |
7 | | - # Name would be the text in current locale and value doesn't change between locales. |
8 | | - # For English forms they would be the same, but for Welsh forms they would be different. |
9 | | - let(:selection_options) { [OpenStruct.new({ name: "display option 1", value: "option 1" }), OpenStruct.new({ name: "display option 2", value: "option 2" })] } |
| 6 | + # For an English form the name and value are the same, for Welsh forms they would be different. |
| 7 | + # For form translations the name is the text in the current locale, but the value is always equal |
| 8 | + # to the name in the English form, i.e. the value doesn't change between locales. |
| 9 | + # The name is rendered for form fillers, but the value is stored. |
| 10 | + # For form processors we always want to show the English text, but we use the English form document |
| 11 | + # when generating submssions so the behaviour with the Welsh translation shouldn't matter. |
| 12 | + let(:selection_options) { cy_selection_options } |
| 13 | + let(:en_selection_options) { [OpenStruct.new({ name: "Option 1", value: "Option 1" }), OpenStruct.new({ name: "Option 2", value: "Option 2" })] } |
| 14 | + let(:cy_selection_options) { [OpenStruct.new({ name: "Opsiwn 1", value: "Option 1" }), OpenStruct.new({ name: "Opsiwn 2", value: "Option 2" })] } |
| 15 | + |
10 | 16 | let(:is_optional) { false } |
11 | 17 | let(:only_one_option) { "false" } |
12 | 18 | let(:question_text) { Faker::Lorem.question } |
|
67 | 73 |
|
68 | 74 | context "when selection has a value" do |
69 | 75 | before do |
70 | | - question.selection = ["option 1"] |
| 76 | + question.selection = ["Option 1"] |
71 | 77 | end |
72 | 78 |
|
73 | 79 | it "shows the answer" do |
74 | | - expect(question.show_answer).to eq("display option 1") |
| 80 | + expect(question.show_answer).to eq("Opsiwn 1") |
75 | 81 | end |
76 | 82 |
|
77 | | - it "shows the answer in show_answer_in_csv" do |
78 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"]) |
79 | | - end |
| 83 | + context "when creating a submission for a form processor" do |
| 84 | + let(:selection_options) { en_selection_options } |
80 | 85 |
|
81 | | - it "returns a hash for show_answer_in_json" do |
82 | | - expect(question.show_answer_in_json).to eq({ |
83 | | - selections: ["option 1"], |
84 | | - answer_text: "display option 1", |
85 | | - }) |
| 86 | + it "shows the answer" do |
| 87 | + expect(question.show_answer).to eq("Option 1") |
| 88 | + end |
| 89 | + |
| 90 | + it "shows the answer in show_answer_in_csv" do |
| 91 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, "Option 1"]) |
| 92 | + end |
| 93 | + |
| 94 | + it "returns a hash for show_answer_in_json" do |
| 95 | + expect(question.show_answer_in_json).to eq({ |
| 96 | + selections: ["Option 1"], |
| 97 | + answer_text: "Option 1", |
| 98 | + }) |
| 99 | + end |
86 | 100 | end |
87 | 101 | end |
88 | 102 |
|
89 | 103 | it "returns invalid when selection is not one of the options" do |
90 | | - question.selection = ["option 1000"] |
| 104 | + question.selection = ["Option 1000"] |
91 | 105 | expect(question).not_to be_valid |
92 | 106 | expect(question.errors[:selection]).to include(I18n.t("activemodel.errors.models.question/selection.attributes.selection.inclusion")) |
93 | 107 | end |
94 | 108 |
|
95 | 109 | it "returns valid with one item selected" do |
96 | | - question.selection = ["option 1"] |
| 110 | + question.selection = ["Option 1"] |
97 | 111 | expect(question).to be_valid |
98 | 112 | expect(question.errors[:selection]).to be_empty |
99 | 113 | end |
100 | 114 |
|
101 | 115 | it "returns valid with two items selected" do |
102 | | - question.selection = ["option 1", "option 2"] |
| 116 | + question.selection = ["Option 1", "Option 2"] |
103 | 117 | expect(question).to be_valid |
104 | 118 | expect(question.errors[:selection]).to be_empty |
105 | 119 | end |
|
144 | 158 | end |
145 | 159 |
|
146 | 160 | it "returns invalid with both an item and none selected" do |
147 | | - question.selection = ["option 1", I18n.t("page.none_of_the_above")] |
| 161 | + question.selection = ["Option 1", I18n.t("page.none_of_the_above")] |
148 | 162 | expect(question).not_to be_valid |
149 | 163 | expect(question.errors[:selection]).to include(I18n.t("activemodel.errors.models.question/selection.attributes.selection.both_none_and_value_selected")) |
150 | 164 | end |
|
155 | 169 |
|
156 | 170 | context "when selection has a value" do |
157 | 171 | before do |
158 | | - question.selection = ["option 1"] |
| 172 | + question.selection = ["Option 1"] |
159 | 173 | end |
160 | 174 |
|
161 | 175 | it "shows the answer" do |
162 | | - expect(question.show_answer).to eq("display option 1") |
| 176 | + expect(question.show_answer).to eq("Opsiwn 1") |
163 | 177 | end |
164 | 178 |
|
165 | | - it "shows the answer in show_answer_in_csv" do |
166 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"]) |
167 | | - end |
| 179 | + context "when creating a submission for a form processor" do |
| 180 | + let(:selection_options) { en_selection_options } |
168 | 181 |
|
169 | | - it "returns a hash for show_answer_in_json" do |
170 | | - expect(question.show_answer_in_json).to eq({ |
171 | | - selections: ["option 1"], |
172 | | - answer_text: "display option 1", |
173 | | - }) |
| 182 | + it "shows the answer" do |
| 183 | + expect(question.show_answer).to eq("Option 1") |
| 184 | + end |
| 185 | + |
| 186 | + it "shows the answer in show_answer_in_csv" do |
| 187 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, "Option 1"]) |
| 188 | + end |
| 189 | + |
| 190 | + it "returns a hash for show_answer_in_json" do |
| 191 | + expect(question.show_answer_in_json).to eq({ |
| 192 | + selections: ["Option 1"], |
| 193 | + answer_text: "Option 1", |
| 194 | + }) |
| 195 | + end |
174 | 196 | end |
175 | 197 | end |
176 | 198 | end |
|
213 | 235 |
|
214 | 236 | context "when selection has a value" do |
215 | 237 | before do |
216 | | - question.selection = "option 1" |
| 238 | + question.selection = "Option 1" |
217 | 239 | end |
218 | 240 |
|
219 | 241 | it "shows the answer" do |
220 | | - expect(question.show_answer).to eq("display option 1") |
| 242 | + expect(question.show_answer).to eq("Opsiwn 1") |
221 | 243 | end |
222 | 244 |
|
223 | | - it "shows the answer in show_answer_in_csv" do |
224 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"]) |
225 | | - end |
| 245 | + context "when creting a submission for a form form processor" do |
| 246 | + let(:selection_options) { en_selection_options } |
226 | 247 |
|
227 | | - it "returns a hash for show_answer_in_json" do |
228 | | - expect(question.show_answer_in_json).to eq({ |
229 | | - answer_text: "display option 1", |
230 | | - }) |
| 248 | + it "shows the answer" do |
| 249 | + expect(question.show_answer).to eq("Option 1") |
| 250 | + end |
| 251 | + |
| 252 | + it "shows the answer in show_answer_in_csv" do |
| 253 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, "Option 1"]) |
| 254 | + end |
| 255 | + |
| 256 | + it "returns a hash for show_answer_in_json" do |
| 257 | + expect(question.show_answer_in_json).to eq({ |
| 258 | + answer_text: "Option 1", |
| 259 | + }) |
| 260 | + end |
231 | 261 | end |
232 | 262 | end |
233 | 263 |
|
234 | 264 | it "returns invalid when selection is not one of the options" do |
235 | | - question.selection = "option 1000" |
| 265 | + question.selection = "Option 1000" |
236 | 266 | expect(question).not_to be_valid |
237 | 267 | expect(question.errors[:selection]).to include(I18n.t("activemodel.errors.models.question/selection.attributes.selection.inclusion")) |
238 | 268 | end |
239 | 269 |
|
240 | 270 | it "returns valid with one item selected" do |
241 | | - question.selection = "option 1" |
| 271 | + question.selection = "Option 1" |
242 | 272 | expect(question).to be_valid |
243 | 273 | expect(question.errors[:selection]).to be_empty |
244 | 274 | end |
|
283 | 313 |
|
284 | 314 | context "when selection has a value" do |
285 | 315 | before do |
286 | | - question.selection = "option 1" |
| 316 | + question.selection = "Option 1" |
287 | 317 | end |
288 | 318 |
|
289 | 319 | it "shows the answer" do |
290 | | - expect(question.show_answer).to eq("display option 1") |
| 320 | + expect(question.show_answer).to eq("Opsiwn 1") |
291 | 321 | end |
292 | 322 |
|
293 | | - it "shows the answer in show_answer_in_csv" do |
294 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"]) |
295 | | - end |
| 323 | + context "when creating a submission for a form processor" do |
| 324 | + let(:selection_options) { en_selection_options } |
296 | 325 |
|
297 | | - it "returns a hash for show_answer_in_json" do |
298 | | - expect(question.show_answer_in_json).to eq({ |
299 | | - answer_text: "display option 1", |
300 | | - }) |
| 326 | + it "shows the answer" do |
| 327 | + expect(question.show_answer).to eq("Option 1") |
| 328 | + end |
| 329 | + |
| 330 | + it "shows the answer in show_answer_in_csv" do |
| 331 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, "Option 1"]) |
| 332 | + end |
| 333 | + |
| 334 | + it "returns a hash for show_answer_in_json" do |
| 335 | + expect(question.show_answer_in_json).to eq({ |
| 336 | + answer_text: "Option 1", |
| 337 | + }) |
| 338 | + end |
301 | 339 | end |
302 | 340 | end |
303 | 341 | end |
|
336 | 374 |
|
337 | 375 | context "when 'None of the above' is not selected" do |
338 | 376 | it "clears the none_of_the_above_answer before validating" do |
339 | | - question.selection = ["option 1"] |
| 377 | + question.selection = ["Option 1"] |
340 | 378 | question.none_of_the_above_answer = "Some answer" |
341 | 379 | expect(question).to be_valid |
342 | 380 | expect(question.none_of_the_above_answer).to be_nil |
|
367 | 405 |
|
368 | 406 | context "when 'None of the above' is not selected" do |
369 | 407 | before do |
370 | | - question.selection = ["option 1"] |
| 408 | + question.selection = ["Option 1"] |
371 | 409 | end |
372 | 410 |
|
373 | 411 | it "is valid when there is no none_of_the_above_answer" do |
|
413 | 451 |
|
414 | 452 | context "when 'None of the above' is not selected" do |
415 | 453 | before do |
416 | | - question.selection = "option 1" |
| 454 | + question.selection = "Option 1" |
417 | 455 | end |
418 | 456 |
|
419 | 457 | it "is valid when there is no none_of_the_above_answer" do |
|
490 | 528 |
|
491 | 529 | context "when 'None of the above' is not selected" do |
492 | 530 | context "when an answer is present" do |
493 | | - let(:selection) { "option 1" } |
| 531 | + let(:selection) { "Option 1" } |
494 | 532 |
|
495 | 533 | it "returns true" do |
496 | 534 | expect(question.answered?).to be true |
|
0 commit comments