Browse Source

Extract method and add tests

pull/493/head
Kat 3 years ago
parent
commit
e131ecb802
  1. 2
      app/components/primary_navigation_component.html.erb
  2. 4
      app/components/primary_navigation_component.rb
  3. 16
      spec/components/primary_navigation_component_spec.rb

2
app/components/primary_navigation_component.html.erb

@ -2,7 +2,7 @@
<div class="govuk-width-container"> <div class="govuk-width-container">
<ul class="app-primary-navigation__list"> <ul class="app-primary-navigation__list">
<% items.each do |item| %> <% 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) %>
<li class="app-primary-navigation__item app-primary-navigation__item--current"> <li class="app-primary-navigation__item app-primary-navigation__item--current">
<%= govuk_link_to item[:name], item[:url], class: "app-primary-navigation__link", aria: { current: "page" } %> <%= govuk_link_to item[:name], item[:url], class: "app-primary-navigation__link", aria: { current: "page" } %>
</li> </li>

4
app/components/primary_navigation_component.rb

@ -5,4 +5,8 @@ class PrimaryNavigationComponent < ViewComponent::Base
@items = items @items = items
super super
end end
def highlighted_tab?(item, path)
item.fetch(:current, false) || item.fetch(:comparable_urls).any? { |url| path.include?(url) }
end
end end

16
spec/components/primary_navigation_component_spec.rb

@ -2,9 +2,9 @@ require "rails_helper"
RSpec.describe PrimaryNavigationComponent, type: :component do RSpec.describe PrimaryNavigationComponent, type: :component do
let(:items) do let(:items) do
[{ name: "Organisations", url: "#", current: true, comparable_urls: [] }, [{ name: "Organisations", url: "/organisations", current: true, comparable_urls: ["/organisations", "/something-else"] },
{ name: "Users", url: "#", comparable_urls: [] }, { name: "Users", url: "/users", comparable_urls: ["/users"] },
{ name: "Logs ", url: "#", comparable_urls: [] }] { name: "Logs ", url: "/logs", comparable_urls: ["/logs"] }]
end end
context "when the item is 'current' in nav tabs" do context "when the item is 'current' in nav tabs" do
@ -15,6 +15,16 @@ RSpec.describe PrimaryNavigationComponent, type: :component do
end end
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 context "when rendering tabs" do
it "all of the nav tabs specified in the items hash are passed to it" do it "all of the nav tabs specified in the items hash are passed to it" do
result = render_inline(described_class.new(items:)) result = render_inline(described_class.new(items:))

Loading…
Cancel
Save