Skip to content

Commit

Permalink
Show required fields label only if form includes required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwoldatwork authored Oct 20, 2023
2 parents 0ad07f4 + 9d5d740 commit 718cca7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/views/components/forms/_custom_layout.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
<%= sanitize(form.instructions) %>
</p>
<% end %>
<p>
<%= t('form.required_field_html') %>
</p>
<% if form.questions.any?(&:is_required?) %>
<p>
<%= t('form.required_field_html') %>
</p>
<% end %>
<%= render 'components/forms/flash', form: form %>
<%= render partial: "components/forms/custom", locals: { form: form, questions: form.questions } %>
</div>
Expand Down
27 changes: 27 additions & 0 deletions spec/views/components/forms/_custom_layout.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'components/forms/_custom_layout.html.erb' do
let(:form) { create(:form) }

subject(:rendered) do
render partial: 'components/forms/custom_layout', locals: { form: }
end

context 'with required fields' do
let(:form) { create(:form, :a11) }

it 'displays required field label' do
expect(rendered).to have_content(strip_tags(t('form.required_field_html')))
end
end

context 'without required fields' do
let(:form) { create(:form, :star_ratings) }

it 'does not display required field label' do
expect(rendered).not_to have_content(strip_tags(t('form.required_field_html')))
end
end
end

0 comments on commit 718cca7

Please sign in to comment.