From 1e17727687b3372078b361448c98df00501e47df Mon Sep 17 00:00:00 2001 From: MadeTech Dushan Date: Thu, 14 Oct 2021 16:54:40 +0100 Subject: [PATCH] Add next page path method with conditional routing --- app/controllers/case_logs_controller.rb | 14 ++++++ spec/controllers/case_logs_controller_spec.rb | 45 +++++++++++++++++ spec/features/case_log_spec.rb | 15 ++++++ spec/fixtures/forms/test_form.json | 48 +++++++++++++++++++ 4 files changed, 122 insertions(+) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index ef73bdd7c..68e2a34d2 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -51,6 +51,20 @@ class CaseLogsController < ApplicationController end end + def self.get_next_page_path(form, previous_page, responses_for_page={}) + questions_for_page = form.questions_for_page(previous_page) + questions_for_page.each do |question, content| + if(content.key?("conditional_route_to")) + content["conditional_route_to"].each do |route, answer| + if responses_for_page[question.to_sym] == answer + return "case_log_#{route.to_s}_path" + end + end + end + end + form.next_page_redirect_path(previous_page) + end + def check_answers form = @@form_handler.get_form("2021_2022") @case_log = CaseLog.find(params[:case_log_id]) diff --git a/spec/controllers/case_logs_controller_spec.rb b/spec/controllers/case_logs_controller_spec.rb index 845bd0d1e..dd8740a07 100644 --- a/spec/controllers/case_logs_controller_spec.rb +++ b/spec/controllers/case_logs_controller_spec.rb @@ -122,5 +122,50 @@ RSpec.describe CaseLogsController, type: :controller do expect(case_log.tenant_code).to eq(tenant_code) end end + + context "conditional routing" do + let(:case_log_form_conditional_question_yes_params) do + { + pregnancy: "Yes", + previous_page: "conditional_question", + } + end + + let(:case_log_form_conditional_question_no_params) do + { + pregnancy: "No", + previous_page: "conditional_question", + } + end + + it "routes to the appropriate conditional page based on the question answer of the current page" do + post :submit_form, params: { id: id, case_log: case_log_form_conditional_question_yes_params } + expect(response).to redirect_to("/case_logs/#{id}/conditional_question_yes_page") + + post :submit_form, params: { id: id, case_log: case_log_form_conditional_question_no_params } + expect(response).to redirect_to("/case_logs/#{id}/conditional_question_no_page") + end + end + end + + describe "get_next_page_path" do + let(:previous_page) { "net_income" } + let(:last_previous_page) { "housing_benefit" } + let(:previous_conditional_page) { "conditional_question" } + let(:form_handler) { FormHandler.instance } + let(:form) { form_handler.get_form("test_form") } + let(:responses_for_page) { { "pregnancy": "No" } } + + it "returns a correct page path if there is no conditional routing" do + expect(CaseLogsController.get_next_page_path(form, previous_page)).to eq("case_log_net_income_uc_proportion_path") + end + + it "returns a check answers page if previous page is the last page" do + expect(CaseLogsController.get_next_page_path(form, last_previous_page)).to eq("case_log_income_and_benefits_check_answers_path") + end + + it "returns a correct page path if there is conditional routing" do + expect(CaseLogsController.get_next_page_path(form, previous_conditional_page, responses_for_page)).to eq("case_log_conditional_question_no_page_path") + end end end diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index fed45d5d1..e48a8f79d 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -296,4 +296,19 @@ RSpec.describe "Test Features" do end end end + + describe "conditional page routing" do + it "can route the user to a different page based on their answer on the current page" do + visit("case_logs/#{id}/conditional_question") + # using a question name that is already in the db to avoid + # having to add a new column to the db for this test + choose("case-log-pregnancy-yes-field") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/conditional_question_yes_page") + click_link(text: "Back") + choose("case-log-pregnancy-no-field") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/conditional_question_no_page") + end + end end diff --git a/spec/fixtures/forms/test_form.json b/spec/fixtures/forms/test_form.json index 6da8edda0..dad82a79d 100644 --- a/spec/fixtures/forms/test_form.json +++ b/spec/fixtures/forms/test_form.json @@ -235,6 +235,54 @@ } } } + }, + "conditional_question": { + "label": "Conditional question", + "pages": { + "conditional_question": { + "questions": { + "pregnancy": { + "check_answer_label": "Has the condition been met?", + "header": "Has the condition been met?", + "type": "radio", + "answer_options": { + "0": "Yes", + "1": "No" + }, + "conditional_route_to": { + "conditional_question_yes_page": "Yes", + "conditional_question_no_page": "No" + } + } + } + }, + "conditional_question_yes_page": { + "questions": { + "conditional_question_yes_question": { + "check_answer_label": "Has the next condition been met?", + "header": "Has the next condition been met?", + "type": "radio", + "answer_options": { + "0": "Yes", + "1": "No" + } + } + } + }, + "conditional_question_no_page": { + "questions": { + "conditional_question_no_question": { + "check_answer_label": "Has the condition not been met?", + "header": "Has the next condition not been met?", + "type": "radio", + "answer_options": { + "0": "Yes", + "1": "No" + } + } + } + } + } } } },