Skip to content

Commit

Permalink
update link_to_search to work with only one search_field
Browse files Browse the repository at this point in the history
  • Loading branch information
dnoneill authored and cbeer committed Oct 31, 2024
1 parent 7ea7f37 commit 344df8a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/components/blacklight/skip_link_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions spec/components/blacklight/skip_link_component_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 344df8a

Please sign in to comment.