Skip to content

Commit 507dc11

Browse files
committed
Refactor specs for selection question when there is translation
This refactor tries to make the specs for the selection question a bit more explicit about what the expected use of the model is when there is a translated version of the form. At the moment, we intend to process form submissions using the English version of the form in all cases, even if one or more questions was answered in Welsh. This means we don't really care about what the behaviour of the selection question `show_answer_*` methods is if the name and value of the option differ; so this commit updates the specs to only test the behaviour with name and value the same. If in future we do start caring about what the behaviour is then we'll need to update these specs, but at least that's an explicit thing. Also we change the example selection options to use Welsh text for the name of the options, because at the moment that's the only reason the name and value of an option would be different.
1 parent 5780eb6 commit 507dc11

File tree

1 file changed

+90
-52
lines changed

1 file changed

+90
-52
lines changed

spec/models/question/selection_spec.rb

Lines changed: 90 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
RSpec.describe Question::Selection, type: :model do
44
subject(:question) { build :selection, only_one_option:, selection_options:, is_optional:, question_text: }
55

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+
1016
let(:is_optional) { false }
1117
let(:only_one_option) { "false" }
1218
let(:question_text) { Faker::Lorem.question }
@@ -67,39 +73,47 @@
6773

6874
context "when selection has a value" do
6975
before do
70-
question.selection = ["option 1"]
76+
question.selection = ["Option 1"]
7177
end
7278

7379
it "shows the answer" do
74-
expect(question.show_answer).to eq("display option 1")
80+
expect(question.show_answer).to eq("Opsiwn 1")
7581
end
7682

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 }
8085

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
86100
end
87101
end
88102

89103
it "returns invalid when selection is not one of the options" do
90-
question.selection = ["option 1000"]
104+
question.selection = ["Option 1000"]
91105
expect(question).not_to be_valid
92106
expect(question.errors[:selection]).to include(I18n.t("activemodel.errors.models.question/selection.attributes.selection.inclusion"))
93107
end
94108

95109
it "returns valid with one item selected" do
96-
question.selection = ["option 1"]
110+
question.selection = ["Option 1"]
97111
expect(question).to be_valid
98112
expect(question.errors[:selection]).to be_empty
99113
end
100114

101115
it "returns valid with two items selected" do
102-
question.selection = ["option 1", "option 2"]
116+
question.selection = ["Option 1", "Option 2"]
103117
expect(question).to be_valid
104118
expect(question.errors[:selection]).to be_empty
105119
end
@@ -144,7 +158,7 @@
144158
end
145159

146160
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")]
148162
expect(question).not_to be_valid
149163
expect(question.errors[:selection]).to include(I18n.t("activemodel.errors.models.question/selection.attributes.selection.both_none_and_value_selected"))
150164
end
@@ -155,22 +169,30 @@
155169

156170
context "when selection has a value" do
157171
before do
158-
question.selection = ["option 1"]
172+
question.selection = ["Option 1"]
159173
end
160174

161175
it "shows the answer" do
162-
expect(question.show_answer).to eq("display option 1")
176+
expect(question.show_answer).to eq("Opsiwn 1")
163177
end
164178

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 }
168181

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
174196
end
175197
end
176198
end
@@ -213,32 +235,40 @@
213235

214236
context "when selection has a value" do
215237
before do
216-
question.selection = "option 1"
238+
question.selection = "Option 1"
217239
end
218240

219241
it "shows the answer" do
220-
expect(question.show_answer).to eq("display option 1")
242+
expect(question.show_answer).to eq("Opsiwn 1")
221243
end
222244

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 }
226247

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
231261
end
232262
end
233263

234264
it "returns invalid when selection is not one of the options" do
235-
question.selection = "option 1000"
265+
question.selection = "Option 1000"
236266
expect(question).not_to be_valid
237267
expect(question.errors[:selection]).to include(I18n.t("activemodel.errors.models.question/selection.attributes.selection.inclusion"))
238268
end
239269

240270
it "returns valid with one item selected" do
241-
question.selection = "option 1"
271+
question.selection = "Option 1"
242272
expect(question).to be_valid
243273
expect(question.errors[:selection]).to be_empty
244274
end
@@ -283,21 +313,29 @@
283313

284314
context "when selection has a value" do
285315
before do
286-
question.selection = "option 1"
316+
question.selection = "Option 1"
287317
end
288318

289319
it "shows the answer" do
290-
expect(question.show_answer).to eq("display option 1")
320+
expect(question.show_answer).to eq("Opsiwn 1")
291321
end
292322

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 }
296325

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
301339
end
302340
end
303341
end
@@ -336,7 +374,7 @@
336374

337375
context "when 'None of the above' is not selected" do
338376
it "clears the none_of_the_above_answer before validating" do
339-
question.selection = ["option 1"]
377+
question.selection = ["Option 1"]
340378
question.none_of_the_above_answer = "Some answer"
341379
expect(question).to be_valid
342380
expect(question.none_of_the_above_answer).to be_nil
@@ -367,7 +405,7 @@
367405

368406
context "when 'None of the above' is not selected" do
369407
before do
370-
question.selection = ["option 1"]
408+
question.selection = ["Option 1"]
371409
end
372410

373411
it "is valid when there is no none_of_the_above_answer" do
@@ -413,7 +451,7 @@
413451

414452
context "when 'None of the above' is not selected" do
415453
before do
416-
question.selection = "option 1"
454+
question.selection = "Option 1"
417455
end
418456

419457
it "is valid when there is no none_of_the_above_answer" do
@@ -490,7 +528,7 @@
490528

491529
context "when 'None of the above' is not selected" do
492530
context "when an answer is present" do
493-
let(:selection) { "option 1" }
531+
let(:selection) { "Option 1" }
494532

495533
it "returns true" do
496534
expect(question.answered?).to be true

0 commit comments

Comments
 (0)