Submit social housing lettings and sales data (CORE)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

167 lines
7.4 KiB

require "rails_helper"
RSpec.describe LocationDeactivationPeriod do
let(:validator) { LocationDeactivationPeriodValidator.new }
let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) }
let(:current_collection_start_date) { Time.zone.local(2023, 4, 1) }
let(:location) { FactoryBot.create(:location, startdate: previous_collection_start_date - 2.years) }
let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: current_collection_start_date, location:) }
describe "#validate" do
before do
allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date)
allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date)
end
context "when not in a crossover period" do
before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false)
end
context "with a deactivation date before the current collection period" do
it "adds an error" do
record.deactivation_date = current_collection_start_date - 1.year
location.location_deactivation_periods.clear
validator.validate(record)
expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2023."
end
end
context "with a deactivation date in the current collection period" do
it "does not add an error" do
record.deactivation_date = current_collection_start_date + 1.day
location.location_deactivation_periods.clear
validator.validate(record)
expect(record.errors).to be_empty
end
end
end
context "when in a crossover period" do
before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true)
end
context "with a deactivation date before the previous collection period" do
it "does not add an error" do
record.deactivation_date = previous_collection_start_date - 2.years
location.location_deactivation_periods.clear
validator.validate(record)
expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2022."
end
end
context "with a deactivation date in the previous collection period" do
it "does not add an error" do
record.deactivation_date = previous_collection_start_date + 1.day
location.location_deactivation_periods.clear
validator.validate(record)
expect(record.errors).to be_empty
end
end
context "with a deactivation date in the current collection period" do
it "does not add an error" do
record.deactivation_date = current_collection_start_date + 1.day
location.location_deactivation_periods.clear
validator.validate(record)
expect(record.errors).to be_empty
end
end
context "but the location was created in the current collection period" do
let(:location) { FactoryBot.create(:location, startdate:) }
let(:startdate) { current_collection_start_date + 2.days }
let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: current_collection_start_date + 3.days, location:) }
context "with a deactivation date in the previous collection period" do
it "adds an error" do
record.deactivation_date = previous_collection_start_date + 1.day
location.location_deactivation_periods.clear
validator.validate(record)
start_date = startdate.to_formatted_s(:govuk_date)
expect(record.errors[:deactivation_date]).to include "The location cannot be deactivated before #{start_date}, the date when it was first available."
end
end
end
end
context "when there is an open deactivation period less than six months in the future" do # validate_reactivation
let!(:location) { FactoryBot.build(:location, created_at: previous_collection_start_date - 2.years) }
before do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 5.months, location:)
end
context "when reactivation date is nil" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now, location:) }
it "adds an error" do
validator.validate(record)
expect(record.errors.count).to eq(1)
expect(record.errors[:reactivation_date_type]).to include("Select one of the options.")
end
end
context "when reactivation date is present" do
context "when reactivation date is before the existing period's start" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, location:) }
it "adds an error" do
validator.validate(record)
expect(record.errors[:reactivation_date].count).to eq(1)
expect(record.errors[:reactivation_date][0]).to match("The reactivation date must be on or after deactivation date.")
end
end
context "when reactivation date is after the existing period's start" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 6.months, location:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
end
end
context "when there is not an open deactivation period within six months" do # validate_deactivation
let!(:location) { FactoryBot.create(:location, created_at: previous_collection_start_date - 2.years) }
before do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 7.months, reactivation_date: Time.zone.now + 8.months, location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 1.month, reactivation_date: Time.zone.now + 2.months, location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 9.months, location:)
end
context "when reactivation date is nil" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now, location:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
context "when reactivation date is present" do
context "when deactivation date is less than six months in the future" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, location:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
context "when deactivation date is more than six months in the future" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 9.months, reactivation_date: Time.zone.now + 10.months, location:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
end
end
end
end