From 055f23723e29057c80fd6bdaaf4caf7d2014d8b0 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 9 May 2022 15:23:54 +0100 Subject: [PATCH] Refactor for testability --- app/helpers/navigation_items_helper.rb | 36 +++----- app/views/layouts/application.html.erb | 2 +- spec/helpers/navigation_items_helper_spec.rb | 86 ++++++++++++++++++++ 3 files changed, 100 insertions(+), 24 deletions(-) create mode 100644 spec/helpers/navigation_items_helper_spec.rb diff --git a/app/helpers/navigation_items_helper.rb b/app/helpers/navigation_items_helper.rb index 8af63cd4c..970f89993 100644 --- a/app/helpers/navigation_items_helper.rb +++ b/app/helpers/navigation_items_helper.rb @@ -1,43 +1,33 @@ module NavigationItemsHelper NavigationItem = Struct.new(:text, :href, :current, :classes) - def primary_items(current_user, user) + def primary_items(path, current_user) if current_user.support? [ - NavigationItem.new("Organisations", organisations_path, organisation_current?), - NavigationItem.new("Users", users_path, users_current?(current_user, user)), - NavigationItem.new("Logs", case_logs_path, logs_current?), + NavigationItem.new("Organisations", organisations_path, organisation_current?(path)), + NavigationItem.new("Users", "/users", users_current?(path)), + NavigationItem.new("Logs", case_logs_path, logs_current?(path)), ] else [ - NavigationItem.new("Logs", case_logs_path, logs_current?), - NavigationItem.new("Users", users_organisation_path(current_user.organisation), users_current?(current_user, user)), - NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", organisation_current?), + NavigationItem.new("Logs", case_logs_path, logs_current?(path)), + NavigationItem.new("Users", users_organisation_path(current_user.organisation), users_current?(path)), + NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", organisation_current?(path)), ] end end private - def current?(current_controller, controllers) - current_controller.controller_name.in?(Array.wrap(controllers)) + def logs_current?(path) + path.include?("/logs") end - def current_action?(current_controller, action) - current_controller.action_name == action + def users_current?(path) + path.include?("/users") end - def logs_current? - current?(controller, %w[case_logs form]) - end - - def users_current?(current_user, user) - return false if current_user == user - - current?(controller, %w[users]) || current_action?(controller, "users") - end - - def organisation_current? - current?(controller, %w[organisations]) && !current_action?(controller, "users") + def organisation_current?(path) + path.include?("/organisations") && !path.include?("/users") end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 95dd2ac81..7a3e85d89 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -66,7 +66,7 @@ <% if !current_user.nil? %> <%= render PrimaryNavigationComponent.new( - items: primary_items(current_user, @user), + items: primary_items(request.path, current_user), ) %> <% end %> diff --git a/spec/helpers/navigation_items_helper_spec.rb b/spec/helpers/navigation_items_helper_spec.rb new file mode 100644 index 000000000..9a060c759 --- /dev/null +++ b/spec/helpers/navigation_items_helper_spec.rb @@ -0,0 +1,86 @@ +require "rails_helper" + +RSpec.describe NavigationItemsHelper do + let(:current_user) { FactoryBot.create(:user, :data_coordinator) } + + let(:users_path) { "/organisations/#{current_user.organisation.id}/users" } + let(:organisation_path) { "/organisations/#{current_user.organisation.id}" } + + describe "#primary items" do + context "when the user is a data coordinator" do + context "when the user is on the users page" do + let(:expected_navigation_items) do + [ + NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), + NavigationItemsHelper::NavigationItem.new("Users", users_path, true), + NavigationItemsHelper::NavigationItem.new("About your organisation", organisation_path, false), + ] + end + + it "returns navigation items with the users item set as current" do + expect(primary_items(users_path, current_user)).to eq(expected_navigation_items) + end + end + + context "when the user is on their organisation details page" do + let(:expected_navigation_items) do + [ + NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), + NavigationItemsHelper::NavigationItem.new("Users", users_path, false), + NavigationItemsHelper::NavigationItem.new("About your organisation", organisation_path, true), + ] + end + + it "returns navigation items with the users item set as current" do + expect(primary_items("#{organisation_path}/details", current_user)).to eq(expected_navigation_items) + end + end + + context "when the user is on the account page" do + let(:expected_navigation_items) do + [ + NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), + NavigationItemsHelper::NavigationItem.new("Users", "/organisations/#{current_user.organisation.id}/users", false), + NavigationItemsHelper::NavigationItem.new("About your organisation", organisation_path, false), + ] + end + + it "returns navigation items with the users item set as current" do + expect(primary_items("/account", current_user)).to eq(expected_navigation_items) + end + end + end + + context "when the user is a support user" do + let(:current_user) { FactoryBot.create(:user, :support) } + + context "when the user is on the users page" do + let(:expected_navigation_items) do + [ + NavigationItemsHelper::NavigationItem.new("Organisations", "/organisations", false), + NavigationItemsHelper::NavigationItem.new("Users", "/users", true), + NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), + ] + end + + it "returns navigation items with the users item set as current" do + expect(primary_items("/users", current_user)).to eq(expected_navigation_items) + end + end + + context "when the user is on the account page" do + let(:expected_navigation_items) do + [ + NavigationItemsHelper::NavigationItem.new("Organisations", "/organisations", false), + NavigationItemsHelper::NavigationItem.new("Users", "/users", false), + NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), + ] + end + + it "returns navigation items with the users item set as current" do + expect(primary_items("/account", current_user)).to eq(expected_navigation_items) + end + end + end + end +end