From f1a2ddb5f8bd1b3929d26de699a8a5fef0ca2fa3 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 20 Oct 2021 16:47:18 +0100 Subject: [PATCH] make total_questions step over the form like a user would and gather relevand questions --- app/controllers/case_logs_controller.rb | 25 ++++++----- app/helpers/check_answers_helper.rb | 52 +++++++++++++++++------ spec/features/case_log_spec.rb | 1 - spec/fixtures/forms/test_form.json | 18 +++----- spec/helpers/check_answers_helper_spec.rb | 40 +++++++++++++++++ 5 files changed, 99 insertions(+), 37 deletions(-) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index d6ab7fd01..beea67d48 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -101,20 +101,23 @@ private # binding.pry questions_for_page = form.questions_for_page(previous_page) questions_for_page.each do |question, content| - - next unless content.key?("conditional_route_to") - - content["conditional_route_to"].each do |route, answer| - # binding.pry - if answer.include?(responses_for_page[question]) - - if route == "check_answers" - subsection = form.subsection_for_page(previous_page) - return "case_log_#{subsection}_check_answers_path" + if content.key?("conditional_route_to") + content["conditional_route_to"].each do |route, answer| + # binding.pry + if answer.include?(responses_for_page[question]) + return "case_log_#{route}_path" end - return "case_log_#{route}_path" end end + + next unless content.key?("next_page") + + next_page = content["next_page"] + if next_page == "check_answers" + subsection = form.subsection_for_page(previous_page) + return "case_log_#{subsection}_check_answers_path" + end + return "case_log_#{content[]}_path" end form.next_page_redirect_path(previous_page) diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index f7936eeeb..af6ac8d7e 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -10,24 +10,50 @@ module CheckAnswersHelper end def total_questions(subsection, case_log, form) - questions = form.questions_for_subsection(subsection) - questions_not_applicable = [] - questions.reject do |question_key, question| - question.fetch("conditional_for", []).map do |conditional_question_key, condition| - if condition_not_met(case_log, question_key, question, condition) - questions_not_applicable << conditional_question_key + total_questions = {} + page_name = form.pages_for_subsection(subsection).keys.first + + while page_name != "check_answers" && page_name != :check_answers + questions = form.questions_for_page(page_name) + question_key = questions.keys[0] + question_value = questions.values[0] + + appliccable_questions = filter_conditional_questions(questions, case_log) + total_questions = total_questions.merge(appliccable_questions) + + page_name = get_next_page_name(form, page_name, appliccable_questions, question_key, case_log, question_value) + end + + total_questions + end + + def filter_conditional_questions(questions, case_log) + appliccable_questions = questions + + questions.each do |k, question| + question.fetch("conditional_for", []).each do |conditional_question_key, condition| + if condition_not_met(case_log, k, question, condition) + appliccable_questions = appliccable_questions.reject { |z| z == conditional_question_key } end end - #check_answers is not a real page - question.fetch("conditional_route_to", []).reject { |q| q == "check_answers" }.map do |conditional_page_key, condition| - if condition_not_met(case_log, question_key, question, condition) - questions_not_applicable += form.questions_for_page(conditional_page_key).keys + end + appliccable_questions + end + + def get_next_page_name(form, page_name, appliccable_questions, question_key, case_log, question_value) + new_page_name = form.next_page(page_name) + if form.all_pages[page_name].key?("default_next_page") + new_page_name = form.all_pages[page_name]["default_next_page"] + end + + if appliccable_questions[question_key].key?("conditional_route_to") + appliccable_questions[question_key]["conditional_route_to"].each do |conditional_page_key, condition| + unless condition_not_met(case_log, question_key, question_value, condition) + new_page_name = conditional_page_key end end - # binding.pry - questions_not_applicable.include?(question_key) - end + new_page_name end def condition_not_met(case_log, question_key, question, condition) diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index ca303ce8d..42a4f4f3b 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -345,7 +345,6 @@ RSpec.describe "Test Features" do expect(page).to have_current_path("/case_logs/#{id}/conditional_question_no_page") end - it "can route based on page inclusion rules" do visit("/case_logs/#{id}/conditional_question_yes_page") choose("case-log-cbl-letting-yes-field", allow_label_click: true) diff --git a/spec/fixtures/forms/test_form.json b/spec/fixtures/forms/test_form.json index d4844e520..4d567f621 100644 --- a/spec/fixtures/forms/test_form.json +++ b/spec/fixtures/forms/test_form.json @@ -254,7 +254,8 @@ "conditional_question_no_page": "No" } } - } + }, + "default_next_page": "check_answers" }, "conditional_question_yes_page": { "questions": { @@ -265,12 +266,10 @@ "answer_options": { "0": "Yes", "1": "No" - }, - "conditional_route_to": { - "check_answers": ["Yes", "No"] } } - } + }, + "default_next_page": "check_answers" }, "conditional_question_no_page": { "questions": { @@ -281,12 +280,10 @@ "answer_options": { "0": "Yes", "1": "No" - }, - "conditional_route_to": { - "conditional_question_no_second_page": ["Yes", "No"] } } - } + }, + "default_next_page": "conditional_question_no_second_page" }, "conditional_question_no_second_page": { "questions": { @@ -297,9 +294,6 @@ "answer_options": { "0": "Yes", "1": "No" - }, - "include_page_condition": { - "pregnancy": "No" } } } diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index 87532a578..01457a1c5 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -17,6 +17,7 @@ RSpec.describe CheckAnswersHelper do let(:subsection_with_numeric_conditionals) { "household_characteristics" } let(:subsection_with_radio_conditionals) { "household_needs" } let(:conditional_routing_subsection) { "conditional_question" } + let(:conditional_page_subsection) { "household_needs" } form_handler = FormHandler.instance let(:form) { form_handler.get_form("test_form") } @@ -125,5 +126,44 @@ RSpec.describe CheckAnswersHelper do expect(total_number_of_questions(conditional_routing_subsection, case_log, form)).to eq(3) end end + + context "total questions" do + it "returns total questions" do + result = total_questions(subsection, case_log, form) + expect(result.class).to eq(Hash) + expected_keys = %w[net_income net_income_frequency net_income_uc_proportion housing_benefit] + expect(result.keys).to eq(expected_keys) + end + + context "conditional questions on the same page" do + it "it filters out conditional questions that were not displayed" do + result = total_questions(conditional_page_subsection, case_log, form) + expected_keys = %w[armed_forces medical_conditions accessibility_requirements condition_effects] + expect(result.keys).to eq(expected_keys) + end + + it "it includes conditional questions that were displayed" do + case_log["armed_forces"] = "Yes - a regular" + result = total_questions(conditional_page_subsection, case_log, form) + expected_keys = %w[armed_forces armed_forces_active armed_forces_injured medical_conditions accessibility_requirements condition_effects] + expect(result.keys).to eq(expected_keys) + end + end + + context "conditional routing" do + it "it ignores skipped pages and the questions therein when conditional routing" do + result = total_questions(conditional_routing_subsection, case_log, form) + expected_keys = %w[pregnancy] + expect(result.keys).to match_array(expected_keys) + end + + it "it includes conditional pages and questions that were displayed" do + case_log["pregnancy"] = "Yes" + result = total_questions(conditional_routing_subsection, case_log, form) + expected_keys = %w[pregnancy cbl_letting] + expect(result.keys).to match_array(expected_keys) + end + end + end end end