From 33aa4a226f7a2bfa6eb639ae19758d966ba862c6 Mon Sep 17 00:00:00 2001 From: Dushan <47317567+dushan-madetech@users.noreply.github.com> Date: Thu, 14 Apr 2022 15:07:14 +0100 Subject: [PATCH] Fix check answers bugs (#474) * fix issues with check answers * lint fixes --- app/models/form/question.rb | 10 ++++++++-- app/models/form/subsection.rb | 7 ++++--- config/forms/2021_2022.json | 7 ++++++- spec/fixtures/forms/2021_2022.json | 6 ++++++ spec/models/form/question_spec.rb | 16 ++++++++++++++++ spec/models/form/subsection_spec.rb | 15 +++++++++++++++ 6 files changed, 55 insertions(+), 6 deletions(-) diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 8d1d43a7c..5d3d50bb9 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -63,8 +63,14 @@ class Form::Question conditional_on.all? { |condition| evaluate_condition(condition, case_log) } end - def hidden_in_check_answers? - hidden_in_check_answers + def hidden_in_check_answers?(case_log) + if hidden_in_check_answers.is_a?(Hash) + hidden_in_check_answers["depends_on"].any? do |hsh| + hsh.all? { |key, value| case_log[key] == value } + end + else + hidden_in_check_answers + end end def has_inferred_check_answers_value?(case_log) diff --git a/app/models/form/subsection.rb b/app/models/form/subsection.rb index 245608409..95216c9d4 100644 --- a/app/models/form/subsection.rb +++ b/app/models/form/subsection.rb @@ -30,9 +30,10 @@ class Form::Subsection return :cannot_start_yet end - qs = applicable_questions(case_log).reject { |q| CaseLog::OPTIONAL_FIELDS.include?(q.id) } + qs = applicable_questions(case_log) + qs_optional_removed = qs.reject { |q| CaseLog::OPTIONAL_FIELDS.include?(q.id) } return :not_started if qs.all? { |question| case_log[question.id].blank? || question.read_only? } - return :completed if qs.all? { |question| question.completed?(case_log) } + return :completed if qs_optional_removed.all? { |question| question.completed?(case_log) } :in_progress end @@ -54,7 +55,7 @@ class Form::Subsection end def applicable_questions(case_log) - questions.select { |q| (displayed_to_user?(case_log, q) && !q.hidden_in_check_answers?) || q.has_inferred_check_answers_value?(case_log) } + questions.select { |q| (displayed_to_user?(case_log, q) && !q.hidden_in_check_answers?(case_log)) || q.has_inferred_check_answers_value?(case_log) } end def answered_questions(case_log) diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 0b92bf3b2..9695c29f4 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1147,7 +1147,12 @@ 0 ] }, - "hidden_in_check_answers": true + "hidden_in_check_answers": { + "depends_on": [ + { "age1_known": 0 }, + { "age1_known": 1 } + ] + } }, "age1": { "header": "Age", diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index e458d4215..77649d8c2 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -849,6 +849,12 @@ "7": { "value": "Don’t know" } + }, + "hidden_in_check_answers": { + "depends_on": [ + { "layear": 0 }, + { "layear": 1 } + ] } } }, diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index 94d65652a..227e99126 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -345,4 +345,20 @@ RSpec.describe Form::Question, type: :model do end end end + + context "when the question has a hidden in check answers attribute with dependencies" do + let(:section_id) { "local_authority" } + let(:subsection_id) { "local_authority" } + let(:page_id) { "time_lived_in_la" } + let(:question_id) { "layear" } + let(:case_log) do + FactoryBot.create(:case_log, :in_progress) + end + + it "can work out if the question will be shown in check answers" do + expect(question.hidden_in_check_answers?(case_log)).to be(false) + case_log.layear = 0 + expect(question.hidden_in_check_answers?(case_log)).to be(true) + end + end end diff --git a/spec/models/form/subsection_spec.rb b/spec/models/form/subsection_spec.rb index 1cd11eb7f..cb0bed8a2 100644 --- a/spec/models/form/subsection_spec.rb +++ b/spec/models/form/subsection_spec.rb @@ -57,6 +57,21 @@ RSpec.describe Form::Subsection, type: :model do expect(subsection.is_started?(case_log)).to be(true) end + context "with optional fields" do + subject(:subsection) { described_class.new(subsection_id, subsection_definition, section) } + + let(:section_id) { "tenancy_and_property" } + let(:section_definition) { form.form_definition["sections"][section_id] } + let(:section) { Form::Section.new(section_id, section_definition, form) } + let(:subsection_id) { "property_information" } + let(:subsection_definition) { section_definition["subsections"][subsection_id] } + + it "has a started status even if only an optional field has been answered" do + case_log.postcode_known = 0 + expect(subsection.is_started?(case_log)).to be(true) + end + end + it "has question helpers for the number of applicable questions" do expected_questions = %w[tenant_code age1 sex1 ecstat1 other_hhmemb ecstat2 propcode] expect(subsection.applicable_questions(case_log).map(&:id)).to eq(expected_questions)