diff --git a/app/helpers/tasklist_helper.rb b/app/helpers/tasklist_helper.rb index 6512be4d2..61fb7dbe5 100644 --- a/app/helpers/tasklist_helper.rb +++ b/app/helpers/tasklist_helper.rb @@ -29,10 +29,6 @@ module TasklistHelper :in_progress end - def all_questions_completed(case_log) - case_log.attributes.all? { |_question, answer| answer.present?} - end - def get_status_style(status_label) STYLES[status_label] end @@ -40,4 +36,19 @@ module TasklistHelper def get_status_label(status) STATUSES[status] end + + def get_next_incomplete_section(form, case_log) + subsections = form.all_subsections.keys + return subsections.find { |subsection| is_incomplete?(subsection, case_log) } + end + + private + def all_questions_completed(case_log) + case_log.attributes.all? { |_question, answer| answer.present?} + end + + def is_incomplete?(subsection, case_log) + status = get_subsection_status(subsection, case_log) + return status == :not_started || status == :in_progress + end end diff --git a/app/views/case_logs/_tasklist.html.erb b/app/views/case_logs/_tasklist.html.erb index dd139fe9b..df9c6d28b 100644 --- a/app/views/case_logs/_tasklist.html.erb +++ b/app/views/case_logs/_tasklist.html.erb @@ -8,7 +8,7 @@
You've completed 0 of 9 sections.
- Skip to next incomplete section + Skip to next incomplete section
<%= render "tasklist", locals: { form: @form } %> diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index 2761820ad..2ae23d9a3 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -39,6 +39,11 @@ RSpec.describe "Test Features" do assert_selector ".govuk-tag", text: /Completed/, count: 0 assert_selector ".govuk-tag", text: /Cannot start yet/, count: 1 end + + it "skips to the first section if no answers are completed" do + visit("/case_logs/#{empty_case_log.id}") + expect(page).to have_link("Skip to next incomplete section", :href => /#household_characteristics/) + end end it "displays the household questions when you click into that section" do diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index ae74cd716..47bbbbd20 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -3,7 +3,6 @@ require "rails_helper" RSpec.describe TasklistHelper do describe "get subsection status" do let!(:case_log) { FactoryBot.create(:case_log) } - @form = Form.new(2021, 2022) it "returns not started if none of the questions in the subsection are answered" do expect(get_subsection_status("income_and_benefits", case_log)).to eq(:not_started) @@ -28,4 +27,19 @@ RSpec.describe TasklistHelper do expect(get_subsection_status("declaration", completed_case_log)).to eq(:not_started) end end + + describe "get next incomplete section" do + let!(:case_log) { FactoryBot.create(:case_log) } + + it "returns the first subsection name if it is not completed" do + @form = Form.new(2021, 2022) + expect(get_next_incomplete_section(@form, case_log)).to eq("household_characteristics") + end + + it "returns the first subsection name if it is partially completed" do + @form = Form.new(2021, 2022) + case_log["tenant_code"] = 123 + expect(get_next_incomplete_section(@form, case_log)).to eq("household_characteristics") + end + end end