diff --git a/app/components/search_result_caption_component.html.erb b/app/components/search_result_caption_component.html.erb index 0053e7ae5..8f3d7e5c3 100644 --- a/app/components/search_result_caption_component.html.erb +++ b/app/components/search_result_caption_component.html.erb @@ -1,7 +1,11 @@ - <% if searched.present? %> - <%= count %> <%= item_label %> found matching ‘<%= searched %>’ of <%= total_count %> total <%= item %>. + <% if searched.present? && filters_count&.positive? %> + <%= count %> <%= item_label.pluralize(count) %> matching search and filters + <% elsif searched.present? %> + <%= count %> <%= item_label.pluralize(count) %> matching search + <% elsif filters_count&.positive? %> + <%= count %> <%= item_label.pluralize(count) %> matching filters <% else %> - <%= count %> total <%= item %> + <%= count %> matching <%= item %> <% end %> diff --git a/app/components/search_result_caption_component.rb b/app/components/search_result_caption_component.rb index 2aa7a5ad2..f748641ab 100644 --- a/app/components/search_result_caption_component.rb +++ b/app/components/search_result_caption_component.rb @@ -1,12 +1,13 @@ class SearchResultCaptionComponent < ViewComponent::Base - attr_reader :searched, :count, :item_label, :total_count, :item, :path + attr_reader :searched, :count, :item_label, :total_count, :item, :filters_count - def initialize(searched:, count:, item_label:, total_count:, item:) + def initialize(searched:, count:, item_label:, total_count:, item:, filters_count:) @searched = searched @count = count @item_label = item_label @total_count = total_count @item = item + @filters_count = filters_count super end end diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index a5d825d55..91038dbc9 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -139,12 +139,12 @@ module FiltersHelper request.path.include?("/lettings-logs") end -private - def applied_filters_count(filter_type) filters_count(applied_filters(filter_type)) end +private + def applied_filters(filter_type) return {} unless session[session_name_for(filter_type)] diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb index 79cad8330..ae35d55f6 100644 --- a/app/views/locations/index.html.erb +++ b/app/views/locations/index.html.erb @@ -24,7 +24,7 @@ <%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> - <%= render(SearchResultCaptionComponent.new(searched: @searched, count: @pagy.count, item_label:, total_count: @total_count, item: "locations")) %> + <%= render(SearchResultCaptionComponent.new(searched: @searched, count: @pagy.count, item_label:, total_count: @total_count, item: "locations", filters_count: applied_filters_count(@filter_type))) %> <% end %> <%= table.head do |head| %> <%= head.row do |row| %> diff --git a/app/views/logs/_log_list.html.erb b/app/views/logs/_log_list.html.erb index 0c97e353a..8d00ee42d 100644 --- a/app/views/logs/_log_list.html.erb +++ b/app/views/logs/_log_list.html.erb @@ -1,7 +1,7 @@

