From dd82aee60c2c9b882ad69e86bfc487e909f1f45e Mon Sep 17 00:00:00 2001 From: Jack <113976590+bibblobcode@users.noreply.github.com> Date: Thu, 9 Feb 2023 13:30:55 +0000 Subject: [PATCH] CLDC-1902 update completed text (#1283) * Add tests to cover updated header * Refactor formhandler --- app/models/form_handler.rb | 42 +++---- spec/helpers/tasklist_helper_spec.rb | 181 +++++++++++++++++++-------- 2 files changed, 147 insertions(+), 76 deletions(-) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 15ec6b8fd..8f34288b3 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -3,6 +3,19 @@ class FormHandler include CollectionTimeHelper attr_reader :forms + SALES_SECTIONS = [ + Form::Sales::Sections::PropertyInformation, + Form::Sales::Sections::Household, + Form::Sales::Sections::Finances, + Form::Sales::Sections::SaleInformation, + ].freeze + + LETTINGS_SECTIONS = [ + Form::Lettings::Sections::TenancyAndProperty, + Form::Lettings::Sections::Household, + Form::Lettings::Sections::RentAndCharges, + ].freeze + def initialize @forms = get_all_forms end @@ -20,18 +33,11 @@ class FormHandler end def sales_forms - sales_sections = [ - Form::Sales::Sections::PropertyInformation, - Form::Sales::Sections::Household, - Form::Sales::Sections::Finances, - Form::Sales::Sections::SaleInformation, - ] - current_form = Form.new(nil, current_collection_start_year, sales_sections, "sales") - previous_form = Form.new(nil, current_collection_start_year - 1, sales_sections, "sales") - next_form = Form.new(nil, current_collection_start_year + 1, sales_sections, "sales") - { "current_sales" => current_form, - "previous_sales" => previous_form, - "next_sales" => next_form } + { + "current_sales" => Form.new(nil, current_collection_start_year, SALES_SECTIONS, "sales"), + "previous_sales" => Form.new(nil, current_collection_start_year - 1, SALES_SECTIONS, "sales"), + "next_sales" => Form.new(nil, current_collection_start_year + 1, SALES_SECTIONS, "sales"), + } end def lettings_forms @@ -45,17 +51,11 @@ class FormHandler end end - lettings_sections = [ - Form::Lettings::Sections::TenancyAndProperty, - Form::Lettings::Sections::Household, - Form::Lettings::Sections::RentAndCharges, - ] - if forms["previous_lettings"].blank? && current_collection_start_year >= 2022 - forms["previous_lettings"] = Form.new(nil, current_collection_start_year - 1, lettings_sections, "lettings") + forms["previous_lettings"] = Form.new(nil, current_collection_start_year - 1, LETTINGS_SECTIONS, "lettings") end - forms["current_lettings"] = Form.new(nil, current_collection_start_year, lettings_sections, "lettings") if forms["current_lettings"].blank? - forms["next_lettings"] = Form.new(nil, current_collection_start_year + 1, lettings_sections, "lettings") if forms["next_lettings"].blank? + forms["current_lettings"] = Form.new(nil, current_collection_start_year, LETTINGS_SECTIONS, "lettings") if forms["current_lettings"].blank? + forms["next_lettings"] = Form.new(nil, current_collection_start_year + 1, LETTINGS_SECTIONS, "lettings") if forms["next_lettings"].blank? forms end diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index ff4902a55..f7726745d 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -1,82 +1,153 @@ require "rails_helper" RSpec.describe TasklistHelper do - let(:empty_lettings_log) { FactoryBot.create(:lettings_log) } - let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, needstype: 1) } - let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + describe "with lettings" do + let(:empty_lettings_log) { FactoryBot.create(:lettings_log) } + let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, needstype: 1) } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } - before do - allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) - end + context "with 2021 2022 form" do + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end - describe "get next incomplete section" do - it "returns the first subsection name if it is not completed" do - expect(get_next_incomplete_section(lettings_log).id).to eq("household_characteristics") - end + describe "get next incomplete section" do + it "returns the first subsection name if it is not completed" do + expect(get_next_incomplete_section(lettings_log).id).to eq("household_characteristics") + end - it "returns the first subsection name if it is partially completed" do - lettings_log["tenancycode"] = 123 - expect(get_next_incomplete_section(lettings_log).id).to eq("household_characteristics") - end - end + it "returns the first subsection name if it is partially completed" do + lettings_log["tenancycode"] = 123 + expect(get_next_incomplete_section(lettings_log).id).to eq("household_characteristics") + end + end - describe "get sections count" do - it "returns the total of sections if no status is given" do - expect(get_subsections_count(empty_lettings_log)).to eq(8) - end + describe "get sections count" do + it "returns the total of sections if no status is given" do + expect(get_subsections_count(empty_lettings_log)).to eq(8) + end - it "returns 0 sections for completed sections if no sections are completed" do - expect(get_subsections_count(empty_lettings_log, :completed)).to eq(0) - end + it "returns 0 sections for completed sections if no sections are completed" do + expect(get_subsections_count(empty_lettings_log, :completed)).to eq(0) + end - it "returns the number of not started sections" do - expect(get_subsections_count(empty_lettings_log, :not_started)).to eq(8) - end + it "returns the number of not started sections" do + expect(get_subsections_count(empty_lettings_log, :not_started)).to eq(8) + end - it "returns the number of sections in progress" do - expect(get_subsections_count(lettings_log, :in_progress)).to eq(3) - end + it "returns the number of sections in progress" do + expect(get_subsections_count(lettings_log, :in_progress)).to eq(3) + end - it "returns 0 for invalid state" do - expect(get_subsections_count(lettings_log, :fake)).to eq(0) - end - end + it "returns 0 for invalid state" do + expect(get_subsections_count(lettings_log, :fake)).to eq(0) + end + end - describe "get_next_page_or_check_answers" do - let(:subsection) { lettings_log.form.get_subsection("household_characteristics") } - let(:user) { FactoryBot.build(:user) } + describe "get_next_page_or_check_answers" do + let(:subsection) { lettings_log.form.get_subsection("household_characteristics") } + let(:user) { FactoryBot.build(:user) } - it "returns the check answers page path if the section has been started already" do - expect(next_page_or_check_answers(subsection, lettings_log, user)).to match(/check-answers/) - end + it "returns the check answers page path if the section has been started already" do + expect(next_page_or_check_answers(subsection, lettings_log, user)).to match(/check-answers/) + end - it "returns the first question page path for the section if it has not been started yet" do - expect(next_page_or_check_answers(subsection, empty_lettings_log, user)).to match(/tenant-code-test/) - end + it "returns the first question page path for the section if it has not been started yet" do + expect(next_page_or_check_answers(subsection, empty_lettings_log, user)).to match(/tenant-code-test/) + end + + it "when first question being not routed to returns the next routed question link" do + empty_lettings_log.housingneeds_a = "No" + expect(next_page_or_check_answers(subsection, empty_lettings_log, user)).to match(/person-1-gender/) + end + end - it "when first question being not routed to returns the next routed question link" do - empty_lettings_log.housingneeds_a = "No" - expect(next_page_or_check_answers(subsection, empty_lettings_log, user)).to match(/person-1-gender/) + describe "subsection link" do + let(:subsection) { lettings_log.form.get_subsection("household_characteristics") } + let(:user) { FactoryBot.build(:user) } + + context "with a subsection that's enabled" do + it "returns the subsection link url" do + expect(subsection_link(subsection, lettings_log, user)).to match(/household-characteristics/) + end + end + + context "with a subsection that cannot be started yet" do + before do + allow(subsection).to receive(:status).with(lettings_log).and_return(:cannot_start_yet) + end + + it "returns the label instead of a link" do + expect(subsection_link(subsection, lettings_log, user)).to match(subsection.label) + end + end + end end end - describe "subsection link" do - let(:subsection) { lettings_log.form.get_subsection("household_characteristics") } - let(:user) { FactoryBot.build(:user) } + describe "#review_log_text" do + around do |example| + Timecop.freeze(now) do + Singleton.__init__(FormHandler) + example.run + end + Singleton.__init__(FormHandler) + end - context "with a subsection that's enabled" do - it "returns the subsection link url" do - expect(subsection_link(subsection, lettings_log, user)).to match(/household-characteristics/) + context "with lettings log" do + context "when collection_period_open? == true" do + context "with 2023 deadline" do + let(:now) { Time.utc(2022, 6, 1) } + let(:lettings_log) { create(:lettings_log, :completed) } + + it "returns relevant text" do + expect(review_log_text(lettings_log)).to eq( + "You can #{govuk_link_to 'review and make changes to this log', review_lettings_log_path(lettings_log)} until 1 July 2023.".html_safe, + ) + end + end + + context "with 2024 deadline" do + let(:now) { Time.utc(2023, 6, 20) } + let(:lettings_log) { create(:lettings_log, :completed, national: 18, waityear: 2) } + + it "returns relevant text" do + expect(review_log_text(lettings_log)).to eq( + "You can #{govuk_link_to 'review and make changes to this log', review_lettings_log_path(lettings_log)} until 1 July 2024.".html_safe, + ) + end + end + end + + context "when collection_period_open? == false" do + let(:now) { Time.utc(2023, 7, 8) } + let(:lettings_log) { create(:lettings_log, :completed, startdate: Time.utc(2023, 2, 8)) } + + it "returns relevant text" do + expect(review_log_text(lettings_log)).to eq("This log is from the 2022/2023 collection window, which is now closed.") + end end end - context "with a subsection that cannot be started yet" do - before do - allow(subsection).to receive(:status).with(lettings_log).and_return(:cannot_start_yet) + context "with sales log" do + context "when collection_period_open? == true" do + let(:now) { Time.utc(2022, 6, 1) } + let(:sales_log) { create(:sales_log, :completed) } + + it "returns relevant text" do + expect(review_log_text(sales_log)).to eq( + "You can #{govuk_link_to 'review and make changes to this log', review_lettings_log_path(sales_log)} until 1 July 2023.".html_safe, + ) + end end - it "returns the label instead of a link" do - expect(subsection_link(subsection, lettings_log, user)).to match(subsection.label) + context "when collection_period_open? == false" do + let(:now) { Time.utc(2023, 7, 8) } + let(:sales_log) { create(:sales_log, :completed, saledate: Time.utc(2023, 2, 8)) } + + it "returns relevant text" do + expect(review_log_text(sales_log)).to eq("This log is from the 2022/2023 collection window, which is now closed.") + end end end end