From e850ff06fdfa364ffd21f6007dbd674a4b29a0c0 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 24 Mar 2022 11:23:32 +0000 Subject: [PATCH] Fix invalid page clearing (#416) * Need to check text/numeric questions separately * add a test for not clearing text fields Co-authored-by: baarkerlounger --- app/models/case_log.rb | 13 ++++++---- lib/tasks/rent_ranges.rake | 2 +- spec/fixtures/forms/2021_2022.json | 28 ++++++++++++++++++++++ spec/models/case_log_spec.rb | 10 ++++++++ spec/models/form_handler_spec.rb | 2 +- spec/models/form_spec.rb | 4 ++-- spec/requests/case_logs_controller_spec.rb | 2 ++ 7 files changed, 52 insertions(+), 9 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 7ca54cac2..559ac4e38 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -293,11 +293,14 @@ private def reset_not_routed_questions form.invalidated_page_questions(self).each do |question| - enabled = form.enabled_page_questions(self) - answer_options = enabled.map(&:id).include?(question.id) ? enabled.find { |q| q.id == question.id }.answer_options : [] - contains_selected_answer_option = answer_options.present? ? answer_options.key?(public_send(question.id).to_s) : false - if !contains_selected_answer_option && respond_to?(question.id.to_s) - public_send("#{question.id}=", nil) + enabled_questions = form.enabled_page_questions(self) + enabled_question_ids = enabled_questions.map(&:id) + 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) + else + public_send("#{question.id}=", nil) unless enabled_question_ids.include?(question.id) end end end diff --git a/lib/tasks/rent_ranges.rake b/lib/tasks/rent_ranges.rake index 85b7427a5..f4b3b24e7 100644 --- a/lib/tasks/rent_ranges.rake +++ b/lib/tasks/rent_ranges.rake @@ -24,6 +24,6 @@ namespace :data_import do ) count += 1 end - pp "Created/updated #{count} records" + pp "Created/updated #{count} records" unless Rails.env.test? end end diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index 73ff5e9ce..482ecc18f 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -762,6 +762,34 @@ "suffix": " every week" } } + }, + "care_home_charge": { + "questions": { + "offered": { + "check_answer_label": "Basic Rent", + "header": "What is the basic rent?", + "hint_text": "Eligible for housing benefit or Universal Credit", + "type": "numeric", + "min": 0, + "step": 1, + "width": 4 + } + }, + "depends_on": [{"period": 3}] + }, + "care_home_charge_bi_weekly": { + "questions": { + "offered": { + "check_answer_label": "Basic Rent", + "header": "What is the basic rent?", + "hint_text": "Eligible for housing benefit or Universal Credit", + "type": "numeric", + "min": 0, + "step": 1, + "width": 4 + } + }, + "depends_on": [{"period": 2}] } } } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 49f2ab188..32ffccf90 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1593,6 +1593,16 @@ RSpec.describe CaseLog do end end + context "when a non select question associated with several pages is routed to" do + let(:case_log) { FactoryBot.create(:case_log, :in_progress, period: 2) } + + it "does not clear the answer value" do + case_log.update!({ offered: 4 }) + case_log.reload + expect(case_log.offered).to eq(4) + end + end + context "when the case log does not have a valid form set yet" do let(:case_log) { FactoryBot.create(:case_log) } diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index 507d2416c..e9bdeb3fd 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -17,7 +17,7 @@ RSpec.describe FormHandler do form_handler = described_class.instance form = form_handler.get_form(test_form_name) expect(form).to be_a(Form) - expect(form.pages.count).to eq(31) + expect(form.pages.count).to eq(33) end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 5cb699061..fff2375aa 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -132,7 +132,7 @@ RSpec.describe Form, type: :model do describe "invalidated_page_questions" do context "when dependencies are not met" do - let(:expected_invalid) { %w[la_known cbl conditional_question_no_second_question net_income_value_check dependent_question layear declaration] } + let(:expected_invalid) { %w[la_known 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) @@ -140,7 +140,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[la_known cbl conditional_question_no_second_question net_income_value_check dependent_question layear declaration] } + let(:expected_invalid) { %w[la_known 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" diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb index ea58cbd28..4a9859cfc 100644 --- a/spec/requests/case_logs_controller_spec.rb +++ b/spec/requests/case_logs_controller_spec.rb @@ -28,6 +28,7 @@ RSpec.describe CaseLogsController, type: :request do let(:tenant_code) { "T365" } let(:age1) { 35 } let(:offered) { 12 } + let(:period) { 2 } let(:property_postcode) { "SE11 6TY" } let(:in_progress) { "in_progress" } let(:completed) { "completed" } @@ -41,6 +42,7 @@ RSpec.describe CaseLogsController, type: :request do "age1": age1, "property_postcode": property_postcode, "offered": offered, + "period": period, } end