Browse Source

CLDC-3278: Restructure tenancy length validations to only error on relevant fields (#2295)

* CLDC-3278: Restructure tenancy length validations to only error on relevant fields

* Update error messages to reflect when length is allowed to be blank correctly

* Don't depend on the rent_type for supported housing

* Rename functions for clarity
pull/2278/head^2
Rachael Booth 10 months ago committed by GitHub
parent
commit
d1298ff10d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 105
      app/models/validations/tenancy_validations.rb
  2. 10
      config/locales/en.yml
  3. 9
      spec/models/lettings_log_spec.rb
  4. 427
      spec/models/validations/tenancy_validations_spec.rb

105
app/models/validations/tenancy_validations.rb

@ -3,48 +3,65 @@ module Validations::TenancyValidations
# or 'validate_' to run on submit as well # or 'validate_' to run on submit as well
include Validations::SharedValidations include Validations::SharedValidations
def validate_fixed_term_tenancy(record) def validate_supported_housing_fixed_tenancy_length(record)
is_present = record.tenancylength.present? return unless record.tenancy_type_fixed_term? && record.is_supported_housing?
is_in_range = record.tenancylength.to_i.between?(min_tenancy_length(record), 99) return if record.tenancylength.blank?
rent_type_dependent_conditions = [
{ min_tenancy_length = 1
condition: (record.is_assured_shorthold_tenancy? && !is_in_range) && is_present, return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
error: I18n.t(
"validations.tenancy.length.shorthold", message = I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:)
min_tenancy_length: min_tenancy_length(record), record.errors.add :needstype, message
), record.errors.add :tenancylength, :tenancylength_invalid, message: message
}, record.errors.add :tenancy, message
{ end
condition: (record.is_secure_tenancy? && !is_in_range) && is_present,
error: I18n.t( def validate_general_needs_fixed_tenancy_length_affordable_social_rent(record)
"validations.tenancy.length.secure", return unless record.tenancy_type_fixed_term? && record.affordable_or_social_rent? && record.is_general_needs?
min_tenancy_length: min_tenancy_length(record), return if record.tenancylength.blank?
),
}, min_tenancy_length = 2
{ return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
condition: (record.is_periodic_tenancy? && !is_in_range) && is_present,
error: I18n.t( message = I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:)
"validations.tenancy.length.secure", record.errors.add :needstype, message
min_tenancy_length: min_tenancy_length(record), record.errors.add :rent_type, message
), record.errors.add :tenancylength, :tenancylength_invalid, message: message
}, record.errors.add :tenancy, message
] end
rent_type_independent_conditions = [
{ def validate_general_needs_fixed_tenancy_length_intermediate_rent(record)
condition: !(record.is_secure_tenancy? || record.is_assured_shorthold_tenancy? || record.is_periodic_tenancy?) && is_present, return unless record.tenancy_type_fixed_term? && !record.affordable_or_social_rent? && record.is_general_needs?
error: I18n.t("validations.tenancy.length.fixed_term_not_required"), return if record.tenancylength.blank?
},
] min_tenancy_length = 1
conditions = rent_type_dependent_conditions + rent_type_independent_conditions return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
conditions.each do |condition| message = I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:)
next unless condition[:condition] record.errors.add :needstype, message
record.errors.add :rent_type, message
record.errors.add :needstype, condition[:error] record.errors.add :tenancylength, :tenancylength_invalid, message: message
record.errors.add :rent_type, condition[:error] if rent_type_dependent_conditions.include?(condition) record.errors.add :tenancy, message
record.errors.add :tenancylength, :tenancylength_invalid, message: condition[:error] end
record.errors.add :tenancy, condition[:error]
end def validate_periodic_tenancy_length(record)
return unless record.is_periodic_tenancy? && record.tenancylength.present?
min_tenancy_length = 1
return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.tenancy.length.invalid_periodic", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message
record.errors.add :tenancy, message
end
def validate_tenancy_length_blank_when_not_required(record)
return if record.tenancylength.blank?
return if record.tenancy_type_fixed_term? || record.is_periodic_tenancy?
message = I18n.t("validations.tenancy.length.fixed_term_not_required")
record.errors.add :tenancylength, :tenancylength_invalid, message: message
record.errors.add :tenancy, message
end end
def validate_other_tenancy_type(record) def validate_other_tenancy_type(record)
@ -59,8 +76,4 @@ module Validations::TenancyValidations
record.errors.add :hhmemb, I18n.t("validations.tenancy.joint_more_than_one_member") record.errors.add :hhmemb, I18n.t("validations.tenancy.joint_more_than_one_member")
end end
end end
def min_tenancy_length(record)
record.is_supported_housing? || record.renttype == 3 || record.is_periodic_tenancy? ? 1 : 2
end
end end

