Browse Source

Reset checkbox answer option answers when the question has been invalidated by other answers

pull/515/head
baarkerlounger 3 years ago
parent
commit
bf4733679a
  1. 8
      app/models/case_log.rb
  2. 8
      app/services/imports/case_logs_import_service.rb
  3. 1
      spec/features/form/accessible_autocomplete_spec.rb
  4. 4
      spec/features/form/check_answers_page_spec.rb
  5. 5
      spec/fixtures/forms/2021_2022.json
  6. 8
      spec/models/case_log_spec.rb
  7. 4
      spec/models/form_spec.rb

8
app/models/case_log.rb

@ -383,7 +383,13 @@ private
if %w[radio checkbox].include?(question.type) 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 : {} 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 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 else
public_send("#{question.id}=", nil) unless enabled_question_ids.include?(question.id) public_send("#{question.id}=", nil) unless enabled_question_ids.include?(question.id)
end end

8
app/services/imports/case_logs_import_service.rb

@ -91,7 +91,7 @@ module Imports
attributes["illness"] = unsafe_string_as_integer(xml_doc, "Q10ia") attributes["illness"] = unsafe_string_as_integer(xml_doc, "Q10ia")
(1..10).each do |index| (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 end
attributes["prevten"] = unsafe_string_as_integer(xml_doc, "Q11") attributes["prevten"] = unsafe_string_as_integer(xml_doc, "Q11")
@ -442,11 +442,11 @@ module Imports
end end
end end
def illness_type(xml_doc, index) def illness_type(xml_doc, index, illness)
illness_type = string_or_nil(xml_doc, "Q10ib-#{index}") illness_type = string_or_nil(xml_doc, "Q10ib-#{index}")
if illness_type == "Yes" if illness_type == "Yes" && illness == "Yes"
1 1
else elsif illness == "Yes"
0 0
end end
end end

1
spec/features/form/accessible_autocomplete_spec.rb

@ -10,6 +10,7 @@ RSpec.describe "Accessible Automcomplete" do
:in_progress, :in_progress,
previous_la_known: 1, previous_la_known: 1,
prevloc: "E09000033", prevloc: "E09000033",
illness: 1,
is_la_inferred: false, is_la_inferred: false,
owning_organisation: user.organisation, owning_organisation: user.organisation,
managing_organisation: user.organisation, managing_organisation: user.organisation,

4
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 it "updates the change/answer link when answers get updated" do
visit("/logs/#{empty_case_log.id}/household-needs/check-answers") 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 assert_selector "a", text: "Change", count: 1
visit("/logs/#{empty_case_log.id}/accessibility-requirements") visit("/logs/#{empty_case_log.id}/accessibility-requirements")
check("case-log-accessibility-requirements-housingneeds-c-field") check("case-log-accessibility-requirements-housingneeds-c-field")
click_button("Save and continue") click_button("Save and continue")
visit("/logs/#{empty_case_log.id}/household-needs/check-answers") 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 assert_selector "a", text: "Change", count: 2
expect(page).to have_link("Change", href: "/logs/#{empty_case_log.id}/accessibility-requirements?referrer=check_answers") expect(page).to have_link("Change", href: "/logs/#{empty_case_log.id}/accessibility-requirements?referrer=check_answers")
end end

5
spec/fixtures/forms/2021_2022.json vendored

@ -342,7 +342,12 @@
} }
} }
} }
},
"depends_on": [
{
"illness": 1
} }
]
} }
} }
} }

8
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) expect { case_log.update!(housingneeds_a: 0) }.to change(case_log, :tenant_code).from("test").to(nil)
end end
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 end
context "with two pages having the same question key, only one's dependency is met" do context "with two pages having the same question key, only one's dependency is met" do

4
spec/models/form_spec.rb

@ -175,7 +175,7 @@ RSpec.describe Form, type: :model do
describe "invalidated_page_questions" do describe "invalidated_page_questions" do
context "when dependencies are not met" 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 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) 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 end
context "with two pages having the same question and only one has dependencies met" do 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 it "returns an array of question keys whose pages conditions are not met" do
case_log["preg_occ"] = "No" case_log["preg_occ"] = "No"

Loading…
Cancel
Save