diff --git a/lib/govuk_design_system_formbuilder/elements/legend.rb b/lib/govuk_design_system_formbuilder/elements/legend.rb index 32039df9..0d0a0e8f 100644 --- a/lib/govuk_design_system_formbuilder/elements/legend.rb +++ b/lib/govuk_design_system_formbuilder/elements/legend.rb @@ -65,6 +65,8 @@ def retrieve_text(supplied_text) end def size_class(size) + return nil if size.nil? + fail "invalid size '#{size}', must be xl, l, m or s" unless size.in?(%w(xl l m s)) %(fieldset__legend--#{size}) diff --git a/spec/support/shared/shared_legend_examples.rb b/spec/support/shared/shared_legend_examples.rb index 76d74a60..47744583 100644 --- a/spec/support/shared/shared_legend_examples.rb +++ b/spec/support/shared/shared_legend_examples.rb @@ -41,6 +41,7 @@ context 'valid sizes' do %w(s m l xl).each do |size| let(:size) { size } + context %(when the legend size is #{size}) do subject { builder.send(*args, legend: { text: legend_text, size: }) } @@ -53,6 +54,17 @@ end end end + + context 'when the legend size is nil' do + subject { builder.send(*args, legend: { text: legend_text, size: nil }) } + + specify 'the size class should be absent' do + expect(subject).to_not have_tag('legend', with: { class: 'govuk-fieldset__legend--s' }) + expect(subject).to_not have_tag('legend', with: { class: 'govuk-fieldset__legend--m' }) + expect(subject).to_not have_tag('legend', with: { class: 'govuk-fieldset__legend--l' }) + expect(subject).to_not have_tag('legend', with: { class: 'govuk-fieldset__legend--xl' }) + end + end end context 'with an invalid size' do