Skip to content
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
6 changes: 4 additions & 2 deletions app/components/tab_navigation_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ def initialize(label:, routes:, **tag_options)
end

def is_current_path?(path)
request.path == URI.parse(path).path
rescue URI::InvalidURIError
recognized_path = Rails.application.routes.recognize_path(path)
request[:controller] == recognized_path[:controller] &&
request[:action] == recognized_path[:action]
rescue ActionController::RoutingError
false
end
end
31 changes: 27 additions & 4 deletions spec/components/tab_navigation_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@
end

context 'with link for current request' do
before do
allow(vc_test_request).to receive(:path).and_return('/first')
let(:request_path) { '/first' }

around do |example|
Rails.application.routes.draw do
get '(:example_param)/first' => 'application#first'
get '(:example_param)/second' => 'application#second'
end

with_request_url(request_path) { example.run }

Rails.application.reload_routes!
end

it 'renders current link as highlighted' do
Expand All @@ -50,8 +59,22 @@
context 'with routes including query parameters' do
let(:routes) do
[
{ path: '/first?foo=bar', text: 'First' },
{ path: '/second?foo=bar', text: 'Second' },
{ path: '/first?example_param=example_param_value', text: 'First' },
{ path: '/second?example_param=example_param_value', text: 'Second' },
]
end

it 'renders current link as highlighted' do
expect(rendered).to have_link('First') { |link| is_current_link?(link) }
expect(rendered).to have_link('Second') { |link| !is_current_link?(link) }
end
end

context 'with equivalent routes based on param' do
let(:routes) do
[
{ path: '/example_param_value/first', text: 'First' },
{ path: '/example_param_value/second', text: 'Second' },
]
end

Expand Down