|
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 | | - let(:selection_options) { [OpenStruct.new({ name: "option 1" }), OpenStruct.new({ name: "option 2" })] } |
| 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" })] } |
7 | 10 | let(:is_optional) { false } |
8 | 11 | let(:only_one_option) { "false" } |
9 | 12 | let(:question_text) { Faker::Lorem.question } |
|
64 | 67 |
|
65 | 68 | context "when selection has a value" do |
66 | 69 | before do |
67 | | - question.selection = %w[something] |
| 70 | + question.selection = ["option 1"] |
68 | 71 | end |
69 | 72 |
|
70 | 73 | it "shows the answer" do |
71 | | - expect(question.show_answer).to eq("something") |
| 74 | + expect(question.show_answer).to eq("display option 1") |
72 | 75 | end |
73 | 76 |
|
74 | 77 | it "shows the answer in show_answer_in_csv" do |
75 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, "something"]) |
| 78 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"]) |
76 | 79 | end |
77 | 80 |
|
78 | 81 | it "returns a hash for show_answer_in_json" do |
79 | 82 | expect(question.show_answer_in_json).to eq({ |
80 | | - selections: %w[something], |
81 | | - answer_text: "something", |
| 83 | + selections: ["option 1"], |
| 84 | + answer_text: "display option 1", |
82 | 85 | }) |
83 | 86 | end |
84 | 87 | end |
|
152 | 155 |
|
153 | 156 | context "when selection has a value" do |
154 | 157 | before do |
155 | | - question.selection = %w[something] |
| 158 | + question.selection = ["option 1"] |
156 | 159 | end |
157 | 160 |
|
158 | 161 | it "shows the answer" do |
159 | | - expect(question.show_answer).to eq("something") |
| 162 | + expect(question.show_answer).to eq("display option 1") |
160 | 163 | end |
161 | 164 |
|
162 | 165 | it "shows the answer in show_answer_in_csv" do |
163 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, "something"]) |
| 166 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"]) |
164 | 167 | end |
165 | 168 |
|
166 | 169 | it "returns a hash for show_answer_in_json" do |
167 | 170 | expect(question.show_answer_in_json).to eq({ |
168 | | - selections: %w[something], |
169 | | - answer_text: "something", |
| 171 | + selections: ["option 1"], |
| 172 | + answer_text: "display option 1", |
170 | 173 | }) |
171 | 174 | end |
172 | 175 | end |
|
210 | 213 |
|
211 | 214 | context "when selection has a value" do |
212 | 215 | before do |
213 | | - question.selection = "something" |
| 216 | + question.selection = "option 1" |
214 | 217 | end |
215 | 218 |
|
216 | 219 | it "shows the answer" do |
217 | | - expect(question.show_answer).to eq("something") |
| 220 | + expect(question.show_answer).to eq("display option 1") |
218 | 221 | end |
219 | 222 |
|
220 | 223 | it "shows the answer in show_answer_in_csv" do |
221 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, "something"]) |
| 224 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"]) |
222 | 225 | end |
223 | 226 |
|
224 | 227 | it "returns a hash for show_answer_in_json" do |
225 | 228 | expect(question.show_answer_in_json).to eq({ |
226 | | - answer_text: "something", |
| 229 | + answer_text: "display option 1", |
227 | 230 | }) |
228 | 231 | end |
229 | 232 | end |
|
280 | 283 |
|
281 | 284 | context "when selection has a value" do |
282 | 285 | before do |
283 | | - question.selection = "something" |
| 286 | + question.selection = "option 1" |
284 | 287 | end |
285 | 288 |
|
286 | 289 | it "shows the answer" do |
287 | | - expect(question.show_answer).to eq("something") |
| 290 | + expect(question.show_answer).to eq("display option 1") |
288 | 291 | end |
289 | 292 |
|
290 | 293 | it "shows the answer in show_answer_in_csv" do |
291 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, "something"]) |
| 294 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"]) |
292 | 295 | end |
293 | 296 |
|
294 | 297 | it "returns a hash for show_answer_in_json" do |
295 | 298 | expect(question.show_answer_in_json).to eq({ |
296 | | - answer_text: "something", |
| 299 | + answer_text: "display option 1", |
297 | 300 | }) |
298 | 301 | end |
299 | 302 | end |
|
430 | 433 |
|
431 | 434 | describe "#selection_options_with_none_of_the_above" do |
432 | 435 | let(:only_one_option) { "true" } |
433 | | - let(:none_of_the_above_option) { OpenStruct.new(name: I18n.t("page.none_of_the_above")) } |
| 436 | + let(:none_of_the_above_option) { OpenStruct.new(name: I18n.t("page.none_of_the_above"), value: I18n.t("page.none_of_the_above")) } |
434 | 437 |
|
435 | 438 | context "when the user can select 'None of the above'" do |
436 | 439 | let(:is_optional) { true } |
|
463 | 466 |
|
464 | 467 | describe "#autocomplete_component?" do |
465 | 468 | context "when there are 30 selection options" do |
466 | | - let(:selection_options) { Array.new(30).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence) } } |
| 469 | + let(:selection_options) { Array.new(30).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence, value: Faker::Lorem.sentence) } } |
467 | 470 |
|
468 | 471 | it "returns false" do |
469 | 472 | expect(question.autocomplete_component?).to be false |
470 | 473 | end |
471 | 474 | end |
472 | 475 |
|
473 | 476 | context "when there are more than 30 selection options" do |
474 | | - let(:selection_options) { Array.new(31).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence) } } |
| 477 | + let(:selection_options) { Array.new(31).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence, value: Faker::Lorem.sentence) } } |
475 | 478 |
|
476 | 479 | it "returns true" do |
477 | 480 | expect(question.autocomplete_component?).to be true |
|
0 commit comments