Paul Robert Lloyd
3 years ago
committed by
GitHub
7 changed files with 169 additions and 22 deletions
@ -0,0 +1,33 @@
|
||||
module NavigationItemsHelper |
||||
NavigationItem = Struct.new(:text, :href, :current, :classes) |
||||
|
||||
def primary_items(path, current_user) |
||||
if current_user.support? |
||||
[ |
||||
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?(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 logs_current?(path) |
||||
path.include?("/logs") |
||||
end |
||||
|
||||
def users_current?(path) |
||||
path.include?("/users") |
||||
end |
||||
|
||||
def organisation_current?(path) |
||||
path.include?("/organisations") && !path.include?("/users") |
||||
end |
||||
end |
@ -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…
Reference in new issue