diff --git a/app/models/custom_tab.rb b/app/models/custom_tab.rb index aee8fab087..22058efcc1 100644 --- a/app/models/custom_tab.rb +++ b/app/models/custom_tab.rb @@ -2,4 +2,6 @@ class CustomTab < ApplicationRecord belongs_to :enterprise, optional: false + + validates :title, presence: true, length: { maximum: 20 } end diff --git a/spec/models/custom_tab_spec.rb b/spec/models/custom_tab_spec.rb index cad60ef8a2..f192068f63 100644 --- a/spec/models/custom_tab_spec.rb +++ b/spec/models/custom_tab_spec.rb @@ -6,4 +6,10 @@ describe CustomTab do describe 'associations' do it { is_expected.to belong_to(:enterprise).required } end + + describe 'validations' do + it { is_expected.to validate_presence_of(:title) } + + it { is_expected.to validate_length_of(:title).is_at_most(20) } + end end diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index 58e68d34f8..7a59620131 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -753,6 +753,25 @@ describe ' expect(distributor1.reload.custom_tab.content).to eq("
Custom tab content
") end + context "managing errors" do + it "can't save custom tab fields if title is blank" do + fill_in "enterprise_custom_tab_attributes_title", with: "" + fill_in_trix_editor "custom_tab_content", with: "Custom tab content" + click_button 'Update' + expect(page).to have_content("Custom tab title can't be blank") + expect(distributor1.reload.custom_tab).to be_nil + end + + it "can't save custom tab fields if title is too long" do + fill_in "enterprise_custom_tab_attributes_title", with: "a" * 21 + fill_in_trix_editor "custom_tab_content", with: "Custom tab content" + click_button 'Update' + expect(page). + to have_content("Custom tab title is too long (maximum is 20 characters)") + expect(distributor1.reload.custom_tab).to be_nil + end + end + context "when custom tab is already created" do let(:custom_tab) { create(:custom_tab, title: "Custom tab title",