diff --git a/app/views/organisations/index.html.erb b/app/views/organisations/index.html.erb
index 4b7590129..966272ed8 100644
--- a/app/views/organisations/index.html.erb
+++ b/app/views/organisations/index.html.erb
@@ -1,9 +1,5 @@
<% item_label = format_label(@pagy.count, "organisation") %>
-<% if @searched.present? %>
- <% title = "Organisations (#{@pagy.count} #{item_label} matching ‘#{@searched}’)" %>
-<% else %>
- <% title = "Organisations" %>
-<% end %>
+<% title = format_title(request.path, @searched, "Organisations", current_user, item_label, @pagy.count) %>
<% content_for :title, title %>
diff --git a/spec/helpers/item_label_helper_spec.rb b/spec/helpers/item_label_helper_spec.rb
index c98029a11..e4f0e210b 100644
--- a/spec/helpers/item_label_helper_spec.rb
+++ b/spec/helpers/item_label_helper_spec.rb
@@ -1,9 +1,8 @@
require "rails_helper"
RSpec.describe ItemLabelHelper do
- let(:item) { "organisation" }
-
describe "#format_label" do
+ let(:item) { "organisation" }
it "returns singular when count is 1" do
expect(format_label(1, item)).to eq("organisation")
end
@@ -12,4 +11,32 @@ RSpec.describe ItemLabelHelper do
expect(format_label(2, item)).to eq("organisations")
end
end
+
+ describe "#format_title" do
+ context "coordinator user" do
+ let(:user) { FactoryBot.create(:user, :support) }
+ let(:path) { "/organisations" }
+ let(:page_title) { "Title" }
+ let(:item_label) { "label" }
+ let(:search_item) { nil }
+ let(:count) { 1 }
+
+ context "search is missing" do
+ let(:expected_title) { page_title }
+
+ it "returns expected title when no search" do
+ expect(format_title(path, nil, page_title, user, item_label, count)).to eq(expected_title)
+ end
+ end
+
+ context "search is present" do
+ let(:search_item) { "foobar" }
+ let(:expected_title) { "#{page_title} (#{count} #{item_label} matching ‘#{search_item}’)" }
+
+ it "returns expected title when search is present" do
+ expect(format_title(path, search_item, page_title, user, item_label, count)).to eq(expected_title)
+ end
+ end
+ end
+ end
end