Browse Source

Fix invalid page clearing (#416)

* Need to check text/numeric questions separately

* add a test for not clearing text fields

Co-authored-by: baarkerlounger <db@slothlife.xyz>
pull/417/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
e850ff06fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      app/models/case_log.rb
  2. 2
      lib/tasks/rent_ranges.rake
  3. 28
      spec/fixtures/forms/2021_2022.json
  4. 10
      spec/models/case_log_spec.rb
  5. 2
      spec/models/form_handler_spec.rb
  6. 4
      spec/models/form_spec.rb
  7. 2
      spec/requests/case_logs_controller_spec.rb

13
app/models/case_log.rb

@ -293,11 +293,14 @@ private
def reset_not_routed_questions def reset_not_routed_questions
form.invalidated_page_questions(self).each do |question| form.invalidated_page_questions(self).each do |question|
enabled = form.enabled_page_questions(self) enabled_questions = form.enabled_page_questions(self)
answer_options = enabled.map(&:id).include?(question.id) ? enabled.find { |q| q.id == question.id }.answer_options : [] enabled_question_ids = enabled_questions.map(&:id)
contains_selected_answer_option = answer_options.present? ? answer_options.key?(public_send(question.id).to_s) : false if %w[radio checkbox].include?(question.type)
if !contains_selected_answer_option && respond_to?(question.id.to_s) enabled_answer_options = enabled_question_ids.include?(question.id) ? enabled_questions.find { |q| q.id == question.id }.answer_options : {}
public_send("#{question.id}=", nil) 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 end
end end

2
lib/tasks/rent_ranges.rake

@ -24,6 +24,6 @@ namespace :data_import do
) )
count += 1 count += 1
end end
pp "Created/updated #{count} records" pp "Created/updated #{count} records" unless Rails.env.test?
end end
end end

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

@ -762,6 +762,34 @@
"suffix": " every week" "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}]
} }
} }
} }

10
spec/models/case_log_spec.rb

@ -1593,6 +1593,16 @@ RSpec.describe CaseLog do
end end
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 context "when the case log does not have a valid form set yet" do
let(:case_log) { FactoryBot.create(:case_log) } let(:case_log) { FactoryBot.create(:case_log) }

2
spec/models/form_handler_spec.rb

@ -17,7 +17,7 @@ RSpec.describe FormHandler do
form_handler = described_class.instance form_handler = described_class.instance
form = form_handler.get_form(test_form_name) form = form_handler.get_form(test_form_name)
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to eq(31) expect(form.pages.count).to eq(33)
end end
end end

4
spec/models/form_spec.rb

@ -132,7 +132,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[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 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)
@ -140,7 +140,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[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 it "returns an array of question keys whose pages conditions are not met" do
case_log["preg_occ"] = "No" case_log["preg_occ"] = "No"

2
spec/requests/case_logs_controller_spec.rb

@ -28,6 +28,7 @@ RSpec.describe CaseLogsController, type: :request do
let(:tenant_code) { "T365" } let(:tenant_code) { "T365" }
let(:age1) { 35 } let(:age1) { 35 }
let(:offered) { 12 } let(:offered) { 12 }
let(:period) { 2 }
let(:property_postcode) { "SE11 6TY" } let(:property_postcode) { "SE11 6TY" }
let(:in_progress) { "in_progress" } let(:in_progress) { "in_progress" }
let(:completed) { "completed" } let(:completed) { "completed" }
@ -41,6 +42,7 @@ RSpec.describe CaseLogsController, type: :request do
"age1": age1, "age1": age1,
"property_postcode": property_postcode, "property_postcode": property_postcode,
"offered": offered, "offered": offered,
"period": period,
} }
end end

Loading…
Cancel
Save