diff --git a/lib/consul_form_builder.rb b/lib/consul_form_builder.rb index e9d59dcd2..5eb292079 100644 --- a/lib/consul_form_builder.rb +++ b/lib/consul_form_builder.rb @@ -76,14 +76,14 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder end def help_text(attribute, options) - if options[:hint] + if options[:hint].present? tag.span options[:hint], class: "help-text", id: help_text_id(attribute, options) end end def help_text_id(attribute, options) - if options[:hint] - "#{custom_label(attribute, nil, nil).match(/for="([^"]+)"/)[1]}-help-text" + if options[:hint].present? + "#{custom_label(attribute, "Example", nil).match(/for="([^"]+)"/)[1]}-help-text" end end end diff --git a/spec/lib/consul_form_builder_spec.rb b/spec/lib/consul_form_builder_spec.rb index a61e984da..7cc39d7c5 100644 --- a/spec/lib/consul_form_builder_spec.rb +++ b/spec/lib/consul_form_builder_spec.rb @@ -28,11 +28,30 @@ describe ConsulFormBuilder do expect(page).to have_css "input[aria-describedby='dummy_title-help-text']" end + it "does not generate empty hints" do + render builder.text_field(:title, hint: "") + + expect(page).not_to have_css ".help-text" + expect(page).not_to have_css "input[aria-describedby]" + end + it "does not generate a hint attribute" do render builder.text_field(:title) expect(page).not_to have_css "input[hint]" end + + describe "attributes requiring interpolation parameters" do + before { I18n.backend.store_translations(:en, { attributes: { title: "Title %{info}" }}) } + after { I18n.backend.reload! } + + it "generates a hint" do + render builder.text_field(:title, label: "Title whatever", hint: "Make it quick") + + expect(page).to have_css ".help-text", text: "Make it quick" + expect(page).to have_css "input[aria-describedby='dummy_title-help-text']" + end + end end describe "#select" do