From 5b44a3832823e5a8d4f39ff7250fd823a4ff3711 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 24 Jan 2022 15:47:27 +0000 Subject: [PATCH] Fix armed forces case --- app/models/form/page.rb | 21 +++++++++------- .../validations/household_validations.rb | 8 ------- app/views/form/page.html.erb | 2 +- config/forms/2021_2022.json | 17 +++++++------ .../form/conditional_questions_spec.rb | 8 +++---- spec/fixtures/forms/2021_2022.json | 24 +++---------------- .../conditional_questions_helper_spec.rb | 4 ++-- 7 files changed, 30 insertions(+), 54 deletions(-) diff --git a/app/models/form/page.rb b/app/models/form/page.rb index 3e30704bc..4d6ab21ff 100644 --- a/app/models/form/page.rb +++ b/app/models/form/page.rb @@ -27,14 +27,19 @@ class Form::Page subsection.enabled?(case_log) && depends_on_met(case_log) end - # We expect to render only one radio question (with conditionals) - # def questions_to_render - # if questions.first.type == "radio" - # [questions.first] - # else - # questions - # end - # end + def non_conditional_questions + @non_conditional_questions ||= questions.reject do |q| + conditional_questions.include?(q.id) + end + end + + def conditional_questions + @conditional_questions ||= questions.flat_map { |q| + next if q.conditional_for.blank? + + q.conditional_for.keys + }.compact + end private diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index d361e15a1..edc414d9a 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -26,20 +26,12 @@ module Validations::HouseholdValidations end def validate_armed_forces_injured(record) - if (record.armedforces == "A current or former regular in the UK Armed Forces (excluding National Service)" || record.armedforces == "A current or former reserve in the UK Armed Forces (excluding National Service)") && record.reservist.blank? - record.errors.add :reservist, I18n.t("validations.household.reservist.injury_required") - end - if (record.armedforces == "No" || record.armedforces == "Prefer not to say") && record.reservist.present? record.errors.add :reservist, I18n.t("validations.household.reservist.injury_not_required") end end def validate_armed_forces_active_response(record) - if record.armedforces == "A current or former regular in the UK Armed Forces (excluding National Service)" && record.leftreg.blank? - record.errors.add :leftreg, I18n.t("validations.household.leftreg.question_required") - end - if record.armedforces != "A current or former regular in the UK Armed Forces (excluding National Service)" && record.leftreg.present? record.errors.add :leftreg, I18n.t("validations.household.leftreg.question_not_required") end diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index d524854eb..0925f6ee8 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -31,7 +31,7 @@ <%= form_with model: @case_log, url: form_case_log_path(@case_log), method: "post" do |f| %> <%= f.govuk_error_summary %> - <% @page.questions.map do |question| %> + <% @page.non_conditional_questions.map do |question| %>
> <% if question.read_only? %>
diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 58647959e..03a242e03 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -924,16 +924,15 @@ "2": "A spouse / civil partner of a UK Armed Forces member who has separated or been bereaved within the last 2 years", "3": "No", "4": "Tenant prefers not to say" - }, - "conditional_for": { - "leftreg": [ - "A current or former regular in the UK Armed Forces (excluding National Service)" - ], - "reservist": [ - "A current or former regular in the UK Armed Forces (excluding National Service)" - ] } - }, + } + } + }, + "armed_forces_member": { + "header": "Experience of the UK Armed Forces", + "description": "", + "depends_on": [{ "armedforces": "A current or former regular in the UK Armed Forces (excluding National Service)" }], + "questions": { "leftreg": { "header": "Are they still serving?", "hint_text": "", diff --git a/spec/features/form/conditional_questions_spec.rb b/spec/features/form/conditional_questions_spec.rb index 96ed1e3c3..5b16b1856 100644 --- a/spec/features/form/conditional_questions_spec.rb +++ b/spec/features/form/conditional_questions_spec.rb @@ -30,13 +30,11 @@ RSpec.describe "Form Conditional Questions" do visit("/logs/#{id}/armed-forces") # Something about our styling makes the selenium webdriver think the actual radio buttons are not visible so we allow label click here choose("case-log-armedforces-a-current-or-former-regular-in-the-uk-armed-forces-excluding-national-service-field", allow_label_click: true) - expect(page).to have_selector("#reservist_div") - choose("case-log-reservist-no-field", allow_label_click: true) - expect(page).to have_checked_field("case-log-reservist-no-field", visible: false) + fill_in("case-log-leftreg-field", with: "text") choose("case-log-armedforces-no-field", allow_label_click: true) - expect(page).not_to have_selector("#reservist_div") + expect(page).not_to have_field("case-log-leftreg-field") choose("case-log-armedforces-a-current-or-former-regular-in-the-uk-armed-forces-excluding-national-service-field", allow_label_click: true) - expect(page).to have_unchecked_field("case-log-reservist-no-field", visible: false) + expect(page).to have_css("#case-log-leftreg-field", text: "") end end end diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index fa3c24efd..73a988b13 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -124,32 +124,14 @@ "4": "Tenant prefers not to say" }, "conditional_for": { - "leftreg": ["A current or former regular in the UK Armed Forces (excluding National Service)"], - "reservist": ["A current or former regular in the UK Armed Forces (excluding National Service)"] + "leftreg": ["A current or former regular in the UK Armed Forces (excluding National Service)"] } }, "leftreg": { "header": "Are they still serving?", "hint_text": "", - "type": "radio", - "check_answer_label": "When did they leave the Armed Forces?", - "answer_options": { - "0": "Yes", - "1": "No - they left up to 5 years ago", - "2": "No - they left more than 5 years ago", - "3": "Prefer not to say" - } - }, - "reservist": { - "header": "Were they seriously injured or ill as a result of their service?", - "hint_text": "", - "type": "radio", - "check_answer_label": "Has anyone in the household been seriously injured or ill as a result of their service in the armed forces?", - "answer_options": { - "0": "Yes", - "1": "No", - "2": "Prefer not to say" - } + "type": "text", + "check_answer_label": "When did they leave the Armed Forces?" } } }, diff --git a/spec/helpers/conditional_questions_helper_spec.rb b/spec/helpers/conditional_questions_helper_spec.rb index 84f078411..5384ff536 100644 --- a/spec/helpers/conditional_questions_helper_spec.rb +++ b/spec/helpers/conditional_questions_helper_spec.rb @@ -5,7 +5,7 @@ RSpec.describe ConditionalQuestionsHelper do let(:page) { case_log.form.get_page("armed_forces") } describe "conditional questions for page" do - let(:conditional_pages) { %w[leftreg reservist] } + let(:conditional_pages) { %w[leftreg] } it "returns the question keys of all conditional questions on the given page" do expect(conditional_questions_for_page(page)).to eq(conditional_pages) @@ -13,7 +13,7 @@ RSpec.describe ConditionalQuestionsHelper do end describe "display question key div" do - let(:conditional_question) { page.questions.find { |q| q.id == "reservist" } } + let(:conditional_question) { page.questions.find { |q| q.id == "leftreg" } } it "returns a non visible div for conditional questions" do expect(display_question_key_div(page, conditional_question)).to match("style='display:none;'")