Browse Source

only reset fields that have not been routed to at all (#217)

pull/218/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
71e7002e57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/models/form.rb
  2. 18
      spec/features/form/validations_spec.rb
  3. 2
      spec/fixtures/forms/2021_2022.json
  4. 17
      spec/models/case_log_spec.rb
  5. 18
      spec/models/form_spec.rb

6
app/models/form.rb

@ -59,11 +59,13 @@ class Form
end end
def invalidated_questions(case_log) 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 end
def invalidated_page_questions(case_log) 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 end
def invalidated_conditional_questions(case_log) def invalidated_conditional_questions(case_log)

18
spec/features/form/validations_spec.rb

@ -102,24 +102,6 @@ RSpec.describe "validations" do
end end
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 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 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 let(:case_log) do

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

@ -325,7 +325,7 @@
}, },
"conditional_question_no_page": { "conditional_question_no_page": {
"questions": { "questions": {
"conditional_question_no_question": { "cbl": {
"check_answer_label": "Has the condition not been met?", "check_answer_label": "Has the condition not been met?",
"header": "Has the next condition not been met?", "header": "Has the next condition not been met?",
"type": "radio", "type": "radio",

17
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") expect(record_from_db["has_benefits"]).to eq("Yes")
end end
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 end

18
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") expect(form.next_page_redirect_path(previous_conditional_page, case_log)).to eq("case_log_conditional_question_no_page_path")
end end
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 end

Loading…
Cancel
Save