From 71e7002e57493cacbc1e36cbbe32c9ef78883667 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 13 Jan 2022 13:13:28 +0000 Subject: [PATCH] only reset fields that have not been routed to at all (#217) --- app/models/form.rb | 6 ++++-- spec/features/form/validations_spec.rb | 18 ------------------ spec/fixtures/forms/2021_2022.json | 2 +- spec/models/case_log_spec.rb | 17 +++++++++++++++++ spec/models/form_spec.rb | 18 ++++++++++++++++++ 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/app/models/form.rb b/app/models/form.rb index 4474eae8e..647ddf59e 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -59,11 +59,13 @@ class Form end def invalidated_questions(case_log) - (invalidated_page_questions(case_log) + invalidated_conditional_questions(case_log)).uniq + invalidated_page_questions(case_log) + invalidated_conditional_questions(case_log) end def invalidated_page_questions(case_log) - invalidated_pages(case_log).flat_map(&:questions) || [] + pages_that_are_routed_to = pages.select { |p| p.routed_to?(case_log) } + enabled_question_ids = pages_that_are_routed_to.flat_map(&:questions).map(&:id) || [] + invalidated_pages(case_log).flat_map(&:questions).reject { |q| enabled_question_ids.include?(q.id) } || [] end def invalidated_conditional_questions(case_log) diff --git a/spec/features/form/validations_spec.rb b/spec/features/form/validations_spec.rb index 0ba1eff37..4aa72e261 100644 --- a/spec/features/form/validations_spec.rb +++ b/spec/features/form/validations_spec.rb @@ -102,24 +102,6 @@ RSpec.describe "validations" do end end - describe "Compound validations" do - context "when you select two compatible answers, that become incompatible if the first answer changes", js: true do - it "clears the second answer on change of the first" do - case_log.update!(other_hhmemb: 1, relat2: "Partner", age2: 32, sex2: "Female", ecstat2: "Not seeking work") - visit("/logs/#{id}/conditional-question") - choose("case-log-preg-occ-yes-field", allow_label_click: true) - click_button("Save and continue") - choose("case-log-cbl-yes-field", allow_label_click: true) - click_button("Save and continue") - page.go_back - click_link("Back") - choose("case-log-preg-occ-no-field", allow_label_click: true) - click_button("Save and continue") - expect(case_log.reload.cbl).to be_nil - end - end - end - describe "Soft Validation" do context "given a weekly net income that is above the expected amount for the given economic status but below the hard max" do let(:case_log) do diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index e6763ba96..aa4e83e0a 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -325,7 +325,7 @@ }, "conditional_question_no_page": { "questions": { - "conditional_question_no_question": { + "cbl": { "check_answer_label": "Has the condition not been met?", "header": "Has the next condition not been met?", "type": "radio", diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index c040ade7c..0afdd3a6e 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1228,4 +1228,21 @@ RSpec.describe Form, type: :model do expect(record_from_db["has_benefits"]).to eq("Yes") end end + + describe "resetting invalidated fields" do + context "when a question that has already been answered, no longer has met dependencies" do + let(:case_log) { FactoryBot.create(:case_log, :in_progress, cbl: "Yes", preg_occ: "No") } + + it "clears the answer" do + expect { case_log.update!(preg_occ: nil) }.to change { case_log.cbl }.from("Yes").to(nil) + end + end + + context "two pages with the same question key, only one's dependency is met" do + let(:case_log) { FactoryBot.create(:case_log, :in_progress, cbl: "Yes", preg_occ: "No") } + it "does not clear the answer" do + expect(case_log.cbl).to eq("Yes") + end + end + end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 1816946d4..3c28daf89 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -31,4 +31,22 @@ RSpec.describe Form, type: :model do expect(form.next_page_redirect_path(previous_conditional_page, case_log)).to eq("case_log_conditional_question_no_page_path") end end + + describe "invalidated_page_questions" do + context "dependencies not met" do + let(:expected_invalid) { %w[la_known cbl conditional_question_no_second_question dependent_question 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) + end + end + + context "two pages with the same question, only one has dependencies met" do + let(:expected_invalid) { %w[la_known conditional_question_no_second_question dependent_question declaration] } + + it "returns an array of question keys whose pages conditions are not met" do + case_log["preg_occ"] = "No" + expect(form.invalidated_page_questions(case_log).map(&:id).uniq).to eq(expected_invalid) + end + end + end end