From e420f1f26ea6bbe84b89a8d52f2b3509be41b801 Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Tue, 6 Jul 2021 17:07:46 +0100 Subject: [PATCH] Add skip link support for custom HTML and classes --- app/helpers/govuk_skip_link_helper.rb | 9 +++-- spec/helpers/govuk_skip_link_helper_spec.rb | 37 +++++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/app/helpers/govuk_skip_link_helper.rb b/app/helpers/govuk_skip_link_helper.rb index fb6f91e4..0b5e7167 100644 --- a/app/helpers/govuk_skip_link_helper.rb +++ b/app/helpers/govuk_skip_link_helper.rb @@ -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 } diff --git a/spec/helpers/govuk_skip_link_helper_spec.rb b/spec/helpers/govuk_skip_link_helper_spec.rb index 758e273d..a525acc5 100644 --- a/spec/helpers/govuk_skip_link_helper_spec.rb +++ b/spec/helpers/govuk_skip_link_helper_spec.rb @@ -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' } @@ -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 @@ -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 + with_tag('span', with: { class: "yellow" }, text: custom_text) + end + end + end end end