From 5f19d73b400a22398bee2a7bc211821b4f2dd196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 12 Aug 2021 22:50:40 +0200 Subject: [PATCH] Use with_request_url in component specs This method is available since view component 2.31.0, and greatly simplifies tests depending on the current URL. --- spec/components/budgets/investments/form_component_spec.rb | 5 ++++- spec/components/layout/locale_switcher_component_spec.rb | 6 ++---- .../sdg_management/relations/index_component_spec.rb | 4 +--- spec/components/shared/filter_selector_component_spec.rb | 3 +-- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/spec/components/budgets/investments/form_component_spec.rb b/spec/components/budgets/investments/form_component_spec.rb index 8e6bcfb11..2402cba81 100644 --- a/spec/components/budgets/investments/form_component_spec.rb +++ b/spec/components/budgets/investments/form_component_spec.rb @@ -7,7 +7,10 @@ describe Budgets::Investments::FormComponent, type: :component do before do allow(controller).to receive(:current_user).and_return(create(:user)) - allow(request).to receive(:path_parameters).and_return(budget_id: budget.id) + end + + around do |example| + with_request_url(new_budget_investment_path(budget)) { example.run } end describe "accept terms of services field" do diff --git a/spec/components/layout/locale_switcher_component_spec.rb b/spec/components/layout/locale_switcher_component_spec.rb index 5d1462e49..e0dd29474 100644 --- a/spec/components/layout/locale_switcher_component_spec.rb +++ b/spec/components/layout/locale_switcher_component_spec.rb @@ -3,10 +3,8 @@ require "rails_helper" describe Layout::LocaleSwitcherComponent, type: :component do let(:component) { Layout::LocaleSwitcherComponent.new } - before do - allow(request).to receive(:path_parameters).and_return( - Rails.application.routes.recognize_path("/") - ) + around do |example| + with_request_url("/") { example.run } end context "with only one language" do diff --git a/spec/components/sdg_management/relations/index_component_spec.rb b/spec/components/sdg_management/relations/index_component_spec.rb index 6e9eb90ad..ce6308811 100644 --- a/spec/components/sdg_management/relations/index_component_spec.rb +++ b/spec/components/sdg_management/relations/index_component_spec.rb @@ -6,14 +6,12 @@ describe SDGManagement::Relations::IndexComponent, type: :component, controller: .and_return(SDGManagement::RelationsController::FILTERS) allow_any_instance_of(SDGManagement::RelationsController).to receive(:current_filter) .and_return(SDGManagement::RelationsController::FILTERS.first) - allow_any_instance_of(ApplicationHelper).to receive(:current_path_with_query_params) - .and_return("/anything") end it "renders the search form" do component = SDGManagement::Relations::IndexComponent.new(Proposal.none.page(1)) - render_inline component + with_request_url("/anything") { render_inline component } expect(page).to have_css "form.complex" end diff --git a/spec/components/shared/filter_selector_component_spec.rb b/spec/components/shared/filter_selector_component_spec.rb index 6943a1a9e..9af8eeea9 100644 --- a/spec/components/shared/filter_selector_component_spec.rb +++ b/spec/components/shared/filter_selector_component_spec.rb @@ -3,11 +3,10 @@ require "rails_helper" describe Shared::FilterSelectorComponent, type: :component do it "renders a form with a select" do component = Shared::FilterSelectorComponent.new(i18n_namespace: "budgets.investments.index") - allow(component).to receive(:url_for).and_return("/") allow(component).to receive(:valid_filters).and_return(["unfeasible", "winners"]) allow(component).to receive(:current_filter).and_return(nil) - render_inline component + with_request_url("/") { render_inline component } expect(page).to have_select "Filtering projects by" expect(page).to have_selector "form[method='get'].filter-selector select"