diff --git a/app/components/primary_navigation_component.html.erb b/app/components/primary_navigation_component.html.erb
index b889d9122..0bccaaf81 100644
--- a/app/components/primary_navigation_component.html.erb
+++ b/app/components/primary_navigation_component.html.erb
@@ -2,7 +2,7 @@
<% items.each do |item| %>
- <% if item.fetch(:current, false) || item.fetch(:comparable_urls).any? { |url| request.fullpath.include?(url) } %>
+ <% if highlighted_tab?(item, request.fullpath) %>
-
<%= govuk_link_to item[:name], item[:url], class: "app-primary-navigation__link", aria: { current: "page" } %>
diff --git a/app/components/primary_navigation_component.rb b/app/components/primary_navigation_component.rb
index 33f5a9529..41ed73535 100644
--- a/app/components/primary_navigation_component.rb
+++ b/app/components/primary_navigation_component.rb
@@ -5,4 +5,8 @@ class PrimaryNavigationComponent < ViewComponent::Base
@items = items
super
end
+
+ def highlighted_tab?(item, path)
+ item.fetch(:current, false) || item.fetch(:comparable_urls).any? { |url| path.include?(url) }
+ end
end
diff --git a/spec/components/primary_navigation_component_spec.rb b/spec/components/primary_navigation_component_spec.rb
index 349f4f4e0..ef2db67de 100644
--- a/spec/components/primary_navigation_component_spec.rb
+++ b/spec/components/primary_navigation_component_spec.rb
@@ -2,9 +2,9 @@ require "rails_helper"
RSpec.describe PrimaryNavigationComponent, type: :component do
let(:items) do
- [{ name: "Organisations", url: "#", current: true, comparable_urls: [] },
- { name: "Users", url: "#", comparable_urls: [] },
- { name: "Logs ", url: "#", comparable_urls: [] }]
+ [{ name: "Organisations", url: "/organisations", current: true, comparable_urls: ["/organisations", "/something-else"] },
+ { name: "Users", url: "/users", comparable_urls: ["/users"] },
+ { name: "Logs ", url: "/logs", comparable_urls: ["/logs"] }]
end
context "when the item is 'current' in nav tabs" do
@@ -15,6 +15,16 @@ RSpec.describe PrimaryNavigationComponent, type: :component do
end
end
+ context "when the current page is sub-page" do
+ it "highlights the correct tab" do
+ navigation_panel = described_class.new(items:)
+
+ expect(navigation_panel).to be_highlighted_tab(items[0], "/something-else")
+ expect(navigation_panel).not_to be_highlighted_tab(items[1], "/something-else")
+ expect(navigation_panel).not_to be_highlighted_tab(items[2], "/something-else")
+ end
+ end
+
context "when rendering tabs" do
it "all of the nav tabs specified in the items hash are passed to it" do
result = render_inline(described_class.new(items:))