From 8cda8cbc84c9ac17db624f95c6c6242270b32a4e Mon Sep 17 00:00:00 2001 From: MadeTech Dushan Date: Tue, 25 Jan 2022 11:22:55 +0000 Subject: [PATCH] add button for moving to next incomplete section --- app/models/form.rb | 20 +++++++ app/views/form/check_answers.html.erb | 8 ++- spec/features/form/check_answers_page_spec.rb | 59 +++++++++++++++++++ spec/models/form_spec.rb | 32 ++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) diff --git a/app/models/form.rb b/app/models/form.rb index 647ddf59e..0617bfad6 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -46,6 +46,26 @@ class Form "case_log_#{nxt_page}_path" end end + + def next_incomplete_section_redirect_path(subsection, case_log) + subsection_ids = subsections.map(&:id) + + if(case_log.status == "completed") + next_subsection = get_subsection(subsection_ids[subsection_ids.length - 1]) + first_question_in_subsection = next_subsection.pages.first.id + return "#{first_question_in_subsection}".dasherize + end + + next_subsection_id_index = subsection_ids.index(subsection.id) + 1 + next_subsection = get_subsection(subsection_ids[next_subsection_id_index]) + + if(next_subsection.status(case_log) == :completed) + next_incomplete_section_redirect_path(next_subsection, case_log) + else + first_question_in_subsection = next_subsection.pages.first.id + return "#{first_question_in_subsection}".dasherize + end + end def conditional_question_conditions conditions = questions.map { |q| Hash(q.id => q.conditional_for) if q.conditional_for.present? }.compact diff --git a/app/views/form/check_answers.html.erb b/app/views/form/check_answers.html.erb index 42dd44c1b..c9bf0b713 100644 --- a/app/views/form/check_answers.html.erb +++ b/app/views/form/check_answers.html.erb @@ -22,7 +22,13 @@ <%= form_with model: @case_log, method: "get" do |f| %> - <%= f.govuk_submit "Save and continue" %> + <%= f.govuk_submit 'Save and continue' do %> + <% if @case_log.status == "in_progress" %> + >Save and go to next incomplete section + <% elsif @case_log.status == "completed" %> + >Save and go to submit + <% end%> + <% end %> <% end %> diff --git a/spec/features/form/check_answers_page_spec.rb b/spec/features/form/check_answers_page_spec.rb index a0f6d8e74..f4f889723 100644 --- a/spec/features/form/check_answers_page_spec.rb +++ b/spec/features/form/check_answers_page_spec.rb @@ -22,6 +22,14 @@ RSpec.describe "Form Check Answers Page" do managing_organisation: user.organisation, ) end + let(:completed_case_log) do + FactoryBot.create( + :case_log, + :completed, + owning_organisation: user.organisation, + managing_organisation: user.organisation, + ) + end let(:id) { case_log.id } before do @@ -117,5 +125,56 @@ RSpec.describe "Form Check Answers Page" do expect(page).not_to have_content(label) end end + + context "when the user wants to bypass the tasklist page from check answers" do + let(:section_completed_case_log) do + FactoryBot.create( + :case_log, + :in_progress, + owning_organisation: user.organisation, + managing_organisation: user.organisation, + tenant_code: "123", + age1: 35, + sex1: "Male", + other_hhmemb: 0 + ) + end + + let(:skip_section_case_log) do + FactoryBot.create( + :case_log, + :in_progress, + owning_organisation: user.organisation, + managing_organisation: user.organisation, + tenant_code: "123", + age1: 35, + sex1: "Male", + other_hhmemb: 0, + armedforces: "No", + illness: "No", + accessibility_requirements: "Don’t know", + la: "York", + condition_effects: "Hearing - such as deafness or partial hearing" + ) + end + + it "they can click a button to move onto the next section" do + visit("/logs/#{section_completed_case_log.id}/household-characteristics/check-answers") + click_link("Save and go to next incomplete section") + expect(page).to have_current_path("/logs/#{section_completed_case_log.id}/armed-forces") + end + + it "they can click a button to skip sections until the next incomplete section" do + visit("/logs/#{skip_section_case_log.id}/household-characteristics/check-answers") + click_link("Save and go to next incomplete section") + expect(page).to have_current_path("/logs/#{skip_section_case_log.id}/tenancy-code") + end + + it "they can click a button to move to the submission section when all sections have been completed", js:true do + visit("/logs/#{completed_case_log.id}/local-authority/check-answers") + click_link("Save and go to submit") + expect(page).to have_current_path("/logs/#{completed_case_log.id}/declaration") + end + end end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 7b376812e..7b7886dc0 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -33,6 +33,38 @@ RSpec.describe Form, type: :model do end end + describe "next_incomplete_section_redirect_path" do + let(:case_log) { FactoryBot.build(:case_log, :in_progress) } + let(:subsection) { form.get_subsection("household_characteristics") } + + context "when a user is on the check answers page for a subsection" do + + before do + case_log.tenant_code = "123" + case_log.age1 = 35 + case_log.sex1 = "Male" + case_log.other_hhmemb = 0 + end + + it "returns the first page of the next incomplete subsection" do + expect(form.next_incomplete_section_redirect_path(subsection, case_log)).to eq("armed-forces") + end + + it "returns the first page of the next incomplete subsection (skipping completed subsections)" do + case_log.armedforces = "No" + case_log.illness = "No" + case_log.accessibility_requirements = "Don’t know" + case_log.la = "York" + case_log.condition_effects = "Hearing - such as deafness or partial hearing" + expect(form.next_incomplete_section_redirect_path(subsection, case_log)).to eq("tenancy-code") + end + + it "returns the declaration section if all sections are complete" do + expect(form.next_incomplete_section_redirect_path(subsection, completed_case_log)).to eq("declaration") + end + end + end + describe "invalidated_page_questions" do context "when dependencies are not met" do let(:expected_invalid) { %w[la_known cbl conditional_question_no_second_question dependent_question declaration] }