From 5f3279b9dba5fb86101436a5d1b504047b73a20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 27 Jan 2021 13:17:59 +0100 Subject: [PATCH 1/4] Extract search form component in SDG Management We're going to add some JavaScript which affect this component, and IMHO it will be easier to know the JavaScript affects this form if both have their own separate file. --- .../relations/index_component.html.erb | 11 +----- .../relations/index_component.rb | 25 ------------- .../relations/search_component.html.erb | 9 +++++ .../relations/search_component.rb | 35 +++++++++++++++++++ .../relations/index_component_spec.rb | 13 +++---- .../relations/search_component_spec.rb | 16 +++++++++ 6 files changed, 65 insertions(+), 44 deletions(-) create mode 100644 app/components/sdg_management/relations/search_component.html.erb create mode 100644 app/components/sdg_management/relations/search_component.rb create mode 100644 spec/components/sdg_management/relations/search_component_spec.rb diff --git a/app/components/sdg_management/relations/index_component.html.erb b/app/components/sdg_management/relations/index_component.html.erb index 484d49ce9..4dfd0cc5a 100644 --- a/app/components/sdg_management/relations/index_component.html.erb +++ b/app/components/sdg_management/relations/index_component.html.erb @@ -1,15 +1,6 @@ <%= header %> -<%= render Admin::SearchComponent.new(label: search_label, class: "complex") do |component| %> - <%= component.select_tag :goal_code, goal_options, - include_blank: goal_blank_option, - "aria-label": goal_label %> - <%= component.select_tag :target_code, target_options, - include_blank: target_blank_option, - "aria-label": target_label %> - <%= component.hidden_field_tag :filter, current_filter %> -<% end %> - +<%= render SDGManagement::Relations::SearchComponent.new(label: search_label, current_filter: current_filter) %> <%= render "shared/filter_subnav", i18n_namespace: "sdg_management.relations.index" %> diff --git a/app/components/sdg_management/relations/index_component.rb b/app/components/sdg_management/relations/index_component.rb index ee55fd7a7..0db934e13 100644 --- a/app/components/sdg_management/relations/index_component.rb +++ b/app/components/sdg_management/relations/index_component.rb @@ -1,6 +1,5 @@ class SDGManagement::Relations::IndexComponent < ApplicationComponent include Header - include SDG::OptionsForSelect delegate :valid_filters, :current_filter, to: :helpers attr_reader :records @@ -31,28 +30,4 @@ class SDGManagement::Relations::IndexComponent < ApplicationComponent def search_label t("admin.shared.search.label.#{model_class.table_name}") end - - def goal_label - t("admin.shared.search.advanced_filters.sdg_goals.label") - end - - def goal_blank_option - t("admin.shared.search.advanced_filters.sdg_goals.all") - end - - def target_label - t("admin.shared.search.advanced_filters.sdg_targets.label") - end - - def target_blank_option - t("admin.shared.search.advanced_filters.sdg_targets.all") - end - - def goal_options - super(params[:goal_code]) - end - - def target_options - super(params[:target_code]) - end end diff --git a/app/components/sdg_management/relations/search_component.html.erb b/app/components/sdg_management/relations/search_component.html.erb new file mode 100644 index 000000000..b4a61be21 --- /dev/null +++ b/app/components/sdg_management/relations/search_component.html.erb @@ -0,0 +1,9 @@ +<%= render Admin::SearchComponent.new(label: label, class: "complex") do |component| %> + <%= component.select_tag :goal_code, goal_options, + include_blank: goal_blank_option, + "aria-label": goal_label %> + <%= component.select_tag :target_code, target_options, + include_blank: target_blank_option, + "aria-label": target_label %> + <%= component.hidden_field_tag :filter, current_filter %> +<% end %> diff --git a/app/components/sdg_management/relations/search_component.rb b/app/components/sdg_management/relations/search_component.rb new file mode 100644 index 000000000..fdbe9c833 --- /dev/null +++ b/app/components/sdg_management/relations/search_component.rb @@ -0,0 +1,35 @@ +class SDGManagement::Relations::SearchComponent < ApplicationComponent + include SDG::OptionsForSelect + attr_reader :label, :current_filter + + def initialize(label:, current_filter: nil) + @label = label + @current_filter = current_filter + end + + private + + def goal_label + t("admin.shared.search.advanced_filters.sdg_goals.label") + end + + def goal_blank_option + t("admin.shared.search.advanced_filters.sdg_goals.all") + end + + def target_label + t("admin.shared.search.advanced_filters.sdg_targets.label") + end + + def target_blank_option + t("admin.shared.search.advanced_filters.sdg_targets.all") + end + + def goal_options + super(params[:goal_code]) + end + + def target_options + super(params[:target_code]) + end +end diff --git a/spec/components/sdg_management/relations/index_component_spec.rb b/spec/components/sdg_management/relations/index_component_spec.rb index dbe5be7e4..e3de6d5c4 100644 --- a/spec/components/sdg_management/relations/index_component_spec.rb +++ b/spec/components/sdg_management/relations/index_component_spec.rb @@ -11,16 +11,11 @@ describe SDGManagement::Relations::IndexComponent, type: :component do .and_return("/anything") end - describe "#goal_options" do - it "orders goals by code in the select" do - component = SDGManagement::Relations::IndexComponent.new(Proposal.none.page(1)) + it "renders the search form" do + component = SDGManagement::Relations::IndexComponent.new(Proposal.none.page(1)) - render_inline component - options = page.find("#goal_code").all("option") + render_inline component - expect(options[0]).to have_content "All goals" - expect(options[1]).to have_content "1. " - expect(options[17]).to have_content "17. " - end + expect(page).to have_css "form.complex" end end diff --git a/spec/components/sdg_management/relations/search_component_spec.rb b/spec/components/sdg_management/relations/search_component_spec.rb new file mode 100644 index 000000000..d3fd93c0f --- /dev/null +++ b/spec/components/sdg_management/relations/search_component_spec.rb @@ -0,0 +1,16 @@ +require "rails_helper" + +describe SDGManagement::Relations::SearchComponent, type: :component do + describe "#goal_options" do + it "orders goals by code in the select" do + component = SDGManagement::Relations::SearchComponent.new(label: "Search proposals") + + render_inline component + options = page.find("#goal_code").all("option") + + expect(options[0]).to have_content "All goals" + expect(options[1]).to have_content "1. " + expect(options[17]).to have_content "17. " + end + end +end From a6714be6b8567af166a0aefe95ba3f6180b3ccf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 28 Jan 2021 13:31:53 +0100 Subject: [PATCH 2/4] Group advanced search tests together We were doing the same tests three times to test the advanced search feature. I'm grouping them in one place and shuffling the sections around to remove duplication and make the test suite faster. --- spec/system/advanced_search_spec.rb | 391 ++++++++++++++++++++++++ spec/system/budgets/investments_spec.rb | 349 --------------------- spec/system/debates_spec.rb | 344 --------------------- spec/system/proposals_spec.rb | 349 --------------------- 4 files changed, 391 insertions(+), 1042 deletions(-) create mode 100644 spec/system/advanced_search_spec.rb diff --git a/spec/system/advanced_search_spec.rb b/spec/system/advanced_search_spec.rb new file mode 100644 index 000000000..ee0e7d5cf --- /dev/null +++ b/spec/system/advanced_search_spec.rb @@ -0,0 +1,391 @@ +require "rails_helper" + +describe "Advanced search", :js do + let(:budget) { create(:budget, name: "Big Budget") } + let(:heading) { create(:budget_heading, budget: budget, name: "More hospitals") } + + scenario "Search debates" do + debate1 = create(:debate, title: "Get Schwifty") + debate2 = create(:debate, title: "Schwifty Hello") + debate3 = create(:debate, title: "Do not show me") + + visit debates_path + + click_link "Advanced search" + fill_in "Write the text", with: "Schwifty" + click_button "Filter" + + expect(page).to have_content("There are 2 debates") + + within("#debates") do + expect(page).to have_content(debate1.title) + expect(page).to have_content(debate2.title) + expect(page).not_to have_content(debate3.title) + end + end + + scenario "Search proposals" do + proposal1 = create(:proposal, title: "Get Schwifty") + proposal2 = create(:proposal, title: "Schwifty Hello") + proposal3 = create(:proposal, title: "Do not show me") + + visit proposals_path + + click_link "Advanced search" + fill_in "Write the text", with: "Schwifty" + click_button "Filter" + + expect(page).to have_content("There are 2 citizen proposals") + + within("#proposals") do + expect(page).to have_content(proposal1.title) + expect(page).to have_content(proposal2.title) + expect(page).not_to have_content(proposal3.title) + end + end + + scenario "Search investments" do + bdgt_invest1 = create(:budget_investment, heading: heading, title: "Get Schwifty") + bdgt_invest2 = create(:budget_investment, heading: heading, title: "Schwifty Hello") + bdgt_invest3 = create(:budget_investment, heading: heading, title: "Do not show me") + + visit budget_investments_path(budget) + + click_link "Advanced search" + fill_in "Write the text", with: "Schwifty" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + context "Search by author type" do + scenario "Public employee" do + ana = create :user, official_level: 1 + john = create :user, official_level: 2 + + debate1 = create(:debate, author: ana) + debate2 = create(:debate, author: ana) + debate3 = create(:debate, author: john) + + visit debates_path + + click_link "Advanced search" + select Setting["official_level_1_name"], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 debates") + + within("#debates") do + expect(page).to have_content(debate1.title) + expect(page).to have_content(debate2.title) + expect(page).not_to have_content(debate3.title) + end + end + + scenario "Municipal Organization" do + ana = create :user, official_level: 2 + john = create :user, official_level: 3 + + proposal1 = create(:proposal, author: ana) + proposal2 = create(:proposal, author: ana) + proposal3 = create(:proposal, author: john) + + visit proposals_path + + click_link "Advanced search" + select Setting["official_level_2_name"], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 citizen proposals") + + within("#proposals") do + expect(page).to have_content(proposal1.title) + expect(page).to have_content(proposal2.title) + expect(page).not_to have_content(proposal3.title) + end + end + + scenario "General director" do + ana = create :user, official_level: 3 + john = create :user, official_level: 4 + + bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest3 = create(:budget_investment, heading: heading, author: john) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select Setting["official_level_3_name"], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "City councillor" do + ana = create :user, official_level: 4 + john = create :user, official_level: 5 + + debate1 = create(:debate, author: ana) + debate2 = create(:debate, author: ana) + debate3 = create(:debate, author: john) + + visit debates_path + + click_link "Advanced search" + select Setting["official_level_4_name"], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 debates") + + within("#debates") do + expect(page).to have_content(debate1.title) + expect(page).to have_content(debate2.title) + expect(page).not_to have_content(debate3.title) + end + end + + scenario "Mayoress" do + ana = create :user, official_level: 5 + john = create :user, official_level: 4 + + proposal1 = create(:proposal, author: ana) + proposal2 = create(:proposal, author: ana) + proposal3 = create(:proposal, author: john) + + visit proposals_path + + click_link "Advanced search" + select Setting["official_level_5_name"], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 citizen proposals") + + within("#proposals") do + expect(page).to have_content(proposal1.title) + expect(page).to have_content(proposal2.title) + expect(page).not_to have_content(proposal3.title) + end + end + + context "Search by date" do + context "Predefined date ranges" do + scenario "Last day" do + bdgt_invest1 = create(:budget_investment, heading: heading, created_at: 1.minute.ago) + bdgt_invest2 = create(:budget_investment, heading: heading, created_at: 1.hour.ago) + bdgt_invest3 = create(:budget_investment, heading: heading, created_at: 2.days.ago) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select "Last 24 hours", from: "js-advanced-search-date-min" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "Last week" do + debate1 = create(:debate, created_at: 1.day.ago) + debate2 = create(:debate, created_at: 5.days.ago) + debate3 = create(:debate, created_at: 8.days.ago) + + visit debates_path + + click_link "Advanced search" + select "Last week", from: "js-advanced-search-date-min" + click_button "Filter" + + within("#debates") do + expect(page).to have_css(".debate", count: 2) + + expect(page).to have_content(debate1.title) + expect(page).to have_content(debate2.title) + expect(page).not_to have_content(debate3.title) + end + end + + scenario "Last month" do + proposal1 = create(:proposal, created_at: 10.days.ago) + proposal2 = create(:proposal, created_at: 20.days.ago) + proposal3 = create(:proposal, created_at: 33.days.ago) + + visit proposals_path + + click_link "Advanced search" + select "Last month", from: "js-advanced-search-date-min" + click_button "Filter" + + expect(page).to have_content("There are 2 citizen proposals") + + within("#proposals") do + expect(page).to have_content(proposal1.title) + expect(page).to have_content(proposal2.title) + expect(page).not_to have_content(proposal3.title) + end + end + + scenario "Last year" do + bdgt_invest1 = create(:budget_investment, heading: heading, created_at: 300.days.ago) + bdgt_invest2 = create(:budget_investment, heading: heading, created_at: 350.days.ago) + bdgt_invest3 = create(:budget_investment, heading: heading, created_at: 370.days.ago) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select "Last year", from: "js-advanced-search-date-min" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + end + + scenario "Search by custom date range" do + debate1 = create(:debate, created_at: 2.days.ago) + debate2 = create(:debate, created_at: 3.days.ago) + debate3 = create(:debate, created_at: 9.days.ago) + + visit debates_path + + click_link "Advanced search" + select "Customized", from: "js-advanced-search-date-min" + fill_in "advanced_search_date_min", with: 7.days.ago + fill_in "advanced_search_date_max", with: 1.day.ago + click_button "Filter" + + within("#debates") do + expect(page).to have_css(".debate", count: 2) + + expect(page).to have_content(debate1.title) + expect(page).to have_content(debate2.title) + expect(page).not_to have_content(debate3.title) + end + end + + scenario "Search by custom invalid date range" do + proposal1 = create(:proposal, created_at: 2.days.ago) + proposal2 = create(:proposal, created_at: 3.days.ago) + proposal3 = create(:proposal, created_at: 9.days.ago) + + visit proposals_path + + click_link "Advanced search" + select "Customized", from: "js-advanced-search-date-min" + fill_in "advanced_search_date_min", with: 4000.years.ago + fill_in "advanced_search_date_max", with: "wrong date" + click_button "Filter" + + expect(page).to have_content("There are 3 citizen proposals") + + within("#proposals") do + expect(page).to have_content(proposal1.title) + expect(page).to have_content(proposal2.title) + expect(page).to have_content(proposal3.title) + end + end + + scenario "Search by multiple filters" do + ana = create :user, official_level: 1 + john = create :user, official_level: 1 + + create(:budget_investment, heading: heading, title: "Get Schwifty", author: ana, created_at: 1.minute.ago) + create(:budget_investment, heading: heading, title: "Hello Schwifty", author: john, created_at: 2.days.ago) + create(:budget_investment, heading: heading, title: "Save the forest") + + visit budget_investments_path(budget) + + click_link "Advanced search" + fill_in "Write the text", with: "Schwifty" + select Setting["official_level_1_name"], from: "advanced_search_official_level" + select "Last 24 hours", from: "js-advanced-search-date-min" + + click_button "Filter" + + expect(page).to have_content("There is 1 investment") + + within("#budget-investments") do + expect(page).to have_content "Get Schwifty" + end + end + + scenario "Maintain advanced search criteria" do + visit debates_path + click_link "Advanced search" + + fill_in "Write the text", with: "Schwifty" + select Setting["official_level_1_name"], from: "advanced_search_official_level" + select "Last 24 hours", from: "js-advanced-search-date-min" + + click_button "Filter" + + within "#js-advanced-search" do + expect(page).to have_selector("input[name='search'][value='Schwifty']") + expect(page).to have_select("advanced_search[official_level]", selected: Setting["official_level_1_name"]) + expect(page).to have_select("advanced_search[date_min]", selected: "Last 24 hours") + end + end + + scenario "Maintain custom date search criteria" do + visit proposals_path + click_link "Advanced search" + + select "Customized", from: "js-advanced-search-date-min" + fill_in "advanced_search_date_min", with: 7.days.ago.strftime("%d/%m/%Y") + fill_in "advanced_search_date_max", with: 1.day.ago.strftime("%d/%m/%Y") + click_button "Filter" + + expect(page).to have_content("citizen proposals cannot be found") + + within "#js-advanced-search" do + expect(page).to have_select("advanced_search[date_min]", selected: "Customized") + expect(page).to have_selector("input[name='advanced_search[date_min]'][value*='#{7.days.ago.strftime("%d/%m/%Y")}']") + expect(page).to have_selector("input[name='advanced_search[date_max]'][value*='#{1.day.ago.strftime("%d/%m/%Y")}']") + end + end + end + + scenario "Search by SDG target" do + Setting["feature.sdg"] = true + Setting["sdg.process.debates"] = true + create(:debate, title: "Unrelated") + create(:debate, title: "High school", sdg_targets: [SDG::Target["4.1"]]) + create(:debate, title: "Preschool", sdg_targets: [SDG::Target["4.2"]]) + + visit debates_path + click_link "Advanced search" + select "4.2", from: "By target" + click_button "Filter" + + expect(page).to have_content("There is 1 debate") + + within("#debates") do + expect(page).to have_content("Preschool") + expect(page).not_to have_content("High school") + expect(page).not_to have_content("Unrelated") + end + end + end +end diff --git a/spec/system/budgets/investments_spec.rb b/spec/system/budgets/investments_spec.rb index be79d04c0..11a7707fd 100644 --- a/spec/system/budgets/investments_spec.rb +++ b/spec/system/budgets/investments_spec.rb @@ -172,355 +172,6 @@ describe "Budget Investments" do expect(page).not_to have_content(investment3.title) end end - - context "Advanced search" do - scenario "Search by text", :js do - bdgt_invest1 = create(:budget_investment, heading: heading, title: "Get Schwifty") - bdgt_invest2 = create(:budget_investment, heading: heading, title: "Schwifty Hello") - bdgt_invest3 = create(:budget_investment, heading: heading, title: "Do not show me") - - visit budget_investments_path(budget) - - click_link "Advanced search" - fill_in "Write the text", with: "Schwifty" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - context "Search by author type" do - scenario "Public employee", :js do - ana = create :user, official_level: 1 - john = create :user, official_level: 2 - - bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest3 = create(:budget_investment, heading: heading, author: john) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - scenario "Municipal Organization", :js do - ana = create :user, official_level: 2 - john = create :user, official_level: 3 - - bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest3 = create(:budget_investment, heading: heading, author: john) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select Setting["official_level_2_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - scenario "General director", :js do - ana = create :user, official_level: 3 - john = create :user, official_level: 4 - - bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest3 = create(:budget_investment, heading: heading, author: john) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select Setting["official_level_3_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - scenario "City councillor", :js do - ana = create :user, official_level: 4 - john = create :user, official_level: 5 - - bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest3 = create(:budget_investment, heading: heading, author: john) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select Setting["official_level_4_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - scenario "Mayoress", :js do - ana = create :user, official_level: 5 - john = create :user, official_level: 4 - - bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) - bdgt_invest3 = create(:budget_investment, heading: heading, author: john) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select Setting["official_level_5_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - end - - context "Search by date" do - context "Predefined date ranges" do - scenario "Last day", :js do - bdgt_invest1 = create(:budget_investment, heading: heading, created_at: 1.minute.ago) - bdgt_invest2 = create(:budget_investment, heading: heading, created_at: 1.hour.ago) - bdgt_invest3 = create(:budget_investment, heading: heading, created_at: 2.days.ago) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select "Last 24 hours", from: "js-advanced-search-date-min" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - scenario "Last week", :js do - bdgt_invest1 = create(:budget_investment, heading: heading, created_at: 1.day.ago) - bdgt_invest2 = create(:budget_investment, heading: heading, created_at: 5.days.ago) - bdgt_invest3 = create(:budget_investment, heading: heading, created_at: 8.days.ago) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select "Last week", from: "js-advanced-search-date-min" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - scenario "Last month", :js do - bdgt_invest1 = create(:budget_investment, heading: heading, created_at: 10.days.ago) - bdgt_invest2 = create(:budget_investment, heading: heading, created_at: 20.days.ago) - bdgt_invest3 = create(:budget_investment, heading: heading, created_at: 33.days.ago) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select "Last month", from: "js-advanced-search-date-min" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - scenario "Last year", :js do - bdgt_invest1 = create(:budget_investment, heading: heading, created_at: 300.days.ago) - bdgt_invest2 = create(:budget_investment, heading: heading, created_at: 350.days.ago) - bdgt_invest3 = create(:budget_investment, heading: heading, created_at: 370.days.ago) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select "Last year", from: "js-advanced-search-date-min" - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - end - - scenario "Search by custom date range", :js do - bdgt_invest1 = create(:budget_investment, heading: heading, created_at: 2.days.ago) - bdgt_invest2 = create(:budget_investment, heading: heading, created_at: 3.days.ago) - bdgt_invest3 = create(:budget_investment, heading: heading, created_at: 9.days.ago) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: 7.days.ago - fill_in "advanced_search_date_max", with: 1.day.ago - click_button "Filter" - - expect(page).to have_content("There are 2 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).not_to have_content(bdgt_invest3.title) - end - end - - scenario "Search by custom invalid date range", :js do - bdgt_invest1 = create(:budget_investment, heading: heading, created_at: 2.days.ago) - bdgt_invest2 = create(:budget_investment, heading: heading, created_at: 3.days.ago) - bdgt_invest3 = create(:budget_investment, heading: heading, created_at: 9.days.ago) - - visit budget_investments_path(budget) - - click_link "Advanced search" - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: 4000.years.ago - fill_in "advanced_search_date_max", with: "wrong date" - click_button "Filter" - - expect(page).to have_content("There are 3 investments") - - within("#budget-investments") do - expect(page).to have_content(bdgt_invest1.title) - expect(page).to have_content(bdgt_invest2.title) - expect(page).to have_content(bdgt_invest3.title) - end - end - - scenario "Search by multiple filters", :js do - ana = create :user, official_level: 1 - john = create :user, official_level: 1 - - create(:budget_investment, heading: heading, title: "Get Schwifty", author: ana, created_at: 1.minute.ago) - create(:budget_investment, heading: heading, title: "Hello Schwifty", author: john, created_at: 2.days.ago) - create(:budget_investment, heading: heading, title: "Save the forest") - - visit budget_investments_path(budget) - - click_link "Advanced search" - fill_in "Write the text", with: "Schwifty" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - select "Last 24 hours", from: "js-advanced-search-date-min" - - click_button "Filter" - - expect(page).to have_content("There is 1 investment") - - within("#budget-investments") do - expect(page).to have_content "Get Schwifty" - end - end - - scenario "Maintain advanced search criteria", :js do - visit budget_investments_path(budget) - click_link "Advanced search" - - fill_in "Write the text", with: "Schwifty" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - select "Last 24 hours", from: "js-advanced-search-date-min" - - click_button "Filter" - - expect(page).to have_content("investments cannot be found") - - within "#js-advanced-search" do - expect(page).to have_selector("input[name='search'][value='Schwifty']") - expect(page).to have_select("advanced_search[official_level]", selected: Setting["official_level_1_name"]) - expect(page).to have_select("advanced_search[date_min]", selected: "Last 24 hours") - end - end - - scenario "Maintain custom date search criteria", :js do - visit budget_investments_path(budget) - click_link "Advanced search" - - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: 7.days.ago.strftime("%d/%m/%Y") - fill_in "advanced_search_date_max", with: 1.day.ago.strftime("%d/%m/%Y") - click_button "Filter" - - expect(page).to have_content("investments cannot be found") - - within "#js-advanced-search" do - expect(page).to have_select("advanced_search[date_min]", selected: "Customized") - expect(page).to have_selector("input[name='advanced_search[date_min]'][value*='#{7.days.ago.strftime("%d/%m/%Y")}']") - expect(page).to have_selector("input[name='advanced_search[date_max]'][value*='#{1.day.ago.strftime("%d/%m/%Y")}']") - end - end - end - end - - scenario "Search by SDG target", :js do - Setting["feature.sdg"] = true - Setting["sdg.process.budgets"] = true - create(:budget_investment, heading: heading, title: "Unrelated") - create(:budget_investment, heading: heading, title: "High school", sdg_targets: [SDG::Target["4.1"]]) - create(:budget_investment, heading: heading, title: "Preschool", sdg_targets: [SDG::Target["4.2"]]) - - visit budget_investments_path(budget) - click_link "Advanced search" - select "4.2", from: "By target" - click_button "Filter" - - expect(page).to have_content("There is 1 investment") - - within("#budget-investments") do - expect(page).to have_content("Preschool") - expect(page).not_to have_content("High school") - expect(page).not_to have_content("Unrelated") - end - end end context("Filters") do diff --git a/spec/system/debates_spec.rb b/spec/system/debates_spec.rb index d2b656139..6e052681e 100644 --- a/spec/system/debates_spec.rb +++ b/spec/system/debates_spec.rb @@ -545,350 +545,6 @@ describe "Debates" do end end - context "Advanced search" do - scenario "Search by text", :js do - debate1 = create(:debate, title: "Get Schwifty") - debate2 = create(:debate, title: "Schwifty Hello") - debate3 = create(:debate, title: "Do not show me") - - visit debates_path - - click_link "Advanced search" - fill_in "Write the text", with: "Schwifty" - click_button "Filter" - - expect(page).to have_content("There are 2 debates") - - within("#debates") do - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - context "Search by author type" do - scenario "Public employee", :js do - ana = create :user, official_level: 1 - john = create :user, official_level: 2 - - debate1 = create(:debate, author: ana) - debate2 = create(:debate, author: ana) - debate3 = create(:debate, author: john) - - visit debates_path - - click_link "Advanced search" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 debates") - - within("#debates") do - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - scenario "Municipal Organization", :js do - ana = create :user, official_level: 2 - john = create :user, official_level: 3 - - debate1 = create(:debate, author: ana) - debate2 = create(:debate, author: ana) - debate3 = create(:debate, author: john) - - visit debates_path - - click_link "Advanced search" - select Setting["official_level_2_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 debates") - - within("#debates") do - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - scenario "General director", :js do - ana = create :user, official_level: 3 - john = create :user, official_level: 4 - - debate1 = create(:debate, author: ana) - debate2 = create(:debate, author: ana) - debate3 = create(:debate, author: john) - - visit debates_path - - click_link "Advanced search" - select Setting["official_level_3_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 debates") - - within("#debates") do - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - scenario "City councillor", :js do - ana = create :user, official_level: 4 - john = create :user, official_level: 5 - - debate1 = create(:debate, author: ana) - debate2 = create(:debate, author: ana) - debate3 = create(:debate, author: john) - - visit debates_path - - click_link "Advanced search" - select Setting["official_level_4_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 debates") - - within("#debates") do - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - scenario "Mayoress", :js do - ana = create :user, official_level: 5 - john = create :user, official_level: 4 - - debate1 = create(:debate, author: ana) - debate2 = create(:debate, author: ana) - debate3 = create(:debate, author: john) - - visit debates_path - - click_link "Advanced search" - select Setting["official_level_5_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 debates") - - within("#debates") do - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - end - - context "Search by date" do - context "Predefined date ranges" do - scenario "Last day", :js do - debate1 = create(:debate, created_at: 1.minute.ago) - debate2 = create(:debate, created_at: 1.hour.ago) - debate3 = create(:debate, created_at: 2.days.ago) - - visit debates_path - - click_link "Advanced search" - select "Last 24 hours", from: "js-advanced-search-date-min" - click_button "Filter" - - within("#debates") do - expect(page).to have_css(".debate", count: 2) - - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - scenario "Last week", :js do - debate1 = create(:debate, created_at: 1.day.ago) - debate2 = create(:debate, created_at: 5.days.ago) - debate3 = create(:debate, created_at: 8.days.ago) - - visit debates_path - - click_link "Advanced search" - select "Last week", from: "js-advanced-search-date-min" - click_button "Filter" - - within("#debates") do - expect(page).to have_css(".debate", count: 2) - - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - scenario "Last month", :js do - debate1 = create(:debate, created_at: 10.days.ago) - debate2 = create(:debate, created_at: 20.days.ago) - debate3 = create(:debate, created_at: 33.days.ago) - - visit debates_path - - click_link "Advanced search" - select "Last month", from: "js-advanced-search-date-min" - click_button "Filter" - - within("#debates") do - expect(page).to have_css(".debate", count: 2) - - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - scenario "Last year", :js do - debate1 = create(:debate, created_at: 300.days.ago) - debate2 = create(:debate, created_at: 350.days.ago) - debate3 = create(:debate, created_at: 370.days.ago) - - visit debates_path - - click_link "Advanced search" - select "Last year", from: "js-advanced-search-date-min" - click_button "Filter" - - within("#debates") do - expect(page).to have_css(".debate", count: 2) - - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - end - - scenario "Search by custom date range", :js do - debate1 = create(:debate, created_at: 2.days.ago) - debate2 = create(:debate, created_at: 3.days.ago) - debate3 = create(:debate, created_at: 9.days.ago) - - visit debates_path - - click_link "Advanced search" - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: 7.days.ago - fill_in "advanced_search_date_max", with: 1.day.ago - click_button "Filter" - - within("#debates") do - expect(page).to have_css(".debate", count: 2) - - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).not_to have_content(debate3.title) - end - end - - scenario "Search by custom invalid date range", :js do - debate1 = create(:debate, created_at: 2.years.ago) - debate2 = create(:debate, created_at: 3.days.ago) - debate3 = create(:debate, created_at: 9.days.ago) - - visit debates_path - - click_link "Advanced search" - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: "9" - fill_in "advanced_search_date_max", with: "444444444" - click_button "Filter" - - within("#debates") do - expect(page).to have_css(".debate", count: 3) - - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) - expect(page).to have_content(debate3.title) - end - end - - scenario "Search by multiple filters", :js do - ana = create :user, official_level: 1 - john = create :user, official_level: 1 - - create(:debate, title: "Get Schwifty", author: ana, created_at: 1.minute.ago) - create(:debate, title: "Hello Schwifty", author: john, created_at: 2.days.ago) - create(:debate, title: "Save the forest") - - visit debates_path - - click_link "Advanced search" - fill_in "Write the text", with: "Schwifty" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - select "Last 24 hours", from: "js-advanced-search-date-min" - - click_button "Filter" - - within("#debates") do - expect(page).to have_css(".debate", count: 1) - expect(page).to have_content "Get Schwifty" - end - end - - scenario "Maintain advanced search criteria", :js do - visit debates_path - click_link "Advanced search" - - fill_in "Write the text", with: "Schwifty" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - select "Last 24 hours", from: "js-advanced-search-date-min" - - click_button "Filter" - - within "#js-advanced-search" do - expect(page).to have_selector("input[name='search'][value='Schwifty']") - expect(page).to have_select("advanced_search[official_level]", selected: Setting["official_level_1_name"]) - expect(page).to have_select("advanced_search[date_min]", selected: "Last 24 hours") - end - end - - scenario "Maintain custom date search criteria", :js do - visit debates_path - click_link "Advanced search" - - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: 7.days.ago.strftime("%d/%m/%Y") - fill_in "advanced_search_date_max", with: 1.day.ago.strftime("%d/%m/%Y") - click_button "Filter" - - within "#js-advanced-search" do - expect(page).to have_select("advanced_search[date_min]", selected: "Customized") - expect(page).to have_selector("input[name='advanced_search[date_min]'][value*='#{7.days.ago.strftime("%d/%m/%Y")}']") - expect(page).to have_selector("input[name='advanced_search[date_max]'][value*='#{1.day.ago.strftime("%d/%m/%Y")}']") - end - end - end - - scenario "Search by SDG target", :js do - Setting["feature.sdg"] = true - Setting["sdg.process.debates"] = true - create(:debate, title: "Unrelated") - create(:debate, title: "High school", sdg_targets: [SDG::Target["4.1"]]) - create(:debate, title: "Preschool", sdg_targets: [SDG::Target["4.2"]]) - - visit debates_path - click_link "Advanced search" - select "4.2", from: "By target" - click_button "Filter" - - expect(page).to have_content("There is 1 debate") - - within("#debates") do - expect(page).to have_content("Preschool") - expect(page).not_to have_content("High school") - expect(page).not_to have_content("Unrelated") - end - end - end - scenario "Order by relevance by default", :js do create(:debate, title: "Show you got", cached_votes_up: 10) create(:debate, title: "Show what you got", cached_votes_up: 1) diff --git a/spec/system/proposals_spec.rb b/spec/system/proposals_spec.rb index 3cae87a62..31f2d13a5 100644 --- a/spec/system/proposals_spec.rb +++ b/spec/system/proposals_spec.rb @@ -1156,355 +1156,6 @@ describe "Proposals" do end end - context "Advanced search" do - scenario "Search by text", :js do - proposal1 = create(:proposal, title: "Get Schwifty") - proposal2 = create(:proposal, title: "Schwifty Hello") - proposal3 = create(:proposal, title: "Do not show me") - - visit proposals_path - - click_link "Advanced search" - fill_in "Write the text", with: "Schwifty" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - context "Search by author type" do - scenario "Public employee", :js do - ana = create :user, official_level: 1 - john = create :user, official_level: 2 - - proposal1 = create(:proposal, author: ana) - proposal2 = create(:proposal, author: ana) - proposal3 = create(:proposal, author: john) - - visit proposals_path - - click_link "Advanced search" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - scenario "Municipal Organization", :js do - ana = create :user, official_level: 2 - john = create :user, official_level: 3 - - proposal1 = create(:proposal, author: ana) - proposal2 = create(:proposal, author: ana) - proposal3 = create(:proposal, author: john) - - visit proposals_path - - click_link "Advanced search" - select Setting["official_level_2_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - scenario "General director", :js do - ana = create :user, official_level: 3 - john = create :user, official_level: 4 - - proposal1 = create(:proposal, author: ana) - proposal2 = create(:proposal, author: ana) - proposal3 = create(:proposal, author: john) - - visit proposals_path - - click_link "Advanced search" - select Setting["official_level_3_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - scenario "City councillor", :js do - ana = create :user, official_level: 4 - john = create :user, official_level: 5 - - proposal1 = create(:proposal, author: ana) - proposal2 = create(:proposal, author: ana) - proposal3 = create(:proposal, author: john) - - visit proposals_path - - click_link "Advanced search" - select Setting["official_level_4_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - scenario "Mayoress", :js do - ana = create :user, official_level: 5 - john = create :user, official_level: 4 - - proposal1 = create(:proposal, author: ana) - proposal2 = create(:proposal, author: ana) - proposal3 = create(:proposal, author: john) - - visit proposals_path - - click_link "Advanced search" - select Setting["official_level_5_name"], from: "advanced_search_official_level" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - end - - context "Search by date" do - context "Predefined date ranges" do - scenario "Last day", :js do - proposal1 = create(:proposal, created_at: 1.minute.ago) - proposal2 = create(:proposal, created_at: 1.hour.ago) - proposal3 = create(:proposal, created_at: 2.days.ago) - - visit proposals_path - - click_link "Advanced search" - select "Last 24 hours", from: "js-advanced-search-date-min" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - scenario "Last week", :js do - proposal1 = create(:proposal, created_at: 1.day.ago) - proposal2 = create(:proposal, created_at: 5.days.ago) - proposal3 = create(:proposal, created_at: 8.days.ago) - - visit proposals_path - - click_link "Advanced search" - select "Last week", from: "js-advanced-search-date-min" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - scenario "Last month", :js do - proposal1 = create(:proposal, created_at: 10.days.ago) - proposal2 = create(:proposal, created_at: 20.days.ago) - proposal3 = create(:proposal, created_at: 33.days.ago) - - visit proposals_path - - click_link "Advanced search" - select "Last month", from: "js-advanced-search-date-min" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - scenario "Last year", :js do - proposal1 = create(:proposal, created_at: 300.days.ago) - proposal2 = create(:proposal, created_at: 350.days.ago) - proposal3 = create(:proposal, created_at: 370.days.ago) - - visit proposals_path - - click_link "Advanced search" - select "Last year", from: "js-advanced-search-date-min" - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - end - - scenario "Search by custom date range", :js do - proposal1 = create(:proposal, created_at: 2.days.ago) - proposal2 = create(:proposal, created_at: 3.days.ago) - proposal3 = create(:proposal, created_at: 9.days.ago) - - visit proposals_path - - click_link "Advanced search" - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: 7.days.ago - fill_in "advanced_search_date_max", with: 1.day.ago - click_button "Filter" - - expect(page).to have_content("There are 2 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).not_to have_content(proposal3.title) - end - end - - scenario "Search by custom invalid date range", :js do - proposal1 = create(:proposal, created_at: 2.days.ago) - proposal2 = create(:proposal, created_at: 3.days.ago) - proposal3 = create(:proposal, created_at: 9.days.ago) - - visit proposals_path - - click_link "Advanced search" - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: 4000.years.ago - fill_in "advanced_search_date_max", with: "wrong date" - click_button "Filter" - - expect(page).to have_content("There are 3 citizen proposals") - - within("#proposals") do - expect(page).to have_content(proposal1.title) - expect(page).to have_content(proposal2.title) - expect(page).to have_content(proposal3.title) - end - end - - scenario "Search by multiple filters", :js do - ana = create :user, official_level: 1 - john = create :user, official_level: 1 - - create(:proposal, title: "Get Schwifty", author: ana, created_at: 1.minute.ago) - create(:proposal, title: "Hello Schwifty", author: john, created_at: 2.days.ago) - create(:proposal, title: "Save the forest") - - visit proposals_path - - click_link "Advanced search" - fill_in "Write the text", with: "Schwifty" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - select "Last 24 hours", from: "js-advanced-search-date-min" - - click_button "Filter" - - expect(page).to have_content("There is 1 citizen proposal") - - within("#proposals") do - expect(page).to have_content "Get Schwifty" - end - end - - scenario "Maintain advanced search criteria", :js do - visit proposals_path - click_link "Advanced search" - - fill_in "Write the text", with: "Schwifty" - select Setting["official_level_1_name"], from: "advanced_search_official_level" - select "Last 24 hours", from: "js-advanced-search-date-min" - - click_button "Filter" - - expect(page).to have_content("citizen proposals cannot be found") - - within "#js-advanced-search" do - expect(page).to have_selector("input[name='search'][value='Schwifty']") - expect(page).to have_select("advanced_search[official_level]", selected: Setting["official_level_1_name"]) - expect(page).to have_select("advanced_search[date_min]", selected: "Last 24 hours") - end - end - - scenario "Maintain custom date search criteria", :js do - visit proposals_path - click_link "Advanced search" - - select "Customized", from: "js-advanced-search-date-min" - fill_in "advanced_search_date_min", with: 7.days.ago.strftime("%d/%m/%Y") - fill_in "advanced_search_date_max", with: 1.day.ago.strftime("%d/%m/%Y") - click_button "Filter" - - expect(page).to have_content("citizen proposals cannot be found") - - within "#js-advanced-search" do - expect(page).to have_select("advanced_search[date_min]", selected: "Customized") - expect(page).to have_selector("input[name='advanced_search[date_min]'][value*='#{7.days.ago.strftime("%d/%m/%Y")}']") - expect(page).to have_selector("input[name='advanced_search[date_max]'][value*='#{1.day.ago.strftime("%d/%m/%Y")}']") - end - end - end - - scenario "Search by SDG target", :js do - Setting["feature.sdg"] = true - Setting["sdg.process.proposals"] = true - create(:proposal, title: "Unrelated") - create(:proposal, title: "High school", sdg_targets: [SDG::Target["4.1"]]) - create(:proposal, title: "Preschool", sdg_targets: [SDG::Target["4.2"]]) - - visit proposals_path - click_link "Advanced search" - select "4.2", from: "By target" - click_button "Filter" - - expect(page).to have_content("There is 1 citizen proposal") - - within("#proposals") do - expect(page).to have_content("Preschool") - expect(page).not_to have_content("High school") - expect(page).not_to have_content("Unrelated") - end - end - end - scenario "Order by relevance by default", :js do create(:proposal, title: "In summary", summary: "Title content too", cached_votes_up: 10) create(:proposal, title: "Title content", summary: "Summary", cached_votes_up: 1) From 0aa0a9c16b7123194dcadb815675dfb1c7d7d79d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 28 Jan 2021 13:38:18 +0100 Subject: [PATCH 3/4] Add missing test to search by SDG We had only added a test to seach by target. --- spec/system/advanced_search_spec.rb | 53 +++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/spec/system/advanced_search_spec.rb b/spec/system/advanced_search_spec.rb index ee0e7d5cf..c3d41a1ea 100644 --- a/spec/system/advanced_search_spec.rb +++ b/spec/system/advanced_search_spec.rb @@ -367,24 +367,47 @@ describe "Advanced search", :js do end end - scenario "Search by SDG target" do - Setting["feature.sdg"] = true - Setting["sdg.process.debates"] = true - create(:debate, title: "Unrelated") - create(:debate, title: "High school", sdg_targets: [SDG::Target["4.1"]]) - create(:debate, title: "Preschool", sdg_targets: [SDG::Target["4.2"]]) + describe "SDG" do + before do + Setting["feature.sdg"] = true + Setting["sdg.process.debates"] = true + Setting["sdg.process.budgets"] = true + end - visit debates_path - click_link "Advanced search" - select "4.2", from: "By target" - click_button "Filter" + scenario "Search by goal" do + create(:budget_investment, title: "Purifier", heading: heading, sdg_goals: [SDG::Goal[6]]) + create(:budget_investment, title: "Hospital", heading: heading, sdg_goals: [SDG::Goal[3]]) - expect(page).to have_content("There is 1 debate") + visit budget_investments_path(budget) + click_link "Advanced search" + select "6. Clean Water and Sanitation", from: "By SDG" + click_button "Filter" - within("#debates") do - expect(page).to have_content("Preschool") - expect(page).not_to have_content("High school") - expect(page).not_to have_content("Unrelated") + expect(page).to have_content("There is 1 investment") + + within("#budget-investments") do + expect(page).to have_content "Purifier" + expect(page).not_to have_content "Hospital" + end + end + + scenario "Search by target" do + create(:debate, title: "Unrelated") + create(:debate, title: "High school", sdg_targets: [SDG::Target["4.1"]]) + create(:debate, title: "Preschool", sdg_targets: [SDG::Target["4.2"]]) + + visit debates_path + click_link "Advanced search" + select "4.2", from: "By target" + click_button "Filter" + + expect(page).to have_content("There is 1 debate") + + within("#debates") do + expect(page).to have_content("Preschool") + expect(page).not_to have_content("High school") + expect(page).not_to have_content("Unrelated") + end end end end From 08c410cc93334f411d2149c555a75c13e22daa9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 28 Jan 2021 13:46:08 +0100 Subject: [PATCH 4/4] Make target filter change when goal filter changes Note we're using both the `hidden` and `disabled` properties to guarantee compatibility with user agents which might still display the option even when using the `hidden` attribute or hiding it with `display: none`. We could also use `hide()` and `show()` instead of the `hidden` property, but since we're using the `disabled` property, I thought the code would be easier to read if we used properties in both cases. Also note users will no longer be able to get, let's say, debates which are related to goal 1 and target 2.1. We think this use case is highly unlikely and there's no need to take it into account. --- app/assets/javascripts/advanced_search.js | 2 ++ app/assets/javascripts/application.js | 4 ++- .../sdg/sync_goal_and_target_filters.js | 28 +++++++++++++++++++ .../sdg_management/relations/search.js | 8 ++++++ .../relations/search_component.html.erb | 2 +- spec/system/advanced_search_spec.rb | 28 +++++++++++++++++++ spec/system/sdg_management/relations_spec.rb | 26 +++++++++++++++++ 7 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/sdg/sync_goal_and_target_filters.js create mode 100644 app/assets/javascripts/sdg_management/relations/search.js diff --git a/app/assets/javascripts/advanced_search.js b/app/assets/javascripts/advanced_search.js index 51b257ee7..9fcf8ae5f 100644 --- a/app/assets/javascripts/advanced_search.js +++ b/app/assets/javascripts/advanced_search.js @@ -32,6 +32,8 @@ App.AdvancedSearch.toggle_date_options(); } }); + + App.SDGSyncGoalAndTargetFilters.sync($("#advanced_search_form")); } }; }).call(this); diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index ad7798135..f66510049 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -112,7 +112,8 @@ //= require columns_selector //= require budget_edit_associations //= require datepicker -//= require sdg/related_list_selector +//= require_tree ./sdg +//= require_tree ./sdg_management var initialize_modules = function() { "use strict"; @@ -166,6 +167,7 @@ var initialize_modules = function() { App.BudgetEditAssociations.initialize(); App.Datepicker.initialize(); App.SDGRelatedListSelector.initialize(); + App.SDGManagementRelationSearch.initialize(); }; var destroy_non_idempotent_modules = function() { diff --git a/app/assets/javascripts/sdg/sync_goal_and_target_filters.js b/app/assets/javascripts/sdg/sync_goal_and_target_filters.js new file mode 100644 index 000000000..6ded27389 --- /dev/null +++ b/app/assets/javascripts/sdg/sync_goal_and_target_filters.js @@ -0,0 +1,28 @@ +(function() { + "use strict"; + App.SDGSyncGoalAndTargetFilters = { + sync: function(form) { + var goal_filter = form.find("[name*=goal]"); + var target_filter = form.find("[name*=target]"); + + goal_filter.on("change", function() { + if (this.value) { + App.SDGSyncGoalAndTargetFilters.enable_goal_options(target_filter, this.value); + } else { + target_filter.find("option").prop("disabled", false).prop("hidden", false); + } + }).trigger("change"); + }, + enable_goal_options: function(target_filter, goal_code) { + var base_target_code = goal_code + "."; + var target_selector = "[value^='" + base_target_code + "'], [value='']"; + + target_filter.find("option" + target_selector).prop("disabled", false).prop("hidden", false); + target_filter.find("option:not(" + target_selector + ")").prop("disabled", true).prop("hidden", true); + + if (!target_filter[0].value.startsWith(base_target_code)) { + target_filter.val(""); + } + } + }; +}).call(this); diff --git a/app/assets/javascripts/sdg_management/relations/search.js b/app/assets/javascripts/sdg_management/relations/search.js new file mode 100644 index 000000000..328d40fea --- /dev/null +++ b/app/assets/javascripts/sdg_management/relations/search.js @@ -0,0 +1,8 @@ +(function() { + "use strict"; + App.SDGManagementRelationSearch = { + initialize: function() { + App.SDGSyncGoalAndTargetFilters.sync($(".sdg-relations-search")); + } + }; +}).call(this); diff --git a/app/components/sdg_management/relations/search_component.html.erb b/app/components/sdg_management/relations/search_component.html.erb index b4a61be21..d77f21c8f 100644 --- a/app/components/sdg_management/relations/search_component.html.erb +++ b/app/components/sdg_management/relations/search_component.html.erb @@ -1,4 +1,4 @@ -<%= render Admin::SearchComponent.new(label: label, class: "complex") do |component| %> +<%= render Admin::SearchComponent.new(label: label, class: "sdg-relations-search complex") do |component| %> <%= component.select_tag :goal_code, goal_options, include_blank: goal_blank_option, "aria-label": goal_label %> diff --git a/spec/system/advanced_search_spec.rb b/spec/system/advanced_search_spec.rb index c3d41a1ea..a18795f65 100644 --- a/spec/system/advanced_search_spec.rb +++ b/spec/system/advanced_search_spec.rb @@ -371,6 +371,7 @@ describe "Advanced search", :js do before do Setting["feature.sdg"] = true Setting["sdg.process.debates"] = true + Setting["sdg.process.proposals"] = true Setting["sdg.process.budgets"] = true end @@ -389,6 +390,10 @@ describe "Advanced search", :js do expect(page).to have_content "Purifier" expect(page).not_to have_content "Hospital" end + + expect(page).to have_select "By target", + selected: "Select a target", + enabled_options: ["Select a target"] + %w[6.1 6.2 6.3 6.4 6.5 6.6 6.A 6.B] end scenario "Search by target" do @@ -409,6 +414,29 @@ describe "Advanced search", :js do expect(page).not_to have_content("Unrelated") end end + + scenario "Dynamic target options depending on the selected goal" do + visit proposals_path + + click_link "Advanced search" + select "1. No Poverty", from: "By SDG" + + expect(page).to have_select "By target", + selected: "Select a target", + enabled_options: ["Select a target"] + %w[1.1 1.2 1.3 1.4 1.5 1.A 1.B] + + select "1.1", from: "By target" + select "13. Climate Action", from: "By SDG" + + expect(page).to have_select "By target", + selected: "Select a target", + enabled_options: ["Select a target"] + %w[13.1 13.2 13.3 13.A 13.B] + + select "13.1", from: "By target" + select "Select a goal", from: "By SDG" + + expect(page).to have_select "By target", selected: "13.1", disabled_options: [] + end end end end diff --git a/spec/system/sdg_management/relations_spec.rb b/spec/system/sdg_management/relations_spec.rb index 9221e01c1..be5bff406 100644 --- a/spec/system/sdg_management/relations_spec.rb +++ b/spec/system/sdg_management/relations_spec.rb @@ -152,6 +152,10 @@ describe "SDG Relations", :js do expect(page).to have_content "School" expect(page).not_to have_content "Hospital" expect(page).to have_css "li.is-active h2", exact_text: "Pending" + + expect(page).to have_select "By target", + selected: "All targets", + enabled_options: ["All targets"] + %w[4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.A 4.B 4.C] end scenario "target filter" do @@ -200,6 +204,28 @@ describe "SDG Relations", :js do expect(page).to have_css "li.is-active h2", exact_text: "All" end + + scenario "dynamic target options depending on the selected goal" do + visit sdg_management_polls_path + + select "1. No Poverty", from: "By goal" + + expect(page).to have_select "By target", + selected: "All targets", + enabled_options: ["All targets"] + %w[1.1 1.2 1.3 1.4 1.5 1.A 1.B] + + select "1.1", from: "By target" + select "13. Climate Action", from: "By goal" + + expect(page).to have_select "By target", + selected: "All targets", + enabled_options: ["All targets"] + %w[13.1 13.2 13.3 13.A 13.B] + + select "13.1", from: "By target" + select "All goals", from: "By goal" + + expect(page).to have_select "By target", selected: "13.1", disabled_options: [] + end end end