From 12f76c15863405b8dd273b94a6f42987306ad5f7 Mon Sep 17 00:00:00 2001 From: Christian Sutter Date: Fri, 4 Oct 2024 12:29:47 +0000 Subject: [PATCH] Implement `FiltersPresenter#reset_url` This returns the URL used for the "Clear all" link on both the filter summary and filter panel in the new "all content" finder UI. - Add real implementation for `FiltersPresenter#reset_url`, merging the query parameters for every applied facet and then removing them from the current URL - Refactor duplication into a `#applied_filters` private method --- app/presenters/filters_presenter.rb | 22 +++++++-- spec/presenters/filters_presenter_spec.rb | 54 ++++++++++++++++++++--- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/app/presenters/filters_presenter.rb b/app/presenters/filters_presenter.rb index 6be7fdd22..90e952645 100644 --- a/app/presenters/filters_presenter.rb +++ b/app/presenters/filters_presenter.rb @@ -5,11 +5,11 @@ def initialize(facets, finder_url_builder) end def any_filters? - facets.any?(&:has_filters?) + applied_filters.any? end def summary_items - facets.flat_map(&:applied_filters).map do |filter| + applied_filters.map do |filter| { label: filter[:name], value: filter[:label], @@ -20,10 +20,26 @@ def summary_items end def reset_url - "#" + return nil unless any_filters? + + all_query_params = applied_filters.map { _1[:query_params] } + merged_facet_params = all_query_params.reduce do |acc, query_params| + acc.deep_merge(query_params) do |_key, old_val, new_val| + if old_val.is_a?(Array) + old_val + new_val + else + new_val + end + end + end + finder_url_builder.url_except(merged_facet_params) end private attr_reader :facets, :finder_url_builder + + def applied_filters + @applied_filters ||= facets.flat_map(&:applied_filters) + end end diff --git a/spec/presenters/filters_presenter_spec.rb b/spec/presenters/filters_presenter_spec.rb index f8385443e..e4d21cb2c 100644 --- a/spec/presenters/filters_presenter_spec.rb +++ b/spec/presenters/filters_presenter_spec.rb @@ -7,9 +7,27 @@ let(:finder_url_builder) { instance_double(UrlBuilder) } let(:facet_without_applied_filters) { double("Facet", has_filters?: false, applied_filters: []) } - let(:facet_with_applied_filters) { double("Facet", has_filters?: true, applied_filters:) } - - let(:applied_filters) { [{ name: "name", label: "label", query_params: { key: %w[value] } }] } + let(:facet_with_applied_filters) do + double( + "Facet", + has_filters?: true, + applied_filters: [{ name: "name", label: "label", query_params: { key: %w[value] } }], + ) + end + let(:another_facet_with_applied_filters) do + double( + "Facet", + has_filters?: true, + applied_filters: [ + { name: "name1", label: "label1", query_params: { key1: %w[value1] } }, + { name: "name2", + label: "label2", + query_params: { + key1: %w[anothervalue1], key2: { value2: "subvalue" } + } }, + ], + ) + end describe "#any_filters" do context "when there are no facets" do @@ -32,8 +50,34 @@ end describe "#reset_url" do - it "returns a static anchor link" do - expect(subject.reset_url).to eq("#") + subject(:reset_url) { filters_presenter.reset_url } + + context "when there are no facets" do + let(:facets) { [] } + + it { is_expected.to be_nil } + end + + context "when there are only facets without applied filters" do + let(:facets) { [facet_without_applied_filters, facet_without_applied_filters] } + + it { is_expected.to be_nil } + end + + context "when there are facets with applied filters" do + let(:facets) { [facet_with_applied_filters, another_facet_with_applied_filters] } + + before do + allow(finder_url_builder).to receive(:url_except).with({ + key: %w[value], + key1: %w[value1 anothervalue1], + key2: { value2: "subvalue" }, + }).and_return("/search/foo") + end + + it "returns the expected reset URL" do + expect(reset_url).to eq("/search/foo") + end end end