Browse Source

Fix test, add delimiter and suffix to the numeric error mesage

pull/1227/head
Kat 4 years ago
parent
commit
fb9a91bac7
  1. 6
      app/models/validations/shared_validations.rb
  2. 2
      spec/models/form/sales/questions/deposit_amount_spec.rb
  3. 19
      spec/models/validations/shared_validations_spec.rb

6
app/models/validations/shared_validations.rb

@ -1,4 +1,6 @@
module Validations::SharedValidations module Validations::SharedValidations
include ActionView::Helpers::NumberHelper
def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil, main_label = nil, other_label = nil) def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil, main_label = nil, other_label = nil)
return unless main_field || other_field return unless main_field || other_field
@ -19,8 +21,8 @@ module Validations::SharedValidations
next unless record[question.id] next unless record[question.id]
field = question.check_answer_label || question.id field = question.check_answer_label || question.id
min = [question.prefix, question.min].join("") min = [question.prefix, number_with_delimiter(question.min, delimiter: ","), question.suffix].join("")
max = [question.prefix, question.max].join("") max = [question.prefix, number_with_delimiter(question.max, delimiter: ","), question.suffix].join("")
begin begin
answer = Float(record.public_send("#{question.id}_before_type_cast")) answer = Float(record.public_send("#{question.id}_before_type_cast"))

2
spec/models/form/sales/questions/deposit_amount_spec.rb

@ -48,6 +48,6 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do
end end
it "has correct max" do it "has correct max" do
expect(question.max).to eq(9_999_999) expect(question.max).to eq(999_999)
end end
end end

19
spec/models/validations/shared_validations_spec.rb

@ -5,6 +5,7 @@ RSpec.describe Validations::SharedValidations do
let(:validator_class) { Class.new { include Validations::SharedValidations } } let(:validator_class) { Class.new { include Validations::SharedValidations } }
let(:record) { FactoryBot.create(:lettings_log) } let(:record) { FactoryBot.create(:lettings_log) }
let(:sales_record) { FactoryBot.create(:sales_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
describe "numeric min max validations" do describe "numeric min max validations" do
@ -67,6 +68,24 @@ RSpec.describe Validations::SharedValidations do
expect(record.errors["age6"]).to be_empty expect(record.errors["age6"]).to be_empty
end end
end end
context "when validating percent" do
it "validates that % suffix is added in the error message" do
sales_record.stairbought = "random"
shared_validator.validate_numeric_min_max(sales_record)
expect(sales_record.errors["stairbought"])
.to include(match I18n.t("validations.numeric.valid", field: "Percentage bought in this staircasing transaction", min: "0 percent", max: "100 percent"))
end
end
context "when validating price" do
it "validates that £ prefix and , is added in the error message" do
sales_record.income1 = "random"
shared_validator.validate_numeric_min_max(sales_record)
expect(sales_record.errors["income1"])
.to include(match I18n.t("validations.numeric.valid", field: "Buyer 1’s gross annual income", min: "£0", max: "£999,999"))
end
end
end end
describe "radio options validations" do describe "radio options validations" do

Loading…
Cancel
Save