Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skip link support for custom HTML and classes #206

Merged
merged 1 commit into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/helpers/govuk_skip_link_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module GovukSkipLinkHelper
def govuk_skip_link(text: 'Skip to main content', href: '#main-content')
link_to text, href, class: 'govuk-skip-link'
def govuk_skip_link(text: 'Skip to main content', href: '#main-content', classes: [], **html_attributes, &block)
link_classes = Array.wrap(classes).append('govuk-skip-link')

return link_to(href, class: link_classes, **html_attributes, &block) if block_given?

link_to(text, href, class: link_classes, **html_attributes)
end
end

ActiveSupport.on_load(:action_view) { include GovukSkipLinkHelper }
37 changes: 35 additions & 2 deletions spec/helpers/govuk_skip_link_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'

RSpec.describe(GovukSkipLinkHelper, type: 'helper') do
include ActionView::Context
include ActionView::Helpers::UrlHelper

let(:custom_link_text) { 'Get straight to the important stuff' }
Expand All @@ -16,7 +17,7 @@
end

context 'when text provided' do
subject { govuk_skip_link text: custom_link_text }
subject { govuk_skip_link(text: custom_link_text) }
it { is_expected.to have_tag('a', with: { href: default_href, class: 'govuk-skip-link' }, text: custom_link_text) }
end

Expand All @@ -26,8 +27,40 @@
end

context 'when href provided' do
subject { govuk_skip_link href: custom_href }
subject { govuk_skip_link(href: custom_href) }
it { is_expected.to have_tag('a', with: { href: custom_href, class: 'govuk-skip-link' }, text: default_link_text) }
end

describe 'extra classes' do
context 'when supplied with extra classes as a string' do
let(:custom_classes) { 'pink' }
subject { govuk_skip_link(classes: custom_classes) }
it { is_expected.to have_tag('a', with: { href: default_href, class: %w(govuk-skip-link).append(custom_classes) }, text: default_link_text) }
end

context 'when supplied with extra classes as an array' do
let(:custom_classes) { %w(yellow spots) }
subject { govuk_skip_link(classes: custom_classes) }
it { is_expected.to have_tag('a', with: { href: default_href, class: %w(govuk-skip-link).append(custom_classes) }, text: default_link_text) }
end
end

describe 'custom html attributes' do
let(:custom_attributes) { { lang: "en-GB", data: { awesome: "yes" } } }
subject { govuk_skip_link(**custom_attributes) }
it { is_expected.to have_tag('a', with: { href: default_href, lang: "en-GB", "data-awesome" => "yes" }, text: default_link_text) }
end

describe 'custom HTML' do
let(:custom_text) { "skip to the content" }
let(:custom_class) { "yellow" }
subject { govuk_skip_link { tag.span(custom_text, class: custom_class) } }

it "inserts the custom HTML into the anchor element" do
expect(subject).to have_tag('a', with: { href: default_href }) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does is_expected.to style work with blocks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry missed this question - I don't believe it's possible.

with_tag('span', with: { class: "yellow" }, text: custom_text)
end
end
end
end
end