10
config/locales/en.yml

@ -558,8 +558,8 @@ en:
tenancy: tenancy:
length: length:
fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term" fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term"
shorthold: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type" invalid_fixed: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type"
secure: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type" invalid_periodic: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type"
internal_transfer: "Answer must be secure tenancy as this tenancy is an internal transfer" internal_transfer: "Answer must be secure tenancy as this tenancy is an internal transfer"
cannot_be_internal_transfer: "Answer cannot be internal transfer as this is not a secure tenancy" cannot_be_internal_transfer: "Answer cannot be internal transfer as this is not a secure tenancy"
not_joint: "This cannot be a joint tenancy as you've told us there's only one person in the household" not_joint: "This cannot be a joint tenancy as you've told us there's only one person in the household"
@ -691,7 +691,7 @@ Make sure these answers are correct."
shared_ownership_deposit: shared_ownership_deposit:
title_text: "You told us that the %{mortgage_deposit_and_discount_error_fields} add up to %{mortgage_deposit_and_discount_total}" title_text: "You told us that the %{mortgage_deposit_and_discount_error_fields} add up to %{mortgage_deposit_and_discount_total}"
old_persons_shared_ownership: old_persons_shared_ownership:
title_text: title_text:
one: "You told us the buyer is using the Older Persons Shared Ownership scheme." one: "You told us the buyer is using the Older Persons Shared Ownership scheme."
two: "You told us the buyers are using the Older Persons Shared Ownership scheme." two: "You told us the buyers are using the Older Persons Shared Ownership scheme."
hint_text: "At least one buyer must be aged 65 years and over to use this scheme." hint_text: "At least one buyer must be aged 65 years and over to use this scheme."
@ -718,12 +718,12 @@ Make sure these answers are correct."
title_text: "You told us that the percentage discount is %{discount}." title_text: "You told us that the percentage discount is %{discount}."
hint_text: "This is higher than we would expect." hint_text: "This is higher than we would expect."
savings: savings:
title_text: title_text:
one: "You told us the buyer’s savings were %{savings}." one: "You told us the buyer’s savings were %{savings}."
two: "You told us the buyers’ savings were %{savings}." two: "You told us the buyers’ savings were %{savings}."
hint_text: "This is higher than we would expect." hint_text: "This is higher than we would expect."
deposit: deposit:
title_text: title_text:
one: "You told us the buyer’s deposit was %{deposit} and their savings were %{savings}." one: "You told us the buyer’s deposit was %{deposit} and their savings were %{savings}."
two: "You told us the buyers’ deposit was %{deposit} and their savings were %{savings}." two: "You told us the buyers’ deposit was %{deposit} and their savings were %{savings}."
hint_text: "The deposit amount is higher than we would expect for the amount of savings they have." hint_text: "The deposit amount is higher than we would expect for the amount of savings they have."

9
spec/models/lettings_log_spec.rb

@ -112,10 +112,17 @@ RSpec.describe LettingsLog do
end end
it "validates tenancy type" do it "validates tenancy type" do
expect(validator).to receive(:validate_fixed_term_tenancy)
expect(validator).to receive(:validate_other_tenancy_type) expect(validator).to receive(:validate_other_tenancy_type)
end end
it "validates tenancy length" do
expect(validator).to receive(:validate_supported_housing_fixed_tenancy_length)
expect(validator).to receive(:validate_general_needs_fixed_tenancy_length_affordable_social_rent)
expect(validator).to receive(:validate_general_needs_fixed_tenancy_length_intermediate_rent)
expect(validator).to receive(:validate_periodic_tenancy_length)
expect(validator).to receive(:validate_tenancy_length_blank_when_not_required)
end
it "validates the previous postcode" do it "validates the previous postcode" do
expect(validator).to receive(:validate_previous_accommodation_postcode) expect(validator).to receive(:validate_previous_accommodation_postcode)
end end

