From d675dccfe971520a39e7c9b98c6841ad4c9a7161 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Wed, 20 Apr 2022 16:44:43 +0100 Subject: [PATCH] Use component for primary navigation --- .../primary_navigation_component.html.erb | 17 ++++++++++++ .../primary_navigation_component.rb | 8 ++++++ .../layouts/_primary_navigation.html.erb | 18 ------------- app/views/layouts/application.html.erb | 2 +- .../primary_navigation_component_spec.rb | 27 +++++++++++++++++++ 5 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 app/components/primary_navigation_component.html.erb create mode 100644 app/components/primary_navigation_component.rb delete mode 100644 app/views/layouts/_primary_navigation.html.erb create mode 100644 spec/components/primary_navigation_component_spec.rb diff --git a/app/components/primary_navigation_component.html.erb b/app/components/primary_navigation_component.html.erb new file mode 100644 index 000000000..a0750d712 --- /dev/null +++ b/app/components/primary_navigation_component.html.erb @@ -0,0 +1,17 @@ + diff --git a/app/components/primary_navigation_component.rb b/app/components/primary_navigation_component.rb new file mode 100644 index 000000000..33f5a9529 --- /dev/null +++ b/app/components/primary_navigation_component.rb @@ -0,0 +1,8 @@ +class PrimaryNavigationComponent < ViewComponent::Base + attr_reader :items + + def initialize(items:) + @items = items + super + end +end diff --git a/app/views/layouts/_primary_navigation.html.erb b/app/views/layouts/_primary_navigation.html.erb deleted file mode 100644 index 0fd8529bd..000000000 --- a/app/views/layouts/_primary_navigation.html.erb +++ /dev/null @@ -1,18 +0,0 @@ - \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9cc24270d..ca0af4254 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -76,7 +76,7 @@ { name: "About your organisation", url: "/organisations/#{current_user.organisation.id}" }, ] %> <% end %> - <%= render partial: "layouts/primary_navigation", locals: {items: items} %> + <%= render PrimaryNavigationComponent.new(items:) %> <% end %>
diff --git a/spec/components/primary_navigation_component_spec.rb b/spec/components/primary_navigation_component_spec.rb new file mode 100644 index 000000000..c088df1e7 --- /dev/null +++ b/spec/components/primary_navigation_component_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +RSpec.describe PrimaryNavigationComponent, type: :component do + let(:items) do + [{ name: "Organisations", url: "#", current: true }, + { name: "Users", url: "#" }, + { name: "Logs ", url: "#" }] + end + + context "when the item is 'current' in nav tabs" do + it "then that tab appears as selected" do + result = render_inline(described_class.new(items:)) + + expect(result.css('.app-primary-navigation__link[aria-current="page"]').text).to include("Organisations") + 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:)) + + expect(result.text).to include("Organisations") + expect(result.text).to include("Users") + expect(result.text).to include("Logs") + end + end +end