diff --git a/app/views/components/forms/_custom_layout.html.erb b/app/views/components/forms/_custom_layout.html.erb index aef0200be..c3456c221 100644 --- a/app/views/components/forms/_custom_layout.html.erb +++ b/app/views/components/forms/_custom_layout.html.erb @@ -15,9 +15,11 @@ <%= sanitize(form.instructions) %>

<% end %> -

- <%= t('form.required_field_html') %> -

+ <% if form.questions.any?(&:is_required?) %> +

+ <%= t('form.required_field_html') %> +

+ <% end %> <%= render 'components/forms/flash', form: form %> <%= render partial: "components/forms/custom", locals: { form: form, questions: form.questions } %> diff --git a/spec/views/components/forms/_custom_layout.html.erb_spec.rb b/spec/views/components/forms/_custom_layout.html.erb_spec.rb new file mode 100644 index 000000000..f6d9b6be6 --- /dev/null +++ b/spec/views/components/forms/_custom_layout.html.erb_spec.rb @@ -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