From 289565543ec9e2f6dbcb611540c9f28077740b24 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 7 Jun 2024 11:38:28 +0100 Subject: [PATCH] Refactor --- .../sales/household_validations_spec.rb | 5 +-- .../sales/setup_validations_spec.rb | 34 +++++++++++-------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 48ab425c5..c12d7c5b3 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -5,9 +5,11 @@ RSpec.describe Validations::Sales::HouseholdValidations do let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } } let(:record) { build(:sales_log, saledate:) } - let(:saledate) { Time.zone.local(2023, 4, 1) } + let(:saledate) { Time.zone.now } describe "#validate_partner_count" do + let(:saledate) { Time.zone.local(2023, 4, 1) } + it "validates that only 1 partner exists" do record.relat2 = "P" record.relat3 = "P" @@ -337,7 +339,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "#validate_buyer1_previous_tenure" do - let(:saledate) { Time.zone.local(2024, 4, 4) } let(:record) { build(:sales_log, saledate:, ownershipsch: 2) } it "adds an error when previous tenure is not valid" do diff --git a/spec/models/validations/sales/setup_validations_spec.rb b/spec/models/validations/sales/setup_validations_spec.rb index 77c04f8c6..bb41e6fc8 100644 --- a/spec/models/validations/sales/setup_validations_spec.rb +++ b/spec/models/validations/sales/setup_validations_spec.rb @@ -7,6 +7,10 @@ RSpec.describe Validations::Sales::SetupValidations do describe "#validate_saledate_collection_year" do context "with sales_in_crossover_period == false" do + before do + allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false) + end + context "when saledate is blank" do let(:record) { build(:sales_log, saledate: nil) } @@ -18,11 +22,7 @@ RSpec.describe Validations::Sales::SetupValidations do end context "when saledate is in the open collection year" do - let(:record) { build(:sales_log, saledate: Time.zone.local(2023, 1, 1)) } - - before do - allow(Time).to receive(:now).and_return(Time.zone.local(2023, 1, 10)) - end + let(:record) { build(:sales_log) } it "does not add an error" do setup_validator.validate_saledate_collection_year(record) @@ -61,6 +61,10 @@ RSpec.describe Validations::Sales::SetupValidations do end context "with sales_in_crossover_period == true" do + before do + allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) + end + context "when saledate is blank" do let(:record) { build(:sales_log, saledate: nil) } @@ -108,11 +112,11 @@ RSpec.describe Validations::Sales::SetupValidations do allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8)) end - it "cannot create new logs for the previous collection year" do + it "cannot create new logs for the archived collection year" do record.update!(saledate: nil) - record.saledate = Time.zone.local(2024, 1, 1) + record.saledate = Time.zone.local(2023, 1, 1) setup_validator.validate_saledate_collection_year(record) - expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025") + expect(record.errors["saledate"]).to include(match "Enter a date within the 23/24 or 24/25 collection years, which is between 1st April 2023 and 31st March 2025") end xit "can edit already created logs for the previous collection year" do @@ -131,19 +135,19 @@ RSpec.describe Validations::Sales::SetupValidations do allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8)) end - it "cannot create new logs for the previous collection year" do + it "cannot create new logs for the archived collection year" do record.update!(saledate: nil) - record.saledate = Time.zone.local(2024, 1, 1) + record.saledate = Time.zone.local(2023, 1, 1) setup_validator.validate_saledate_collection_year(record) - expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025") + expect(record.errors["saledate"]).to include(match "Enter a date within the 23/24 or 24/25 collection years, which is between 1st April 2023 and 31st March 2025") end - it "cannot edit already created logs for the previous collection year" do - record.saledate = Time.zone.local(2024, 1, 2) + it "cannot edit already created logs for the archived collection year" do + record.saledate = Time.zone.local(2023, 1, 2) record.save!(validate: false) - record.saledate = Time.zone.local(2024, 1, 1) + record.saledate = Time.zone.local(2023, 1, 1) setup_validator.validate_saledate_collection_year(record) - expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025") + expect(record.errors["saledate"]).to include(match "Enter a date within the 23/24 or 24/25 collection years, which is between 1st April 2023 and 31st March 2025") end end end