From bf4733679a51953621b72712c368ead97fdc9dfe Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Tue, 26 Apr 2022 16:52:22 +0100 Subject: [PATCH] Reset checkbox answer option answers when the question has been invalidated by other answers --- app/models/case_log.rb | 8 +++++++- app/services/imports/case_logs_import_service.rb | 8 ++++---- spec/features/form/accessible_autocomplete_spec.rb | 1 + spec/features/form/check_answers_page_spec.rb | 4 ++-- spec/fixtures/forms/2021_2022.json | 7 ++++++- spec/models/case_log_spec.rb | 8 ++++++++ spec/models/form_spec.rb | 4 ++-- 7 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index db0e25069..f61ea4f56 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -383,7 +383,13 @@ private if %w[radio checkbox].include?(question.type) enabled_answer_options = enabled_question_ids.include?(question.id) ? enabled_questions.find { |q| q.id == question.id }.answer_options : {} current_answer_option_valid = enabled_answer_options.present? ? enabled_answer_options.key?(public_send(question.id).to_s) : false - public_send("#{question.id}=", nil) if !current_answer_option_valid && respond_to?(question.id.to_s) + if !current_answer_option_valid && respond_to?(question.id.to_s) + public_send("#{question.id}=", nil) + else + (question.answer_options.keys - enabled_answer_options.keys).map do |invalid_answer_option| + public_send("#{invalid_answer_option}=", nil) if respond_to?(invalid_answer_option) + end + end else public_send("#{question.id}=", nil) unless enabled_question_ids.include?(question.id) end diff --git a/app/services/imports/case_logs_import_service.rb b/app/services/imports/case_logs_import_service.rb index c42bc476e..bc7e1e0d9 100644 --- a/app/services/imports/case_logs_import_service.rb +++ b/app/services/imports/case_logs_import_service.rb @@ -91,7 +91,7 @@ module Imports attributes["illness"] = unsafe_string_as_integer(xml_doc, "Q10ia") (1..10).each do |index| - attributes["illness_type_#{index}"] = illness_type(xml_doc, index) + attributes["illness_type_#{index}"] = illness_type(xml_doc, index, attributes["illness"]) end attributes["prevten"] = unsafe_string_as_integer(xml_doc, "Q11") @@ -442,11 +442,11 @@ module Imports end end - def illness_type(xml_doc, index) + def illness_type(xml_doc, index, illness) illness_type = string_or_nil(xml_doc, "Q10ib-#{index}") - if illness_type == "Yes" + if illness_type == "Yes" && illness == "Yes" 1 - else + elsif illness == "Yes" 0 end end diff --git a/spec/features/form/accessible_autocomplete_spec.rb b/spec/features/form/accessible_autocomplete_spec.rb index ea557b017..e196954da 100644 --- a/spec/features/form/accessible_autocomplete_spec.rb +++ b/spec/features/form/accessible_autocomplete_spec.rb @@ -10,6 +10,7 @@ RSpec.describe "Accessible Automcomplete" do :in_progress, previous_la_known: 1, prevloc: "E09000033", + illness: 1, is_la_inferred: false, owning_organisation: user.organisation, managing_organisation: user.organisation, diff --git a/spec/features/form/check_answers_page_spec.rb b/spec/features/form/check_answers_page_spec.rb index 8bce7412e..77e5a8445 100644 --- a/spec/features/form/check_answers_page_spec.rb +++ b/spec/features/form/check_answers_page_spec.rb @@ -87,13 +87,13 @@ RSpec.describe "Form Check Answers Page" do it "updates the change/answer link when answers get updated" do visit("/logs/#{empty_case_log.id}/household-needs/check-answers") - assert_selector "a", text: /Answer (?!the missing questions)/, count: 4 + assert_selector "a", text: /Answer (?!the missing questions)/, count: 3 assert_selector "a", text: "Change", count: 1 visit("/logs/#{empty_case_log.id}/accessibility-requirements") check("case-log-accessibility-requirements-housingneeds-c-field") click_button("Save and continue") visit("/logs/#{empty_case_log.id}/household-needs/check-answers") - assert_selector "a", text: /Answer (?!the missing questions)/, count: 3 + assert_selector "a", text: /Answer (?!the missing questions)/, count: 2 assert_selector "a", text: "Change", count: 2 expect(page).to have_link("Change", href: "/logs/#{empty_case_log.id}/accessibility-requirements?referrer=check_answers") end diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index c0ab4ca1f..eb41a0819 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -342,7 +342,12 @@ } } } - } + }, + "depends_on": [ + { + "illness": 1 + } + ] } } } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 0ce9ac85f..3f49ea2d4 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1664,6 +1664,14 @@ RSpec.describe CaseLog do expect { case_log.update!(housingneeds_a: 0) }.to change(case_log, :tenant_code).from("test").to(nil) end end + + context "when the question type has answer options" do + let(:case_log) { FactoryBot.create(:case_log, :in_progress, illness: 1, illness_type_1: 1) } + + it "clears the answer" do + expect { case_log.update!(illness: 0) }.to change(case_log, :illness_type_1).from(1).to(nil) + end + end end context "with two pages having the same question key, only one's dependency is met" do diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 6f906e439..43628ab78 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -175,7 +175,7 @@ RSpec.describe Form, type: :model do describe "invalidated_page_questions" do context "when dependencies are not met" do - let(:expected_invalid) { %w[cbl conditional_question_no_second_question net_income_value_check dependent_question offered layear declaration] } + let(:expected_invalid) { %w[condition_effects cbl conditional_question_no_second_question net_income_value_check dependent_question offered layear declaration] } it "returns an array of question keys whose pages conditions are not met" do expect(form.invalidated_page_questions(case_log).map(&:id).uniq).to eq(expected_invalid) @@ -183,7 +183,7 @@ RSpec.describe Form, type: :model do end context "with two pages having the same question and only one has dependencies met" do - let(:expected_invalid) { %w[cbl conditional_question_no_second_question net_income_value_check dependent_question offered layear declaration] } + let(:expected_invalid) { %w[condition_effects cbl conditional_question_no_second_question net_income_value_check dependent_question offered layear declaration] } it "returns an array of question keys whose pages conditions are not met" do case_log["preg_occ"] = "No"