Skip to content

Commit

Permalink
Merge pull request #4784 from McEileen/4673/improve-client-share-erro…
Browse files Browse the repository at this point in the history
…r-message

Improve client share error message
  • Loading branch information
cielf authored Nov 20, 2024
2 parents ca8ccd8 + beec1c8 commit 20cf220
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/models/partners/served_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class ServedArea < ApplicationRecord
belongs_to :partner_profile, class_name: "Partners::Profile"
belongs_to :county
validates :client_share, numericality: {only_integer: true}
validates :client_share, inclusion: {in: 1..100}
validates :client_share, inclusion: {in: 1..100, message: "Client share must be between 1 and 100 inclusive"}
end
end
30 changes: 19 additions & 11 deletions spec/models/partners/served_area_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@
#

RSpec.describe Partners::ServedArea, type: :model do
it { should belong_to(:partner_profile) }
it { should belong_to(:county) }
describe "validations" do
let(:served_area_nil_client_share) { build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: nil) }
it { should belong_to(:partner_profile) }
it { should belong_to(:county) }

it "must only allow integer client shares" do
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 50)).to be_valid
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 50.5)).not_to be_valid
end
it "displays a user friendly error message for nil client shares" do
expect(served_area_nil_client_share).not_to be_valid
expect(served_area_nil_client_share.errors.messages[:client_share]).to match_array(["Client share must be between 1 and 100 inclusive", "is not a number"])
end

it "must only allow integer client shares" do
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 50)).to be_valid
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 50.5)).not_to be_valid
end

it "must only allow valid client share values" do
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 0)).not_to be_valid
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 101)).not_to be_valid
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 1)).to be_valid
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 100)).to be_valid
it "must only allow valid client share values" do
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 0)).not_to be_valid
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 101)).not_to be_valid
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 1)).to be_valid
expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 100)).to be_valid
end
end

describe "versioning" do
Expand Down
2 changes: 1 addition & 1 deletion spec/services/partner_profile_update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
expect(profile.served_areas.size).to eq(2)
result = PartnerProfileUpdateService.new(profile.partner, partner_params, incorrect_attributes_missing_client_share).call
expect(result.success?).to eq(false)
expect(result.error.to_s).to include("Validation failed: Served areas client share is not a number, Served areas client share is not included in the list")
expect(result.error.to_s).to include("Validation failed: Served areas client share is not a number, Served areas client share Client share must be between 1 and 100 inclusive")
profile.reload
expect(profile.served_areas.size).to eq(2)
end
Expand Down

0 comments on commit 20cf220

Please sign in to comment.