Skip to content

Commit

Permalink
Add headings and tests to shared components helper
Browse files Browse the repository at this point in the history
  • Loading branch information
andysellick committed Feb 22, 2019
1 parent 924160c commit ed518c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/govuk_publishing_components/presenters/shared_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
module GovukPublishingComponents
module Presenters
class SharedHelper
attr_reader :options, :margin_bottom
attr_reader :options, :margin_bottom, :heading_level

def initialize(local_assigns)
@options = local_assigns
@margin_bottom = @options[:margin_bottom] || 3
@heading_level = @options[:heading_level] || 2
end

def get_margin_bottom
[*0..9].include?(@margin_bottom) ? "govuk-!-margin-bottom-#{margin_bottom}" : "govuk-!-margin-bottom-3"
end

def get_heading_level
[*1..6].include?(@heading_level) ? "h#{@heading_level}" : "h2"
end
end
end
end
40 changes: 40 additions & 0 deletions spec/lib/components/shared_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

RSpec.describe GovukPublishingComponents::Presenters::SharedHelper do
describe "Shared component helper" do
it "returns a default margin class" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})
margin_class = shared_helper.get_margin_bottom
expect(margin_class).to eql('govuk-!-margin-bottom-3')
end

it "returns a given margin class" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({ margin_bottom: 6 })
margin_class = shared_helper.get_margin_bottom
expect(margin_class).to eql('govuk-!-margin-bottom-6')
end

it "returns the default margin class if passed value is wrong" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({ margin_bottom: "a" })
margin_class = shared_helper.get_margin_bottom
expect(margin_class).to eql('govuk-!-margin-bottom-3')
end

it "returns a default heading level" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})
heading = shared_helper.get_heading_level
expect(heading).to eql('h2')
end

it "returns a given heading level" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({ heading_level: 6 })
heading = shared_helper.get_heading_level
expect(heading).to eql('h6')
end

it "returns the default heading level if passed value is wrong" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({ heading_level: 9 })
heading = shared_helper.get_heading_level
expect(heading).to eql('h2')
end
end
end

0 comments on commit ed518c2

Please sign in to comment.