Browse Source

Ensure derived option is still shown in check answers

pull/446/head
baarkerlounger 3 years ago
parent
commit
517de548b1
  1. 6
      app/models/form/question.rb
  2. 4
      spec/features/form/check_answers_page_spec.rb
  3. 11
      spec/fixtures/forms/2021_2022.json
  4. 3
      spec/helpers/check_answers_helper_spec.rb
  5. 2
      spec/models/form/page_spec.rb
  6. 16
      spec/models/form/question_spec.rb
  7. 8
      spec/models/form/subsection_spec.rb
  8. 2
      spec/models/form_handler_spec.rb
  9. 7
      spec/models/form_spec.rb

6
app/models/form/question.rb

@ -68,6 +68,7 @@ class Form::Question
end
def has_inferred_check_answers_value?(case_log)
return true if selected_answer_option_is_derived?(case_log)
return inferred_check_answers_value["condition"].values[0] == case_log[inferred_check_answers_value["condition"].keys[0]] if inferred_check_answers_value.present?
false
@ -156,6 +157,11 @@ class Form::Question
private
def selected_answer_option_is_derived?(case_log)
selected_option = answer_options&.dig(case_log[id].to_s.presence)
selected_option.is_a?(Hash) && selected_option["derived"]
end
def has_inferred_display_value?(case_log)
inferred_check_answers_value.present? && case_log[inferred_check_answers_value["condition"].keys.first] == inferred_check_answers_value["condition"].values.first
end

4
spec/features/form/check_answers_page_spec.rb

@ -72,7 +72,7 @@ RSpec.describe "Form Check Answers Page" do
# This way only the links in the table will get picked up
it "has an answer link for questions missing an answer" do
visit("/logs/#{empty_case_log.id}/#{subsection}/check-answers?referrer=check_answers")
assert_selector "a", text: /Answer (?!the missing questions)/, count: 4
assert_selector "a", text: /Answer (?!the missing questions)/, count: 5
assert_selector "a", text: "Change", count: 0
expect(page).to have_link("Answer", href: "/logs/#{empty_case_log.id}/person-1-age?referrer=check_answers")
end
@ -80,7 +80,7 @@ RSpec.describe "Form Check Answers Page" do
it "has a change link for answered questions" do
fill_in_number_question(empty_case_log.id, "age1", 28, "person-1-age")
visit("/logs/#{empty_case_log.id}/#{subsection}/check-answers")
assert_selector "a", text: /Answer (?!the missing questions)/, count: 3
assert_selector "a", text: /Answer (?!the missing questions)/, count: 4
assert_selector "a", text: "Change", count: 1
expect(page).to have_link("Change", href: "/logs/#{empty_case_log.id}/person-1-age?referrer=check_answers")
end

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

@ -128,8 +128,7 @@
"conditional_for": {
"relat2": ">0",
"age2": ">0",
"sex2": ">0",
"ecstat2": ">0"
"sex2": ">0"
}
},
"relat2": {
@ -172,7 +171,13 @@
"value": "Prefer not to say"
}
}
},
}
}
},
"person_2_working_situation": {
"header": "",
"description": "",
"questions": {
"ecstat2": {
"check_answer_label": "Person 2’s Work",
"header": "Which of these best describes person 2’s working situation?",

3
spec/helpers/check_answers_helper_spec.rb

@ -9,7 +9,7 @@ RSpec.describe CheckAnswersHelper do
context "when a section hasn't been completed yet" do
it "returns that you have unanswered questions" do
expect(display_answered_questions_summary(subsection, case_log))
.to match(/You have answered 2 of 6 questions./)
.to match(/You have answered 2 of 7 questions./)
end
end
@ -19,6 +19,7 @@ RSpec.describe CheckAnswersHelper do
case_log.other_hhmemb = 0
case_log.propcode = "123"
case_log.ecstat1 = 200
case_log.ecstat2 = 9
expect(display_answered_questions_summary(subsection, case_log))
.to match(/You answered all the questions./)
expect(display_answered_questions_summary(subsection, case_log))

2
spec/models/form/page_spec.rb

@ -64,7 +64,7 @@ RSpec.describe Form::Page, type: :model do
context "with expression routing conditions" do
let(:section_id) { "household" }
let(:subsection_id) { "household_characteristics" }
let(:page_id) { "household_number_of_other_members" }
let(:page_id) { "person_2_working_situation" }
it "evaluates not met conditions correctly" do
case_log.age2 = 12

16
spec/models/form/question_spec.rb

@ -125,7 +125,7 @@ RSpec.describe Form::Question, type: :model do
context "when answer options include derived options" do
let(:section_id) { "household" }
let(:subsection_id) { "household_characteristics" }
let(:page_id) { "household_number_of_other_members" }
let(:page_id) { "person_2_working_situation" }
let(:question_id) { "ecstat2" }
let(:expected_answer_options) do
{ "0" => { "value" => "Other" }, "1" => { "value" => "Prefer not to say" } }
@ -243,6 +243,20 @@ RSpec.describe Form::Question, type: :model do
end
end
context "when the answer option is a derived answer option" do
let(:section_id) { "household" }
let(:subsection_id) { "household_characteristics" }
let(:page_id) { "person_2_working_situation" }
let(:question_id) { "ecstat2" }
let(:case_log) do
FactoryBot.create(:case_log, :in_progress, hhmemb: 2, details_known_2: 0, age2_known: 0, age2: 12)
end
it "knows it has an inferred value for check answers" do
expect(question.has_inferred_check_answers_value?(case_log)).to be true
end
end
context "when type is date" do
let(:section_id) { "local_authority" }
let(:subsection_id) { "local_authority" }

8
spec/models/form/subsection_spec.rb

@ -25,7 +25,7 @@ RSpec.describe Form::Subsection, type: :model do
end
it "has pages" do
expected_pages = %w[tenant_code person_1_age person_1_gender person_1_working_situation household_number_of_other_members propcode]
expected_pages = %w[tenant_code person_1_age person_1_gender person_1_working_situation household_number_of_other_members person_2_working_situation propcode]
expect(subsection.pages.map(&:id)).to eq(expected_pages)
end
@ -58,9 +58,9 @@ RSpec.describe Form::Subsection, type: :model do
end
it "has question helpers for the number of applicable questions" do
expected_questions = %w[tenant_code age1 sex1 ecstat1 other_hhmemb 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_count(case_log)).to eq(6)
expect(subsection.applicable_questions_count(case_log)).to eq(7)
end
it "has question helpers for the number of answered questions" do
@ -79,7 +79,7 @@ RSpec.describe Form::Subsection, type: :model do
end
it "has a question helpers for the unanswered questions" do
expected_questions = %w[sex1 ecstat1 other_hhmemb propcode]
expected_questions = %w[sex1 ecstat1 other_hhmemb ecstat2 propcode]
expect(subsection.unanswered_questions(case_log).map(&:id)).to eq(expected_questions)
end
end

2
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(33)
expect(form.pages.count).to eq(34)
end
end

7
spec/models/form_spec.rb

@ -128,9 +128,12 @@ RSpec.describe Form, type: :model do
before do
case_log.tenant_code = "123"
case_log.age1 = 35
case_log.sex1 = "Male"
case_log.sex1 = "M"
case_log.ecstat1 = 0
case_log.other_hhmemb = 0
case_log.other_hhmemb = 1
case_log.relat2 = "P"
case_log.sex2 = "F"
case_log.ecstat2 = 1
end
it "returns the first page of the next incomplete subsection if the subsection is not in progress" do

Loading…
Cancel
Save