Browse Source

Set header and suffix based on previous answer

pull/198/head
baarkerlounger 4 years ago
parent
commit
ea29ec9688
  1. 12
      app/models/form/question.rb
  2. 4
      app/views/form/_numeric_question.html.erb
  3. 57
      config/forms/2021_2022.json
  4. 31
      spec/views/form/page_view_spec.rb

12
app/models/form/question.rb

@ -82,18 +82,6 @@ class Form::Question
case_log[id].present?
end
def prefix_text(case_log = nil)
return prefix.to_s unless prefix && case_log.respond_to?(prefix)
case_log.public_send(prefix).to_s
end
def suffix_text(case_log = nil)
return suffix.to_s unless suffix && case_log.respond_to?(suffix)
case_log.public_send(suffix).to_s
end
private
def checkbox_answer_label(case_log)

4
app/views/form/_numeric_question.html.erb

@ -6,7 +6,7 @@
hint: { text: question.hint_text&.html_safe },
min: question.min, max: question.max, step: question.step,
width: question.width, :readonly => question.read_only?,
prefix_text: question.prefix_text(@case_log),
suffix_text: question.suffix_text(@case_log),
prefix_text: question.prefix.to_s,
suffix_text: question.suffix.to_s,
**stimulus_html_attributes(question)
%>

57
config/forms/2021_2022.json

@ -1824,7 +1824,8 @@
}
}
},
"net_income": {
"weekly_net_income": {
"depends_on": { "net_income_known": "Yes – the household has a weekly income" },
"header": "",
"description": "",
"questions": {
@ -1836,7 +1837,59 @@
"min": 0,
"step": "1",
"prefix": "£",
"suffix": "incfreq"
"suffix": "every week"
}
},
"soft_validations": {
"override_net_income_validation": {
"check_answer_label": "Net income confirmed?",
"type": "validation_override",
"answer_options": {
"override_net_income_validation": "Yes"
}
}
}
},
"monthly_net_income": {
"depends_on": { "net_income_known": "Yes – the household has a monthly income" },
"header": "",
"description": "",
"questions": {
"earnings": {
"check_answer_label": "Income",
"header": "How much income does the household have in total every month?",
"hint_text": "",
"type": "numeric",
"min": 0,
"step": "1",
"prefix": "£",
"suffix": "every month"
}
},
"soft_validations": {
"override_net_income_validation": {
"check_answer_label": "Net income confirmed?",
"type": "validation_override",
"answer_options": {
"override_net_income_validation": "Yes"
}
}
}
},
"yearly_net_income": {
"depends_on": { "net_income_known": "Yes – the household has a yearly income" },
"header": "",
"description": "",
"questions": {
"earnings": {
"check_answer_label": "Income",
"header": "How much income does the household have in total every year?",
"hint_text": "",
"type": "numeric",
"min": 0,
"step": "1",
"prefix": "£",
"suffix": "every year"
}
},
"soft_validations": {

31
spec/views/form/page_view_spec.rb

@ -19,6 +19,14 @@ RSpec.describe "form/page" do
end
end
before do
assign(:case_log, case_log)
assign(:page, page)
assign(:subsection, subsection)
assign_attributes(question, attribs)
render
end
after do
# Revert any changes we've made to avoid affecting other specs as the form,
# subsection, page, question objects being acted on are in memory
@ -26,36 +34,17 @@ RSpec.describe "form/page" do
end
context "given a numeric question with prefix and suffix" do
let(:attribs) { { type: "numeric", prefix: "£", suffix: "incfreq" } }
let(:net_income_known) { "Yes – the household has a weekly income" }
let(:expected_suffix) { "Weekly" }
before do
case_log.update!(net_income_known: net_income_known)
assign(:case_log, case_log)
assign(:page, page)
assign(:subsection, subsection)
assign_attributes(question, attribs)
render
end
let(:attribs) { { type: "numeric", prefix: "£", suffix: "every week" } }
it "renders prefix and suffix text" do
expect(rendered).to match(/govuk-input__prefix/)
expect(rendered).to match(/£/)
expect(rendered).to match(/govuk-input__suffix/)
expect(rendered).to match(expected_suffix)
expect(rendered).to match("every week")
end
end
context "given a question with extra guidance" do
before do
assign(:case_log, case_log)
assign(:page, page)
assign(:subsection, subsection)
assign_attributes(question, attribs)
render
end
let(:expected_guidance) { /What counts as income?/ }
context "with radio type" do

Loading…
Cancel
Save