From b43387d11b144a8f88e5ea8cc12b8bdb0210ab58 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 21 Jun 2024 14:58:16 +0100 Subject: [PATCH] Refactor --- .../interruption_screen_helper_spec.rb | 2 +- spec/helpers/locations_helper_spec.rb | 66 ++++++++++--------- spec/helpers/tasklist_helper_spec.rb | 22 +++---- spec/models/form/subsection_spec.rb | 4 +- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/spec/helpers/interruption_screen_helper_spec.rb b/spec/helpers/interruption_screen_helper_spec.rb index f364299cf..cd044e61a 100644 --- a/spec/helpers/interruption_screen_helper_spec.rb +++ b/spec/helpers/interruption_screen_helper_spec.rb @@ -239,7 +239,7 @@ RSpec.describe InterruptionScreenHelper do it "returns a list of questions affected by the soft validation" do expect(soft_validation_affected_questions(question, lettings_log).count).to eq(2) - expect(soft_validation_affected_questions(question, lettings_log).map(&:id)).to eq(%w[ecstat1 age1]) + expect(soft_validation_affected_questions(question, lettings_log).map(&:id)).to match_array(%w[ecstat1 age1]) end end end diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index 863ec5cbe..aba7d7c98 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -49,84 +49,88 @@ RSpec.describe LocationsHelper do describe "Active periods" do let(:location) { FactoryBot.create(:location, startdate: nil) } + let(:today) { Time.zone.local(2023, 10, 10) } + let(:one_year_ago) { today - 1.year } + let(:over_a_year_ago) { one_year_ago - 5.months } + let(:beginning_of_collection) { Time.zone.local(2022, 4, 1) } before do - allow(Time).to receive(:now).and_return(Time.zone.local(2023, 10, 10)) + allow(Time).to receive(:now).and_return(today) allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true) end it "returns one active period without to date" do expect(location_active_periods(location).count).to eq(1) - expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: nil) + expect(location_active_periods(location).first).to have_attributes(from: beginning_of_collection, to: nil) end it "ignores reactivations that were deactivated on the same day" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), location:) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago + 1.month, location:) location.reload expect(location_active_periods(location).count).to eq(1) - expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(location_active_periods(location).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago) end it "returns sequential non reactivated active periods" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), location:) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago + 2.months, location:) location.reload expect(location_active_periods(location).count).to eq(2) - expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) - expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6)) + expect(location_active_periods(location).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago) + expect(location_active_periods(location).second).to have_attributes(from: over_a_year_ago + 1.month, to: over_a_year_ago + 2.months) end it "returns sequential reactivated active periods" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), location:) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago + 2.months, reactivation_date: over_a_year_ago + 3.months, location:) location.reload expect(location_active_periods(location).count).to eq(3) - expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) - expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6)) - expect(location_active_periods(location).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil) + expect(location_active_periods(location).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago) + expect(location_active_periods(location).second).to have_attributes(from: over_a_year_ago + 1.month, to: over_a_year_ago + 2.months) + expect(location_active_periods(location).third).to have_attributes(from: over_a_year_ago + 3.months, to: nil) end it "returns non sequential non reactivated active periods" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5), location:) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: nil, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago + 2.months, reactivation_date: over_a_year_ago + 3.months, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago, reactivation_date: nil, location:) location.reload expect(location_active_periods(location).count).to eq(2) - expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) - expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil) + expect(location_active_periods(location).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago) + expect(location_active_periods(location).second).to have_attributes(from: over_a_year_ago + 3.months, to: nil) end it "returns non sequential reactivated active periods" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5), location:) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago + 2.months, reactivation_date: over_a_year_ago + 3.months, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, location:) location.reload expect(location_active_periods(location).count).to eq(3) - expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) - expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6)) - expect(location_active_periods(location).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil) + expect(location_active_periods(location).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago) + expect(location_active_periods(location).second).to have_attributes(from: over_a_year_ago + 1.month, to: over_a_year_ago + 2.months) + expect(location_active_periods(location).third).to have_attributes(from: over_a_year_ago + 3.months, to: nil) end it "returns correct active periods when reactivation happends during a deactivated period" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 11, 11), location:) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 6), reactivation_date: Time.zone.local(2022, 7, 7), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago, reactivation_date: one_year_ago, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: beginning_of_collection + 2.days, reactivation_date: over_a_year_ago + 1.month, location:) location.reload expect(location_active_periods(location).count).to eq(2) - expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6)) - expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 11, 11), to: nil) + expect(location_active_periods(location).first).to have_attributes(from: beginning_of_collection, to: beginning_of_collection + 2.days) + expect(location_active_periods(location).second).to have_attributes(from: one_year_ago, to: nil) end it "returns correct active periods when a full deactivation period happens during another deactivation period" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 11), location:) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 6), reactivation_date: Time.zone.local(2022, 7, 7), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago - 1.month, reactivation_date: over_a_year_ago + 3.months, location:) location.reload expect(location_active_periods(location).count).to eq(2) - expect(location_active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6)) - expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 7, 7), to: nil) + expect(location_active_periods(location).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago - 1.month) + expect(location_active_periods(location).second).to have_attributes(from: over_a_year_ago + 3.months, to: nil) end end diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index 57335512f..5d3bd4f91 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -120,10 +120,10 @@ RSpec.describe TasklistHelper do describe "#review_log_text" do context "with sales log" do context "when collection_period_open? == true" do - let(:sales_log) { build(:sales_log, :completed, saledate: Time.utc(2022, 6, 9), id: 123) } + let(:sales_log) { build(:sales_log, :completed, saledate: Time.zone.local(2022, 6, 9), id: 123) } before do - allow(sales_log.form).to receive(:submission_deadline).and_return(Time.utc(2023, 6, 9)) + allow(sales_log.form).to receive(:submission_deadline).and_return(Time.zone.local(2023, 6, 9)) allow(sales_log).to receive(:collection_period_open?).and_return(true) end @@ -135,10 +135,10 @@ RSpec.describe TasklistHelper do end context "when collection_period_open? == false" do - let!(:sales_log) { build(:sales_log, :completed, saledate: Time.utc(2022, 6, 1)) } + let!(:sales_log) { build(:sales_log, :completed, saledate: Time.zone.local(2022, 6, 1)) } before do - allow(sales_log.form).to receive(:submission_deadline).and_return(Time.utc(2023, 6, 9)) + allow(sales_log.form).to receive(:submission_deadline).and_return(Time.zone.local(2023, 6, 9)) allow(sales_log).to receive(:collection_period_open?).and_return(false) end @@ -148,10 +148,10 @@ RSpec.describe TasklistHelper do end context "when older_than_previous_collection_year" do - let(:sales_log) { build(:sales_log, :completed, saledate: Time.utc(2022, 2, 1)) } + let(:sales_log) { build(:sales_log, :completed, saledate: Time.zone.local(2022, 2, 1)) } before do - allow(sales_log.form).to receive(:submission_deadline).and_return(Time.utc(2022, 6, 9)) + allow(sales_log.form).to receive(:submission_deadline).and_return(Time.zone.local(2022, 6, 9)) allow(sales_log).to receive(:older_than_previous_collection_year?).and_return(true) end @@ -166,7 +166,7 @@ RSpec.describe TasklistHelper do let(:lettings_log) { build(:lettings_log, :completed, id: 123) } before do - allow(lettings_log.form).to receive(:submission_deadline).and_return(Time.utc(2023, 6, 9)) + allow(lettings_log.form).to receive(:submission_deadline).and_return(Time.zone.local(2023, 6, 9)) allow(lettings_log).to receive(:collection_period_open?).and_return(true) end @@ -178,10 +178,10 @@ RSpec.describe TasklistHelper do end context "when collection_period_open? == false" do - let!(:lettings_log) { build(:lettings_log, :completed, startdate: Time.utc(2022, 6, 1), id: 123) } + let!(:lettings_log) { build(:lettings_log, :completed, startdate: Time.zone.local(2022, 6, 1), id: 123) } before do - allow(lettings_log.form).to receive(:submission_deadline).and_return(Time.utc(2023, 6, 9)) + allow(lettings_log.form).to receive(:submission_deadline).and_return(Time.zone.local(2023, 6, 9)) allow(lettings_log).to receive(:collection_period_open?).and_return(false) end @@ -191,10 +191,10 @@ RSpec.describe TasklistHelper do end context "when older_than_previous_collection_year" do - let(:lettings_log) { build(:lettings_log, :completed, startdate: Time.utc(2022, 2, 1)) } + let(:lettings_log) { build(:lettings_log, :completed, startdate: Time.zone.local(2022, 2, 1)) } before do - allow(lettings_log.form).to receive(:submission_deadline).and_return(Time.utc(2022, 6, 9)) + allow(lettings_log.form).to receive(:submission_deadline).and_return(Time.zone.local(2022, 6, 9)) allow(lettings_log).to receive(:older_than_previous_collection_year?).and_return(true) end diff --git a/spec/models/form/subsection_spec.rb b/spec/models/form/subsection_spec.rb index c8b4e90f5..d02f8099b 100644 --- a/spec/models/form/subsection_spec.rb +++ b/spec/models/form/subsection_spec.rb @@ -34,7 +34,7 @@ RSpec.describe Form::Subsection, type: :model do end context "with an in progress lettings log" do - let(:lettings_log) { FactoryBot.build(:lettings_log, :in_progress, tenancycode: 3, age1: 18) } + let(:lettings_log) { FactoryBot.build(:lettings_log, :in_progress) } it "has a status" do expect(subsection.status(lettings_log)).to eq(:in_progress) @@ -52,7 +52,7 @@ RSpec.describe Form::Subsection, type: :model do context "with optional fields" do it "has a started status even if only an optional field has been answered" do - lettings_log.age1 = nil + lettings_log.tenancycode = 3 expect(subsection.is_started?(lettings_log)).to be(true) end end