Browse Source

Refactor

pull/2421/head
Kat 2 years ago
parent
commit
b43387d11b
  1. 2
      spec/helpers/interruption_screen_helper_spec.rb
  2. 66
      spec/helpers/locations_helper_spec.rb
  3. 22
      spec/helpers/tasklist_helper_spec.rb
  4. 4
      spec/models/form/subsection_spec.rb

2
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 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).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 end
end end

66
spec/helpers/locations_helper_spec.rb

@ -49,84 +49,88 @@ RSpec.describe LocationsHelper do
describe "Active periods" do describe "Active periods" do
let(:location) { FactoryBot.create(:location, startdate: nil) } 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 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) allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true)
end end
it "returns one active period without to date" do it "returns one active period without to date" do
expect(location_active_periods(location).count).to eq(1) 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 end
it "ignores reactivations that were deactivated on the same day" do 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: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, 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 + 1.month, location:)
location.reload location.reload
expect(location_active_periods(location).count).to eq(1) 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 end
it "returns sequential non reactivated active periods" do 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: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, 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 + 2.months, location:)
location.reload location.reload
expect(location_active_periods(location).count).to eq(2) 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).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago)
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).second).to have_attributes(from: over_a_year_ago + 1.month, to: over_a_year_ago + 2.months)
end end
it "returns sequential reactivated active periods" do 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: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, 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 + 2.months, reactivation_date: over_a_year_ago + 3.months, location:)
location.reload location.reload
expect(location_active_periods(location).count).to eq(3) 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).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago)
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).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: Time.zone.local(2022, 8, 5), to: nil) expect(location_active_periods(location).third).to have_attributes(from: over_a_year_ago + 3.months, to: nil)
end end
it "returns non sequential non reactivated active periods" do 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: over_a_year_ago + 2.months, reactivation_date: over_a_year_ago + 3.months, 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, reactivation_date: nil, location:)
location.reload location.reload
expect(location_active_periods(location).count).to eq(2) 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).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago)
expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil) expect(location_active_periods(location).second).to have_attributes(from: over_a_year_ago + 3.months, to: nil)
end end
it "returns non sequential reactivated active periods" do 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: over_a_year_ago + 2.months, reactivation_date: over_a_year_ago + 3.months, 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, reactivation_date: over_a_year_ago + 1.month, location:)
location.reload location.reload
expect(location_active_periods(location).count).to eq(3) 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).first).to have_attributes(from: beginning_of_collection, to: over_a_year_ago)
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).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: Time.zone.local(2022, 8, 5), to: nil) expect(location_active_periods(location).third).to have_attributes(from: over_a_year_ago + 3.months, to: nil)
end end
it "returns correct active periods when reactivation happends during a deactivated period" do 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: over_a_year_ago, reactivation_date: one_year_ago, 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: beginning_of_collection + 2.days, reactivation_date: over_a_year_ago + 1.month, location:)
location.reload location.reload
expect(location_active_periods(location).count).to eq(2) 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).first).to have_attributes(from: beginning_of_collection, to: beginning_of_collection + 2.days)
expect(location_active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 11, 11), to: nil) expect(location_active_periods(location).second).to have_attributes(from: one_year_ago, to: nil)
end end
it "returns correct active periods when a full deactivation period happens during another deactivation period" do 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: over_a_year_ago, reactivation_date: over_a_year_ago + 1.month, 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 - 1.month, reactivation_date: over_a_year_ago + 3.months, location:)
location.reload location.reload
expect(location_active_periods(location).count).to eq(2) 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).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: Time.zone.local(2022, 7, 7), to: nil) expect(location_active_periods(location).second).to have_attributes(from: over_a_year_ago + 3.months, to: nil)
end end
end end

22
spec/helpers/tasklist_helper_spec.rb

@ -120,10 +120,10 @@ RSpec.describe TasklistHelper do
describe "#review_log_text" do describe "#review_log_text" do
context "with sales log" do context "with sales log" do
context "when collection_period_open? == true" 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 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) allow(sales_log).to receive(:collection_period_open?).and_return(true)
end end
@ -135,10 +135,10 @@ RSpec.describe TasklistHelper do
end end
context "when collection_period_open? == false" do 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 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) allow(sales_log).to receive(:collection_period_open?).and_return(false)
end end
@ -148,10 +148,10 @@ RSpec.describe TasklistHelper do
end end
context "when older_than_previous_collection_year" do 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 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) allow(sales_log).to receive(:older_than_previous_collection_year?).and_return(true)
end end
@ -166,7 +166,7 @@ RSpec.describe TasklistHelper do
let(:lettings_log) { build(:lettings_log, :completed, id: 123) } let(:lettings_log) { build(:lettings_log, :completed, id: 123) }
before do 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) allow(lettings_log).to receive(:collection_period_open?).and_return(true)
end end
@ -178,10 +178,10 @@ RSpec.describe TasklistHelper do
end end
context "when collection_period_open? == false" do 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 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) allow(lettings_log).to receive(:collection_period_open?).and_return(false)
end end
@ -191,10 +191,10 @@ RSpec.describe TasklistHelper do
end end
context "when older_than_previous_collection_year" do 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 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) allow(lettings_log).to receive(:older_than_previous_collection_year?).and_return(true)
end end

4
spec/models/form/subsection_spec.rb

@ -34,7 +34,7 @@ RSpec.describe Form::Subsection, type: :model do
end end
context "with an in progress lettings log" do 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 it "has a status" do
expect(subsection.status(lettings_log)).to eq(:in_progress) 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 context "with optional fields" do
it "has a started status even if only an optional field has been answered" 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) expect(subsection.is_started?(lettings_log)).to be(true)
end end
end end

Loading…
Cancel
Save