Skip to content

Commit 9d53faa

Browse files
committed
Update link_to_tab variable names and add tests
1 parent 5ece9e6 commit 9d53faa

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

app/helpers/search_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ def link_to_result(result)
2828
# @return [String] HTML link element for the tab
2929
def link_to_tab(target, label = nil)
3030
clean_target = target.downcase.gsub(' ', '_').downcase
31-
target = label || target
31+
tab_label = label || target
3232
if @active_tab == clean_target
33-
link_to target, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: clean_target)),
33+
link_to tab_label, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: clean_target)),
3434
aria: { current: 'page' },
3535
class: 'active tab-link',
3636
data: { turbo_frame: 'search-results', turbo_action: 'advance' }
3737
else
38-
link_to target, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: clean_target)),
38+
link_to tab_label, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: clean_target)),
3939
class: 'tab-link',
4040
data: { turbo_frame: 'search-results', turbo_action: 'advance' }
4141
end

test/helpers/search_helper_test.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class SearchHelperTest < ActionView::TestCase
203203
@active_tab = 'bar'
204204
actual = link_to_tab('Foo')
205205

206-
assert_select Nokogiri::HTML::Document.parse( actual ), 'a' do |link|
206+
assert_select Nokogiri::HTML::Document.parse(actual), 'a' do |link|
207207
assert_select '[class*=?]', 'active', count: 0
208208
assert_select '[class*=?]', 'tab-link'
209209
assert_select '[aria-current=?]', 'page', count: 0
@@ -214,7 +214,7 @@ class SearchHelperTest < ActionView::TestCase
214214
@active_tab = 'foo'
215215
actual = link_to_tab('Foo')
216216

217-
assert_select Nokogiri::HTML::Document.parse( actual ), 'a' do |link|
217+
assert_select Nokogiri::HTML::Document.parse(actual), 'a' do |link|
218218
assert_select link, '[class*=?]', 'active'
219219
assert_select link, '[aria-current=?]', 'page', count: 1
220220
end
@@ -231,4 +231,36 @@ class SearchHelperTest < ActionView::TestCase
231231
expected_url = 'https://mit.primo.exlibrisgroup.com/discovery/search?query=any%2Ccontains%2Cdata+%26+analytics&vid=01MIT_INST%3AMIT'
232232
assert_equal expected_url, primo_search_url(query)
233233
end
234+
235+
test 'tab label defaults to target when label is nil' do
236+
@active_tab = 'sample_tab'
237+
actual = link_to_tab('Sample Tab', nil)
238+
assert_select Nokogiri::HTML::Document.parse(actual), 'a' do |link|
239+
assert_equal link.text, 'Sample Tab'
240+
end
241+
end
242+
243+
test 'tab label uses label when label is provided' do
244+
@active_tab = 'sample_tab'
245+
actual = link_to_tab('Sample Tab', 'Custom Label')
246+
assert_select Nokogiri::HTML::Document.parse(actual), 'a' do |link|
247+
assert_equal link.text, 'Custom Label'
248+
end
249+
end
250+
251+
test 'tab param uses clean_target when label is nil' do
252+
@active_tab = 'sample_tab'
253+
actual = link_to_tab('Sample Tab', nil)
254+
assert_select Nokogiri::HTML::Document.parse(actual), 'a' do |link|
255+
assert_equal link.first[:href], '/results?tab=sample_tab'
256+
end
257+
end
258+
259+
test 'tab param uses clean_target when label is provided' do
260+
@active_tab = 'sample_tab'
261+
actual = link_to_tab('Sample Tab', 'Custom Label')
262+
assert_select Nokogiri::HTML::Document.parse(actual), 'a' do |link|
263+
assert_equal link.first[:href], '/results?tab=sample_tab'
264+
end
265+
end
234266
end

0 commit comments

Comments
 (0)