From 344df8a11eaf123e66b4d2c6319e86c23fc0e872 Mon Sep 17 00:00:00 2001 From: dnoneill Date: Mon, 28 Oct 2024 12:11:47 -0400 Subject: [PATCH] update link_to_search to work with only one search_field --- .../blacklight/skip_link_component.rb | 8 ++- .../blacklight/skip_link_component_spec.rb | 52 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 spec/components/blacklight/skip_link_component_spec.rb diff --git a/app/components/blacklight/skip_link_component.rb b/app/components/blacklight/skip_link_component.rb index 3df5301f2a..21f9a5ad04 100644 --- a/app/components/blacklight/skip_link_component.rb +++ b/app/components/blacklight/skip_link_component.rb @@ -3,13 +3,19 @@ module Blacklight class SkipLinkComponent < Blacklight::Component def link_to_search - link_to t('blacklight.skip_links.search_field'), '#search_field', class: link_classes + link_to t('blacklight.skip_links.search_field'), search_id, class: link_classes end def link_to_main link_to t('blacklight.skip_links.main_content'), '#main-container', class: link_classes end + def search_id + return '#search_field' if helpers.blacklight_config.search_fields.values.many? { |field_def| helpers.should_render_field?(field_def) } + + '#q' + end + def link_classes 'd-inline-flex p-2 m-1' end diff --git a/spec/components/blacklight/skip_link_component_spec.rb b/spec/components/blacklight/skip_link_component_spec.rb new file mode 100644 index 0000000000..b0cd55b339 --- /dev/null +++ b/spec/components/blacklight/skip_link_component_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Blacklight::SkipLinkComponent, type: :component do + subject(:rendered) do + render_inline_to_capybara_node(described_class.new) + end + + before do + allow(controller).to receive(:blacklight_config).and_return(blacklight_config) + end + + context 'with no search fields' do + let(:blacklight_config) do + Blacklight::Configuration.new.configure do |config| + config.search_fields = {} + end + end + + it 'renders skip links with correct link to search' do + expect(rendered).to have_link("Skip to main content", href: '#main-container') + expect(rendered).to have_link("Skip to search", href: "#q") + end + end + + context 'with one search field' do + let(:blacklight_config) do + Blacklight::Configuration.new.configure do |config| + config.search_fields = { "all_fields" => "" } + end + end + + it 'renders skip links with correct link to search' do + expect(rendered).to have_link("Skip to main content", href: "#main-container") + expect(rendered).to have_link("Skip to search", href: "#q") + end + end + + context 'with two search field' do + let(:blacklight_config) do + Blacklight::Configuration.new.configure do |config| + config.search_fields = { "all_fields" => "", "title_field" => "" } + end + end + + it 'renders skip links with correct link to search' do + expect(rendered).to have_link("Skip to main content", href: "#main-container") + expect(rendered).to have_link("Skip to search", href: "#search_field") + end + end +end