diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 193bf2aea..7f99e5e2b 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -354,68 +354,6 @@ RSpec.describe CaseLog do end end - context "when validating major repairs date" do - it "cannot be later than the tenancy start date" do - expect { - described_class.create!( - mrcdate: Date.new(2021, 10, 10), - startdate: Date.new(2021, 10, 9), - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - - expect { - described_class.create!( - mrcdate: Date.new(2021, 10, 9), - startdate: Date.new(2021, 10, 10), - owning_organisation:, - managing_organisation:, - ) - }.not_to raise_error - end - - it "must not be completed if reason for vacancy is first let" do - expect { - described_class.create!( - mrcdate: Date.new(2020, 10, 10), - rsnvac: "First let of new-build property", - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - - expect { - described_class.create!( - mrcdate: Date.new(2020, 10, 10), - rsnvac: "First let of conversion, rehabilitation or acquired property", - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - - expect { - described_class.create!( - mrcdate: Date.new(2020, 10, 10), - rsnvac: "First let of leased property", - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - end - - it "must have less than two years between the tenancy start date and major repairs date" do - expect { - described_class.create!( - startdate: Date.new(2021, 10, 10), - mrcdate: Date.new(2017, 10, 10), - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - end - end - context "when saving void date" do it "must have less than 10 years between the tenancy start date and void" do expect { @@ -630,6 +568,10 @@ RSpec.describe CaseLog do it "validates armed forces" do expect(validator).to receive(:validate_armed_forces) end + + it "validates property major repairs date" do + expect(validator).to receive(:validate_property_major_repairs) + end end describe "status" do diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 6be9e6cf9..67163ce5e 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -24,5 +24,78 @@ RSpec.describe Validations::DateValidations do date_validator.validate_startdate(record) expect(record.errors["startdate"]).to include(match I18n.t("validations.date.invalid_date")) end + + it "does not raise an error when valid" do + record.startdate = Time.zone.local(2022, 1, 1) + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + end + end + + describe "major repairs date" do + it "cannot be after the tenancy start date" do + record.startdate = Time.zone.local(2022, 1, 1) + record.mrcdate = Time.zone.local(2022, 2, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]) + .to include(match I18n.t("validations.property.mrcdate.before_tenancy_start")) + end + + it "must be before the tenancy start date" do + record.startdate = Time.zone.local(2022, 2, 1) + record.mrcdate = Time.zone.local(2022, 1, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]).to be_empty + end + + it "cannot be more than 2 years before the tenancy start date" do + record.startdate = Time.zone.local(2022, 2, 1) + record.mrcdate = Time.zone.local(2020, 1, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]) + .to include(match I18n.t("validations.property.mrcdate.730_days_before_tenancy_start")) + end + + it "must be within 2 years of the tenancy start date" do + record.startdate = Time.zone.local(2022, 2, 1) + record.mrcdate = Time.zone.local(2020, 3, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]).to be_empty + end + + context "when reason for vacancy is first let of property" do + it "validates that no major repair date is provided for a new build" do + record.rsnvac = "First let of new-build property" + record.mrcdate = Time.zone.local(2022, 1, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]) + .to include(match I18n.t("validations.property.mrcdate.not_first_let")) + end + + it "validates that no major repair date is provided for a conversion" do + record.rsnvac = "First let of conversion, rehabilitation or acquired property" + record.mrcdate = Time.zone.local(2022, 1, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]) + .to include(match I18n.t("validations.property.mrcdate.not_first_let")) + end + + it "validates that no major repair date is provided for a leased property" do + record.rsnvac = "First let of leased property" + record.mrcdate = Time.zone.local(2022, 1, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]) + .to include(match I18n.t("validations.property.mrcdate.not_first_let")) + end + end + + context "when the reason for vacancy is not the first let of property" do + it "expects that major repairs can have been done" do + record.rsnvac = "Tenant moved to care home" + record.mrcdate = Time.zone.local(2022, 1, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]).to be_empty + end + end end end