Browse Source

Fix check answers bugs (#474)

* fix issues with check answers

* lint fixes
pull/475/head
Dushan 3 years ago committed by GitHub
parent
commit
33aa4a226f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      app/models/form/question.rb
  2. 7
      app/models/form/subsection.rb
  3. 7
      config/forms/2021_2022.json
  4. 6
      spec/fixtures/forms/2021_2022.json
  5. 16
      spec/models/form/question_spec.rb
  6. 15
      spec/models/form/subsection_spec.rb

10
app/models/form/question.rb

@ -63,8 +63,14 @@ class Form::Question
conditional_on.all? { |condition| evaluate_condition(condition, case_log) } conditional_on.all? { |condition| evaluate_condition(condition, case_log) }
end end
def hidden_in_check_answers? def hidden_in_check_answers?(case_log)
hidden_in_check_answers 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 end
def has_inferred_check_answers_value?(case_log) def has_inferred_check_answers_value?(case_log)

7
app/models/form/subsection.rb

@ -30,9 +30,10 @@ class Form::Subsection
return :cannot_start_yet return :cannot_start_yet
end 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 :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 :in_progress
end end
@ -54,7 +55,7 @@ class Form::Subsection
end end
def applicable_questions(case_log) 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 end
def answered_questions(case_log) def answered_questions(case_log)

7
config/forms/2021_2022.json

@ -1147,7 +1147,12 @@
0 0
] ]
}, },
"hidden_in_check_answers": true "hidden_in_check_answers": {
"depends_on": [
{ "age1_known": 0 },
{ "age1_known": 1 }
]
}
}, },
"age1": { "age1": {
"header": "Age", "header": "Age",

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

@ -849,6 +849,12 @@
"7": { "7": {
"value": "Don’t know" "value": "Don’t know"
} }
},
"hidden_in_check_answers": {
"depends_on": [
{ "layear": 0 },
{ "layear": 1 }
]
} }
} }
}, },

16
spec/models/form/question_spec.rb

@ -345,4 +345,20 @@ RSpec.describe Form::Question, type: :model do
end end
end 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 end

15
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) expect(subsection.is_started?(case_log)).to be(true)
end 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 it "has question helpers for the number of applicable questions" do
expected_questions = %w[tenant_code age1 sex1 ecstat1 other_hhmemb ecstat2 propcode] expected_questions = %w[tenant_code age1 sex1 ecstat1 other_hhmemb ecstat2 propcode]
expect(subsection.applicable_questions(case_log).map(&:id)).to eq(expected_questions) expect(subsection.applicable_questions(case_log).map(&:id)).to eq(expected_questions)

Loading…
Cancel
Save