Browse Source

Refactor for testability

pull/619/head
baarkerlounger 3 years ago
parent
commit
055f23723e
  1. 36
      app/helpers/navigation_items_helper.rb
  2. 2
      app/views/layouts/application.html.erb
  3. 86
      spec/helpers/navigation_items_helper_spec.rb

36
app/helpers/navigation_items_helper.rb

@ -1,43 +1,33 @@
module NavigationItemsHelper module NavigationItemsHelper
NavigationItem = Struct.new(:text, :href, :current, :classes) NavigationItem = Struct.new(:text, :href, :current, :classes)
def primary_items(current_user, user) def primary_items(path, current_user)
if current_user.support? if current_user.support?
[ [
NavigationItem.new("Organisations", organisations_path, organisation_current?), NavigationItem.new("Organisations", organisations_path, organisation_current?(path)),
NavigationItem.new("Users", users_path, users_current?(current_user, user)), NavigationItem.new("Users", "/users", users_current?(path)),
NavigationItem.new("Logs", case_logs_path, logs_current?), NavigationItem.new("Logs", case_logs_path, logs_current?(path)),
] ]
else else
[ [
NavigationItem.new("Logs", case_logs_path, logs_current?), NavigationItem.new("Logs", case_logs_path, logs_current?(path)),
NavigationItem.new("Users", users_organisation_path(current_user.organisation), users_current?(current_user, user)), NavigationItem.new("Users", users_organisation_path(current_user.organisation), users_current?(path)),
NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", organisation_current?), NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", organisation_current?(path)),
] ]
end end
end end
private private
def current?(current_controller, controllers) def logs_current?(path)
current_controller.controller_name.in?(Array.wrap(controllers)) path.include?("/logs")
end end
def current_action?(current_controller, action) def users_current?(path)
current_controller.action_name == action path.include?("/users")
end end
def logs_current? def organisation_current?(path)
current?(controller, %w[case_logs form]) path.include?("/organisations") && !path.include?("/users")
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")
end end
end end

2
app/views/layouts/application.html.erb

@ -66,7 +66,7 @@
<% if !current_user.nil? %> <% if !current_user.nil? %>
<%= render PrimaryNavigationComponent.new( <%= render PrimaryNavigationComponent.new(
items: primary_items(current_user, @user), items: primary_items(request.path, current_user),
) %> ) %>
<% end %> <% end %>

86
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
Loading…
Cancel
Save