Skip to content

Commit

Permalink
Raise error if heading level is not 1..6
Browse files Browse the repository at this point in the history
  • Loading branch information
agoldstone93 committed Dec 2, 2024
1 parent e700431 commit b0f71d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= tag.div(**html_attributes) do %>
<div class="<%= brand %>-summary-card__title-wrapper">
<%= content_tag(heading_tag, title, class: "#{brand}-summary-card__title") %>
<%= content_tag(heading_level, title, class: "#{brand}-summary-card__title") %>

<% if actions.any? %>
<ul class="<%= brand %>-summary-card__actions">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class GovukComponent::SummaryListComponent::CardComponent < GovukComponent::Base

def initialize(title:, heading_level: 2, actions: [], classes: [], html_attributes: {})
@title = title
@heading_level = heading_level
@heading_level = heading_tag(heading_level)
actions.each { |a| with_action { a } } if actions.any?

super(classes:, html_attributes:)
Expand All @@ -18,8 +18,10 @@ def default_attributes
{ class: "#{brand}-summary-card" }
end

def heading_tag
"h#{heading_level}"
def heading_tag(level)
fail(ArgumentError, "heading_level must be 1-6") unless level.in?(1..6)

"h#{level}"
end

def action_text(action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,23 @@
end

context "with a custom heading level" do
let(:custom_heading_level) { 3 }
before { render_inline(described_class.new(title:, heading_level: custom_heading_level)) }
context "when the heading level is valid" do
let(:custom_heading_level) { 3 }
before { render_inline(described_class.new(title:, heading_level: custom_heading_level)) }

specify "the card has the right heading level" do
expect(rendered_content).to have_tag(%(h#{custom_heading_level}), with: { class: 'govuk-summary-card__title' })
specify "the card has the right heading level" do
expect(rendered_content).to have_tag(%(h#{custom_heading_level}), with: { class: 'govuk-summary-card__title' })
end
end

context "when the heading level is invalid" do
let(:custom_heading_level) { 8 }

specify "has the overriden level" do
expected_message = "heading_level must be 1-6"

expect { described_class.new(title:, heading_level: custom_heading_level) }.to raise_error(ArgumentError, expected_message)
end
end
end

Expand Down

0 comments on commit b0f71d0

Please sign in to comment.