- <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "logs")) %> + <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "logs", filters_count: applied_filters_count(@filter_type))) %> <% if logs&.any? %> <%= govuk_link_to "Download (CSV)", csv_download_url, type: "text/csv", class: "govuk-!-margin-right-4" %> <% if @current_user.support? %> diff --git a/app/views/organisations/_organisation_list.html.erb b/app/views/organisations/_organisation_list.html.erb index d93915de8..7c871a125 100644 --- a/app/views/organisations/_organisation_list.html.erb +++ b/app/views/organisations/_organisation_list.html.erb @@ -1,7 +1,7 @@
<%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> - <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "organisations")) %> + <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "organisations", filters_count: applied_filters_count(@filter_type))) %> <% end %> <%= table.head do |head| %> <%= head.row do |row| %> diff --git a/app/views/schemes/_scheme_list.html.erb b/app/views/schemes/_scheme_list.html.erb index 3ca1e5f81..b898e6018 100644 --- a/app/views/schemes/_scheme_list.html.erb +++ b/app/views/schemes/_scheme_list.html.erb @@ -1,7 +1,7 @@
<%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> - <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "schemes")) %> + <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "schemes", filters_count: applied_filters_count(@filter_type))) %> <% end %> <%= table.head do |head| %> <%= head.row do |row| %> diff --git a/app/views/users/_user_list.html.erb b/app/views/users/_user_list.html.erb index c21e74f62..389053294 100644 --- a/app/views/users/_user_list.html.erb +++ b/app/views/users/_user_list.html.erb @@ -1,7 +1,7 @@
<%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> - <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "users")) %> + <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "users", filters_count: applied_filters_count(@filter_type))) %> <% if current_user.support? %> <% query = searched.present? ? "?search=#{searched}" : nil %> <%= govuk_link_to "Download (CSV)", "#{request.path}.csv#{query}", type: "text/csv" %> diff --git a/spec/components/search_result_caption_component_spec.rb b/spec/components/search_result_caption_component_spec.rb index 2f1a55282..fa136d91b 100644 --- a/spec/components/search_result_caption_component_spec.rb +++ b/spec/components/search_result_caption_component_spec.rb @@ -6,19 +6,53 @@ RSpec.describe SearchResultCaptionComponent, type: :component do let(:item_label) { "user" } let(:total_count) { 3 } let(:item) { "schemes" } + let(:filters_count) { 1 } + let(:result) { render_inline(described_class.new(searched:, count:, item_label:, total_count:, item:, filters_count:)) } - it "renders table caption including the search results and total" do - result = render_inline(described_class.new(searched:, count:, item_label:, total_count:, item:, path:)) - expect(result.to_html).to eq("\n #{count} #{item_label} found matching ‘#{searched}’ of #{total_count} total #{item}.\n") + context "when search and filter results are found" do + it "renders table caption including the search results and total" do + expect(result.to_html).to eq("\n 2 users matching search and filters\n\n") + end + end + + context "when search results are found" do + let(:filters_count) { nil } + + it "renders table caption including the search results and total" do + expect(result.to_html).to eq("\n 2 users matching search\n\n") + end + end + + context "when filter results are found" do + let(:searched) { nil } + + it "renders table caption including the search results and total" do + expect(result.to_html).to eq("\n 2 users matching filters\n\n") + end end - context "when no search results are found" do + context "when no search/filter is applied" do let(:searched) { nil } + let(:filters_count) { nil } it "renders table caption with total count only" do - result = render_inline(described_class.new(searched:, count:, item_label:, total_count:, item:, path:)) + expect(result.to_html).to eq("\n #{count} matching #{item}\n\n") + end + end + + context "when nothing is found" do + let(:count) { 0 } - expect(result.to_html).to eq("\n #{count} total #{item}\n\n") + it "renders table caption with total count only" do + expect(result.to_html).to eq("\n 0 users matching search and filters\n\n") + end + end + + context "when 1 record is found" do + let(:count) { 1 } + + it "renders table caption with total count only" do + expect(result.to_html).to eq("\n 1 user matching search and filters\n\n") end end end diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index b4701517f..ea9debdc1 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -774,7 +774,7 @@ RSpec.describe LettingsLogsController, type: :request do end it "shows the total log count" do - expect(CGI.unescape_html(response.body)).to match("1 total logs") + expect(CGI.unescape_html(response.body)).to match("1 matching logs") end it "does not show the pagination links" do @@ -868,7 +868,7 @@ RSpec.describe LettingsLogsController, type: :request do end it "shows the total log count" do - expect(CGI.unescape_html(response.body)).to match("26 total logs") + expect(CGI.unescape_html(response.body)).to match("26 matching logs") end it "has pagination links" do diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index fd3bd48e5..2570866cf 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -274,7 +274,7 @@ RSpec.describe LocationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 location found matching ‘#{search_param}’") + expect(page).to have_content("1 location matching search") end it "has search in the title" do @@ -402,7 +402,7 @@ RSpec.describe LocationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 location found matching ‘#{search_param}’") + expect(page).to have_content("1 location matching search") end it "has search in the title" do diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 36ee01437..ab4b72136 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -89,7 +89,7 @@ RSpec.describe OrganisationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 scheme found matching ‘#{search_param}’") + expect(page).to have_content("1 scheme matching search") end it "has search in the title" do @@ -171,7 +171,7 @@ RSpec.describe OrganisationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 scheme found matching ‘#{search_param}’") + expect(page).to have_content("1 scheme matching search") end it "has search in the title" do @@ -332,7 +332,7 @@ RSpec.describe OrganisationsController, type: :request do end it "shows the pagination count" do - expect(page).to have_content("#{user.organisation.users.count} total users") + expect(page).to have_content("#{user.organisation.users.count} matching users") end end @@ -716,7 +716,7 @@ RSpec.describe OrganisationsController, type: :request do total_number_of_orgs = Organisation.all.count expect(page).to have_link organisation.name, href: "organisations/#{organisation.id}/lettings-logs" expect(page).to have_link unauthorised_organisation.name, href: "organisations/#{unauthorised_organisation.id}/lettings-logs" - expect(page).to have_content("#{total_number_of_orgs} total organisations") + expect(page).to have_content("#{total_number_of_orgs} matching organisations") end it "shows a search bar" do @@ -745,7 +745,7 @@ RSpec.describe OrganisationsController, type: :request do end it "only shows logs for that organisation" do - expect(page).to have_content("#{total_number_of_org1_logs} total logs") + expect(page).to have_content("#{total_number_of_org1_logs} matching logs") organisation.lettings_logs.visible.map(&:id).each do |lettings_log_id| expect(page).to have_link lettings_log_id.to_s, href: "/lettings-logs/#{lettings_log_id}" @@ -899,7 +899,7 @@ RSpec.describe OrganisationsController, type: :request do end it "only shows logs for that organisation" do - expect(page).to have_content("#{number_of_org1_sales_logs} total logs") + expect(page).to have_content("#{number_of_org1_sales_logs} matching logs") organisation.sales_logs.map(&:id).each do |sales_log_id| expect(page).to have_link sales_log_id.to_s, href: "/sales-logs/#{sales_log_id}" end @@ -1033,7 +1033,7 @@ RSpec.describe OrganisationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.") + expect(page).to have_content("1 user matching search") end context "when we need case insensitive search" do @@ -1053,7 +1053,7 @@ RSpec.describe OrganisationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.") + expect(page).to have_content("1 user matching search") end end end @@ -1075,13 +1075,13 @@ RSpec.describe OrganisationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.") + expect(page).to have_content("1 user matching search") end context "when our search term matches an email and a name" do let!(:matching_user) { create(:user, organisation:, name: "Foobar", email: "some@example.com") } let!(:another_matching_user) { create(:user, organisation:, name: "Joe", email: "foobar@example.com") } - let!(:org_user_count) { User.where(organisation:).count } + let(:org_user_count) { User.where(organisation:).count } let(:search_param) { "Foobar" } before do @@ -1103,7 +1103,7 @@ RSpec.describe OrganisationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("2 users found matching ‘#{search_param}’ of #{org_user_count} total users.") + expect(page).to have_content("2 users matching search") end end end @@ -1160,7 +1160,7 @@ RSpec.describe OrganisationsController, type: :request do end it "shows the total organisations count" do - expect(CGI.unescape_html(response.body)).to match("#{total_organisations_count} total organisations") + expect(CGI.unescape_html(response.body)).to match("#{total_organisations_count} matching organisations") end it "has pagination links" do @@ -1194,7 +1194,7 @@ RSpec.describe OrganisationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 organisations found matching ‘#{search_param}’") + expect(page).to have_content("1 organisations matching search") end it "has search in the title" do @@ -1210,7 +1210,7 @@ RSpec.describe OrganisationsController, type: :request do end it "updates the table caption" do - expect(page).to have_content("2 organisations found matching ‘#{search_param}’") + expect(page).to have_content("2 organisations matching search") end it "has search in the title" do diff --git a/spec/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb index 919f70725..97c5851fd 100644 --- a/spec/requests/sales_logs_controller_spec.rb +++ b/spec/requests/sales_logs_controller_spec.rb @@ -655,7 +655,7 @@ RSpec.describe SalesLogsController, type: :request do end it "shows the total log count" do - expect(CGI.unescape_html(response.body)).to match("1 total logs") + expect(CGI.unescape_html(response.body)).to match("1 matching logs") end it "does not show the pagination links" do @@ -708,7 +708,7 @@ RSpec.describe SalesLogsController, type: :request do end it "shows the total log count" do - expect(CGI.unescape_html(response.body)).to match("26 total logs") + expect(CGI.unescape_html(response.body)).to match("26 matching logs") end it "has pagination links" do diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 33e0dcfd5..9ba00aa43 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -234,7 +234,7 @@ RSpec.describe SchemesController, type: :request do end it "shows the total organisations count" do - expect(CGI.unescape_html(response.body)).to match("#{schemes.count} total schemes") + expect(CGI.unescape_html(response.body)).to match("#{schemes.count} matching schemes") end context "when paginating over 20 results" do @@ -250,7 +250,7 @@ RSpec.describe SchemesController, type: :request do end it "shows the total schemes count" do - expect(CGI.unescape_html(response.body)).to match("#{total_schemes_count} total schemes") + expect(CGI.unescape_html(response.body)).to match("#{total_schemes_count} matching schemes") end it "shows which schemes are being shown on the current page" do @@ -275,7 +275,7 @@ RSpec.describe SchemesController, type: :request do end it "shows the total schemes count" do - expect(CGI.unescape_html(response.body)).to match("#{total_schemes_count} total schemes") + expect(CGI.unescape_html(response.body)).to match("#{total_schemes_count} matching schemes") end it "has pagination links" do @@ -321,7 +321,7 @@ RSpec.describe SchemesController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 scheme found matching ‘#{search_param}’") + expect(page).to have_content("1 scheme matching search") end it "has search in the title" do diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index f35be6b0e..dbdde21b8 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -430,7 +430,7 @@ RSpec.describe UsersController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 user found matching ‘filter’ of 4 total users.") + expect(page).to have_content("1 user matching search") end end @@ -466,7 +466,7 @@ RSpec.describe UsersController, type: :request do end it "updates the table caption" do - expect(page).to have_content("2 users found matching ‘joe’ of 4 total users.") + expect(page).to have_content("2 users matching search") end end end @@ -1136,7 +1136,7 @@ RSpec.describe UsersController, type: :request do end it "shows the pagination count" do - expect(page).to have_content("4 total users") + expect(page).to have_content("4 matching users") end it "shows the download csv link" do @@ -1164,7 +1164,7 @@ RSpec.describe UsersController, type: :request do end it "updates the table caption" do - expect(page).to have_content("1 user found matching ‘#{search_param}’ of 4 total users.") + expect(page).to have_content("1 user matching search") end it "includes the search term in the CSV download link" do @@ -1207,7 +1207,7 @@ RSpec.describe UsersController, type: :request do end it "updates the table caption" do - expect(page).to have_content("2 users found matching ‘joe’ of 4 total users.") + expect(page).to have_content("2 users matching search") end end end