427
spec/models/validations/tenancy_validations_spec.rb

@ -3,280 +3,272 @@ require "rails_helper"
RSpec.describe Validations::TenancyValidations do RSpec.describe Validations::TenancyValidations do
subject(:tenancy_validator) { validator_class.new } subject(:tenancy_validator) { validator_class.new }
before do let(:validator_class) { Class.new { include Validations::TenancyValidations } }
Timecop.freeze(Time.zone.local(2021, 5, 1))
end
after do describe "tenancy length validations" do
Timecop.unfreeze let(:record) { FactoryBot.create(:lettings_log, :setup_completed) }
end
let(:validator_class) { Class.new { include Validations::TenancyValidations } } shared_examples "adds expected errors based on the tenancy length" do |tenancy_type_case, error_fields, min_tenancy_length|
let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2021, 5, 1), needstype: 1, rent_type: 1) } context "and tenancy type is #{tenancy_type_case[:name]}" do
let(:expected_error) { tenancy_type_case[:expected_error].call(min_tenancy_length) }
describe "fixed term tenancy validations" do
context "when fixed term tenancy" do
context "when type of tenancy is not assured or assured shorthold" do
let(:expected_error) { I18n.t("validations.tenancy.length.fixed_term_not_required") }
it "tenancy length should not be present" do
record.tenancy = 3
record.tenancylength = 10
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors["needstype"]).to include(match(expected_error))
expect(record.errors["rent_type"]).not_to include(match(expected_error))
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when type of tenancy is assured shorthold" do before { record.tenancy = tenancy_type_case[:code] }
let(:expected_error) do
I18n.t(
"validations.tenancy.length.shorthold",
min_tenancy_length: 2,
)
end
before { record.tenancy = 4 } context "and tenancy length is less than #{min_tenancy_length}" do
before { record.tenancylength = min_tenancy_length - 1 }
context "when tenancy length is less than 2" do it "adds errors to #{error_fields.join(', ')}" do
it "adds an error" do validation.call(record)
record.tenancylength = 1 error_fields.each do |field|
tenancy_validator.validate_fixed_term_tenancy(record) expect(record.errors[field]).to include(match(expected_error))
expect(record.errors["needstype"]).to include(match(expected_error)) end
expect(record.errors["rent_type"]).to include(match(expected_error)) expect(record.errors.size).to be(error_fields.length)
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end end
end end
context "when tenancy length is greater than 99" do context "and tenancy length is more than 99" do
it "adds an error" do before { record.tenancylength = 100 }
record.tenancylength = 100
tenancy_validator.validate_fixed_term_tenancy(record) it "adds errors to #{error_fields.join(', ')}" do
expect(record.errors["needstype"]).to include(match(expected_error)) validation.call(record)
expect(record.errors["rent_type"]).to include(match(expected_error)) error_fields.each do |field|
expect(record.errors["tenancylength"]).to include(match(expected_error)) expect(record.errors[field]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error)) end
expect(record.errors.size).to be(error_fields.length)
end end
end end
context "when tenancy length is between 2-99" do context "and tenancy length is between #{min_tenancy_length} and 99" do
it "does not add an error" do before { record.tenancylength = min_tenancy_length }
record.tenancylength = 3
tenancy_validator.validate_fixed_term_tenancy(record) it "does not add errors" do
validation.call(record)
expect(record.errors).to be_empty expect(record.errors).to be_empty
end end
end end
context "when tenancy length has not been answered" do context "and tenancy length is not set" do
it "does not add an error" do before { record.tenancylength = nil }
record.tenancylength = nil
tenancy_validator.validate_fixed_term_tenancy(record) it "does not add errors" do
validation.call(record)
expect(record.errors).to be_empty expect(record.errors).to be_empty
end end
end end
end end
end
context "when the collection start year is before 2022" do shared_examples "does not add errors when tenancy type is not fixed term" do
context "when type of tenancy is secure" do context "and tenancy type is not fixed term" do
let(:expected_error) do before do
I18n.t( record.tenancy = 8
"validations.tenancy.length.secure", record.tenancylength = 0
min_tenancy_length: 2, end
)
end it "does not add errors" do
validation.call(record)
expect(record.errors).to be_empty
end
end
end
before { record.tenancy = 1 } fixed_term_tenancy_type_cases = [
{
name: "assured shorthold",
code: 4,
expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) },
},
{
name: "secure fixed term",
code: 6,
expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) },
},
]
describe "#validate_supported_housing_fixed_tenancy_length" do
subject(:validation) { ->(record) { tenancy_validator.validate_supported_housing_fixed_tenancy_length(record) } }
context "when needs type is supported housing" do
before { record.needstype = 2 }
error_fields = %w[needstype tenancylength tenancy]
fixed_term_tenancy_type_cases.each do |tenancy_type_case|
include_examples "adds expected errors based on the tenancy length", tenancy_type_case, error_fields, 1
end
context "when tenancy length is less than 2" do include_examples "does not add errors when tenancy type is not fixed term"
it "adds an error" do end
record.tenancylength = 1
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors["needstype"]).to include(match(expected_error))
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when tenancy length is greater than 99" do context "when needs type is general needs" do
it "adds an error" do before do
record.tenancylength = 100 record.needstype = 1
tenancy_validator.validate_fixed_term_tenancy(record) record.tenancy = 4
expect(record.errors["needstype"]).to include(match(expected_error)) record.tenancylength = 0
expect(record.errors["tenancylength"]).to include(match(expected_error)) end
expect(record.errors["tenancy"]).to include(match(expected_error))
end it "does not add errors" do
validation.call(record)
expect(record.errors).to be_empty
end
end
end
describe "#validate_general_needs_fixed_tenancy_length_affordable_social_rent" do
subject(:validation) { ->(record) { tenancy_validator.validate_general_needs_fixed_tenancy_length_affordable_social_rent(record) } }
context "when needs type is general needs" do
before { record.needstype = 1 }
context "and rent type is affordable or social rent" do
before { record.renttype = 1 }
error_fields = %w[needstype rent_type tenancylength tenancy]
fixed_term_tenancy_type_cases.each do |tenancy_type_case|
include_examples "adds expected errors based on the tenancy length", tenancy_type_case, error_fields, 2
end end
context "when tenancy length is between 2-99" do include_examples "does not add errors when tenancy type is not fixed term"
it "does not add an error" do end
record.tenancylength = 3
tenancy_validator.validate_fixed_term_tenancy(record) context "and rent type is intermediate rent" do
expect(record.errors).to be_empty before do
end record.renttype = 3
record.tenancy = 4
record.tenancylength = 0
end end
context "when tenancy length has not been answered" do it "does not add errors" do
it "does not add an error" do validation.call(record)
record.tenancylength = nil expect(record.errors).to be_empty
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors).to be_empty
end
end end
end end
end end
context "when the collection start year is 2022 or later" do context "when needs type is supported housing" do
before do before do
Timecop.freeze(2022, 5, 1) record.needstype = 2
record.renttype = 1
record.tenancy = 4
record.tenancylength = 0
end end
after do it "does not add errors" do
Timecop.unfreeze validation.call(record)
expect(record.errors).to be_empty
end end
end
end
let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 5, 1), needstype: 1, rent_type: 1) } describe "#validate_general_needs_fixed_tenancy_length_intermediate_rent" do
subject(:validation) { ->(record) { tenancy_validator.validate_general_needs_fixed_tenancy_length_intermediate_rent(record) } }
context "when type of tenancy is Secure - fixed term" do context "when needs type is general needs" do
let(:expected_error) do before { record.needstype = 1 }
I18n.t(
"validations.tenancy.length.secure",
min_tenancy_length: 2,
)
end
before { record.tenancy = 6 } context "and rent type is intermediate rent" do
before { record.renttype = 3 }
context "when tenancy length is less than 2" do error_fields = %w[needstype rent_type tenancylength tenancy]
it "adds an error" do fixed_term_tenancy_type_cases.each do |tenancy_type_case|
record.tenancylength = 1 include_examples "adds expected errors based on the tenancy length", tenancy_type_case, error_fields, 1
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors["needstype"]).to include(match(expected_error))
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end end
context "when tenancy length is greater than 99" do include_examples "does not add errors when tenancy type is not fixed term"
it "adds an error" do end
record.tenancylength = 100
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors["needstype"]).to include(match(expected_error))
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when tenancy length is between 2-99" do context "and rent type is not intermediate rent" do
it "does not add an error" do before do
record.tenancylength = 3 record.renttype = 2
tenancy_validator.validate_fixed_term_tenancy(record) record.tenancy = 4
expect(record.errors).to be_empty record.tenancylength = 0
end
end end
context "when tenancy length has not been answered" do it "does not add errors" do
it "does not add an error" do validation.call(record)
record.tenancylength = nil expect(record.errors).to be_empty
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors).to be_empty
end
end end
end end
end
context "when type of tenancy is Secure - lifetime" do context "when needs type is supported housing" do
let(:expected_error) do before do
I18n.t( record.needstype = 2
"validations.tenancy.length.secure", record.renttype = 3
min_tenancy_length: 2, record.tenancy = 4
) record.tenancylength = 0
end end
before { record.tenancy = 7 } it "does not add errors" do
validation.call(record)
expect(record.errors).to be_empty
end
end
end
context "when tenancy length is less than 2" do describe "#validate_periodic_tenancy_length" do
it "adds an error" do subject(:validation) { ->(record) { tenancy_validator.validate_periodic_tenancy_length(record) } }
record.tenancylength = 1
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors["needstype"]).to include(match(expected_error))
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when tenancy length is greater than 99" do periodic_tenancy_case = {
it "adds an error" do name: "periodic",
record.tenancylength = 100 code: 8,
tenancy_validator.validate_fixed_term_tenancy(record) expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_periodic", min_tenancy_length:) },
expect(record.errors["needstype"]).to include(match(expected_error)) }
expect(record.errors["tenancylength"]).to include(match(expected_error)) error_fields = %w[tenancylength tenancy]
expect(record.errors["tenancy"]).to include(match(expected_error)) include_examples "adds expected errors based on the tenancy length", periodic_tenancy_case, error_fields, 1
end
end
context "when tenancy length is between 2-99" do context "when tenancy type is not periodic" do
it "does not add an error" do before do
record.tenancylength = 3 record.tenancy = 6
tenancy_validator.validate_fixed_term_tenancy(record) record.tenancylength = 0
expect(record.errors).to be_empty end
end
end
context "when tenancy length has not been answered" do it "does not add errors" do
it "does not add an error" do validation.call(record)
record.tenancylength = nil expect(record.errors).to be_empty
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors).to be_empty
end
end
end end
end
context "when type of tenancy is periodic" do describe "#validate_tenancy_length_blank_when_not_required" do
let(:expected_error) do context "when a tenancy length is provided" do
I18n.t( before { record.tenancylength = 10 }
"validations.tenancy.length.secure",
min_tenancy_length: 1,
)
end
before { record.tenancy = 8 } context "and tenancy type is not fixed term or periodic" do
before { record.tenancy = 5 }
context "when tenancy length is less than 1" do it "adds errors to tenancylength and tenancy" do
it "adds an error" do tenancy_validator.validate_tenancy_length_blank_when_not_required(record)
record.tenancylength = 0 expected_error = I18n.t("validations.tenancy.length.fixed_term_not_required")
tenancy_validator.validate_fixed_term_tenancy(record) expect(record.errors["tenancylength"]).to include(expected_error)
expect(record.errors["needstype"]).to include(match(expected_error)) expect(record.errors["tenancy"]).to include(expected_error)
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end end
end end
context "when tenancy length is greater than 99" do tenancy_types_with_length = [
it "adds an error" do { name: "assured shorthold", code: 4 },
record.tenancylength = 100 { name: "secure fixed term", code: 6 },
tenancy_validator.validate_fixed_term_tenancy(record) { name: "periodic", code: 8 },
expect(record.errors["needstype"]).to include(match(expected_error)) ]
expect(record.errors["tenancylength"]).to include(match(expected_error)) tenancy_types_with_length.each do |type|
expect(record.errors["tenancy"]).to include(match(expected_error)) context "and tenancy type is #{type[:name]}" do
before { record.tenancy = type[:code] }
it "does not add errors" do
tenancy_validator.validate_tenancy_length_blank_when_not_required(record)
expect(record.errors).to be_empty
end
end end
end end
end
context "when tenancy length is between 2-99" do context "when tenancy length is not provided" do
it "does not add an error" do before do
record.tenancylength = 3 record.tenancylength = nil
tenancy_validator.validate_fixed_term_tenancy(record) record.tenancy = 5
expect(record.errors).to be_empty
end
end end
context "when tenancy length has not been answered" do it "does not add errors" do
it "does not add an error" do tenancy_validator.validate_tenancy_length_blank_when_not_required(record)
record.tenancylength = nil expect(record.errors).to be_empty
tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors).to be_empty
end
end end
end end
end end
@ -284,6 +276,7 @@ RSpec.describe Validations::TenancyValidations do
end end
describe "tenancy type validations" do describe "tenancy type validations" do
let(:record) { FactoryBot.create(:lettings_log, :setup_completed) }
let(:field) { "validations.other_field_missing" } let(:field) { "validations.other_field_missing" }
let(:main_field_label) { "tenancy type" } let(:main_field_label) { "tenancy type" }
let(:other_field) { "tenancyother" } let(:other_field) { "tenancyother" }
@ -327,20 +320,11 @@ RSpec.describe Validations::TenancyValidations do
describe "joint tenancy validation" do describe "joint tenancy validation" do
context "when the data inputter has said that there is only one member in the household" do context "when the data inputter has said that there is only one member in the household" do
before do let(:record) { FactoryBot.create(:lettings_log, :setup_completed, hhmemb: 1) }
Timecop.freeze(2022, 5, 1)
end
after do
Timecop.unfreeze
end
let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 5, 1)) }
let(:expected_error) { I18n.t("validations.tenancy.not_joint") } let(:expected_error) { I18n.t("validations.tenancy.not_joint") }
let(:hhmemb_expected_error) { I18n.t("validations.tenancy.joint_more_than_one_member") } let(:hhmemb_expected_error) { I18n.t("validations.tenancy.joint_more_than_one_member") }
it "displays an error if the data inputter says the letting is a joint tenancy" do it "displays an error if the data inputter says the letting is a joint tenancy" do
record.hhmemb = 1
record.joint = 1 record.joint = 1
tenancy_validator.validate_joint_tenancy(record) tenancy_validator.validate_joint_tenancy(record)
expect(record.errors["joint"]).to include(match(expected_error)) expect(record.errors["joint"]).to include(match(expected_error))
@ -348,7 +332,6 @@ RSpec.describe Validations::TenancyValidations do
end end
it "does not display an error if the data inputter says the letting is not a joint tenancy" do it "does not display an error if the data inputter says the letting is not a joint tenancy" do
record.hhmemb = 1
record.joint = 2 record.joint = 2
tenancy_validator.validate_joint_tenancy(record) tenancy_validator.validate_joint_tenancy(record)
expect(record.errors["joint"]).to be_empty expect(record.errors["joint"]).to be_empty
@ -356,7 +339,6 @@ RSpec.describe Validations::TenancyValidations do
end end
it "does not display an error if the data inputter has given the household members but not input if it is a joint tenancy" do it "does not display an error if the data inputter has given the household members but not input if it is a joint tenancy" do
record.hhmemb = 1
record.joint = nil record.joint = nil
tenancy_validator.validate_joint_tenancy(record) tenancy_validator.validate_joint_tenancy(record)
expect(record.errors["joint"]).to be_empty expect(record.errors["joint"]).to be_empty
@ -364,7 +346,6 @@ RSpec.describe Validations::TenancyValidations do
end end
it "does not error when don't know answer to joint" do it "does not error when don't know answer to joint" do
record.hhmemb = 1
record.joint = 3 record.joint = 3
tenancy_validator.validate_joint_tenancy(record) tenancy_validator.validate_joint_tenancy(record)

Loading…
Cancel
Save