From b8ed81c5b8b72b2a4561e19b38ef821dba189459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 2 Jul 2023 01:59:50 +0200 Subject: [PATCH] Simplify setting up admin component tests --- .../admin/allowed_table_actions_component_spec.rb | 3 +-- .../admin/budget_headings/headings_component_spec.rb | 2 +- .../admin/budget_phases/toggle_enabled_component_spec.rb | 2 +- spec/components/admin/budgets/actions_component_spec.rb | 3 +-- .../budgets/calculate_winners_button_component_spec.rb | 3 +-- spec/components/admin/budgets/links_component_spec.rb | 4 +--- .../admin/budgets/table_actions_component_spec.rb | 2 +- spec/components/admin/geozones/index_component_spec.rb | 2 +- .../admin/poll/officers/officers_component_spec.rb | 2 +- .../answers/documents/table_actions_component_spec.rb | 3 +-- .../questions/answers/table_actions_component_spec.rb | 4 +--- .../answers/videos/table_actions_component_spec.rb | 4 +--- .../admin/poll/questions/table_actions_component_spec.rb | 4 +--- .../admin/roles/table_actions_component_spec.rb | 2 +- spec/components/admin/table_actions_component_spec.rb | 2 +- spec/spec_helper.rb | 8 ++++++++ 16 files changed, 23 insertions(+), 27 deletions(-) diff --git a/spec/components/admin/allowed_table_actions_component_spec.rb b/spec/components/admin/allowed_table_actions_component_spec.rb index 6396be6b8..929ce0d72 100644 --- a/spec/components/admin/allowed_table_actions_component_spec.rb +++ b/spec/components/admin/allowed_table_actions_component_spec.rb @@ -1,8 +1,7 @@ require "rails_helper" -describe Admin::AllowedTableActionsComponent, controller: Admin::BaseController do +describe Admin::AllowedTableActionsComponent, :admin do before do - sign_in(create(:administrator).user) allow_any_instance_of(Admin::AllowedTableActionsComponent).to receive(:can?).and_return true end let(:record) { create(:banner, title: "Important!") } diff --git a/spec/components/admin/budget_headings/headings_component_spec.rb b/spec/components/admin/budget_headings/headings_component_spec.rb index e36c5ab4c..b425b216b 100644 --- a/spec/components/admin/budget_headings/headings_component_spec.rb +++ b/spec/components/admin/budget_headings/headings_component_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Admin::BudgetHeadings::HeadingsComponent, controller: Admin::BaseController do +describe Admin::BudgetHeadings::HeadingsComponent, :admin do it "includes group name in the message when there are no headings" do group = create(:budget_group, name: "Whole planet") diff --git a/spec/components/admin/budget_phases/toggle_enabled_component_spec.rb b/spec/components/admin/budget_phases/toggle_enabled_component_spec.rb index 6786e1eb8..8b980bf5c 100644 --- a/spec/components/admin/budget_phases/toggle_enabled_component_spec.rb +++ b/spec/components/admin/budget_phases/toggle_enabled_component_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Admin::BudgetPhases::ToggleEnabledComponent, controller: Admin::BaseController do +describe Admin::BudgetPhases::ToggleEnabledComponent, :admin do let(:phase) { create(:budget).phases.informing } let(:component) { Admin::BudgetPhases::ToggleEnabledComponent.new(phase) } diff --git a/spec/components/admin/budgets/actions_component_spec.rb b/spec/components/admin/budgets/actions_component_spec.rb index b67ac8c9d..e47845a80 100644 --- a/spec/components/admin/budgets/actions_component_spec.rb +++ b/spec/components/admin/budgets/actions_component_spec.rb @@ -1,8 +1,7 @@ require "rails_helper" -describe Admin::Budgets::ActionsComponent, controller: Admin::BaseController do +describe Admin::Budgets::ActionsComponent, :admin do include Rails.application.routes.url_helpers - before { sign_in(create(:administrator).user) } let(:budget) { create(:budget) } let(:component) { Admin::Budgets::ActionsComponent.new(budget) } diff --git a/spec/components/admin/budgets/calculate_winners_button_component_spec.rb b/spec/components/admin/budgets/calculate_winners_button_component_spec.rb index 1c26c1f16..e11975cdb 100644 --- a/spec/components/admin/budgets/calculate_winners_button_component_spec.rb +++ b/spec/components/admin/budgets/calculate_winners_button_component_spec.rb @@ -1,9 +1,8 @@ require "rails_helper" -describe Admin::Budgets::CalculateWinnersButtonComponent, controller: Admin::BaseController do +describe Admin::Budgets::CalculateWinnersButtonComponent, :admin do let(:budget) { create(:budget) } let(:component) { Admin::Budgets::CalculateWinnersButtonComponent.new(budget) } - before { sign_in(create(:administrator).user) } it "renders when reviewing ballots" do budget.update!(phase: "reviewing_ballots") diff --git a/spec/components/admin/budgets/links_component_spec.rb b/spec/components/admin/budgets/links_component_spec.rb index 997b1b6bd..14d25f889 100644 --- a/spec/components/admin/budgets/links_component_spec.rb +++ b/spec/components/admin/budgets/links_component_spec.rb @@ -1,8 +1,6 @@ require "rails_helper" -describe Admin::Budgets::LinksComponent, controller: Admin::BaseController do - before { sign_in(create(:administrator).user) } - +describe Admin::Budgets::LinksComponent, :admin do describe "see results link" do let(:budget) { create(:budget, :finished) } let(:component) { Admin::Budgets::LinksComponent.new(budget) } diff --git a/spec/components/admin/budgets/table_actions_component_spec.rb b/spec/components/admin/budgets/table_actions_component_spec.rb index 39dd13b43..0d994e5f5 100644 --- a/spec/components/admin/budgets/table_actions_component_spec.rb +++ b/spec/components/admin/budgets/table_actions_component_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Admin::Budgets::TableActionsComponent, controller: Admin::BaseController do +describe Admin::Budgets::TableActionsComponent, :admin do let(:budget) { create(:budget) } let(:component) { Admin::Budgets::TableActionsComponent.new(budget) } diff --git a/spec/components/admin/geozones/index_component_spec.rb b/spec/components/admin/geozones/index_component_spec.rb index 6d3fe3dd4..dc5b7e1e9 100644 --- a/spec/components/admin/geozones/index_component_spec.rb +++ b/spec/components/admin/geozones/index_component_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Admin::Geozones::IndexComponent, controller: Admin::BaseController do +describe Admin::Geozones::IndexComponent, :admin do describe "Coordinates description" do it "includes whether coordinates are defined or not" do geozones = [ diff --git a/spec/components/admin/poll/officers/officers_component_spec.rb b/spec/components/admin/poll/officers/officers_component_spec.rb index c2ed0f1a6..51246a02d 100644 --- a/spec/components/admin/poll/officers/officers_component_spec.rb +++ b/spec/components/admin/poll/officers/officers_component_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Admin::Poll::Officers::OfficersComponent, controller: Admin::BaseController do +describe Admin::Poll::Officers::OfficersComponent, :admin do let(:existing_officer) { create(:poll_officer, name: "Old officer") } let(:new_officer) { build(:poll_officer, name: "New officer") } let(:officers) { [existing_officer, new_officer] } diff --git a/spec/components/admin/poll/questions/answers/documents/table_actions_component_spec.rb b/spec/components/admin/poll/questions/answers/documents/table_actions_component_spec.rb index 2d2261aea..218b46c41 100644 --- a/spec/components/admin/poll/questions/answers/documents/table_actions_component_spec.rb +++ b/spec/components/admin/poll/questions/answers/documents/table_actions_component_spec.rb @@ -1,7 +1,6 @@ require "rails_helper" -describe Admin::Poll::Questions::Answers::Documents::TableActionsComponent, controller: Admin::BaseController do - before { sign_in(create(:administrator).user) } +describe Admin::Poll::Questions::Answers::Documents::TableActionsComponent, :admin do let(:future_answer) { create(:poll_question_answer, poll: create(:poll, :future)) } let(:current_answer) { create(:poll_question_answer, poll: create(:poll)) } diff --git a/spec/components/admin/poll/questions/answers/table_actions_component_spec.rb b/spec/components/admin/poll/questions/answers/table_actions_component_spec.rb index 4e6080a11..4b8243209 100644 --- a/spec/components/admin/poll/questions/answers/table_actions_component_spec.rb +++ b/spec/components/admin/poll/questions/answers/table_actions_component_spec.rb @@ -1,8 +1,6 @@ require "rails_helper" -describe Admin::Poll::Questions::Answers::TableActionsComponent, controller: Admin::BaseController do - before { sign_in(create(:administrator).user) } - +describe Admin::Poll::Questions::Answers::TableActionsComponent, :admin do it "displays the edit and destroy actions when the poll has not started" do answer = create(:poll_question_answer, poll: create(:poll, :future)) diff --git a/spec/components/admin/poll/questions/answers/videos/table_actions_component_spec.rb b/spec/components/admin/poll/questions/answers/videos/table_actions_component_spec.rb index e8449103d..199e83c7a 100644 --- a/spec/components/admin/poll/questions/answers/videos/table_actions_component_spec.rb +++ b/spec/components/admin/poll/questions/answers/videos/table_actions_component_spec.rb @@ -1,8 +1,6 @@ require "rails_helper" -describe Admin::Poll::Questions::Answers::Videos::TableActionsComponent, controller: Admin::BaseController do - before { sign_in(create(:administrator).user) } - +describe Admin::Poll::Questions::Answers::Videos::TableActionsComponent, :admin do it "displays the edit and destroy actions when the poll has not started" do video = create(:poll_answer_video, poll: create(:poll, :future)) diff --git a/spec/components/admin/poll/questions/table_actions_component_spec.rb b/spec/components/admin/poll/questions/table_actions_component_spec.rb index b1b84513b..3b1095d9c 100644 --- a/spec/components/admin/poll/questions/table_actions_component_spec.rb +++ b/spec/components/admin/poll/questions/table_actions_component_spec.rb @@ -1,8 +1,6 @@ require "rails_helper" -describe Admin::Poll::Questions::TableActionsComponent, controller: Admin::BaseController do - before { sign_in(create(:administrator).user) } - +describe Admin::Poll::Questions::TableActionsComponent, :admin do it "displays the edit and destroy actions when the poll has not started" do question = create(:poll_question, poll: create(:poll, :future)) diff --git a/spec/components/admin/roles/table_actions_component_spec.rb b/spec/components/admin/roles/table_actions_component_spec.rb index 445f790e1..b1e06d116 100644 --- a/spec/components/admin/roles/table_actions_component_spec.rb +++ b/spec/components/admin/roles/table_actions_component_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Admin::Roles::TableActionsComponent, controller: Admin::BaseController do +describe Admin::Roles::TableActionsComponent, :admin do let(:user) { create(:user) } it "renders button to add the role for new records" do diff --git a/spec/components/admin/table_actions_component_spec.rb b/spec/components/admin/table_actions_component_spec.rb index cc5b554de..3951a2421 100644 --- a/spec/components/admin/table_actions_component_spec.rb +++ b/spec/components/admin/table_actions_component_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Admin::TableActionsComponent, controller: Admin::BaseController do +describe Admin::TableActionsComponent, :admin do let(:record) { create(:banner, title: "Important!") } it "renders edit and destroy actions by default" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7c941094e..edd1036f2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -94,6 +94,14 @@ RSpec.configure do |config| sign_in(nil) end + config.before(:each, :admin, type: :component) do + sign_in(create(:administrator).user) + end + + config.around(:each, :admin, type: :component) do |example| + with_controller_class(Admin::BaseController) { example.run } + end + config.around(:each, :controller, type: :component) do |example| with_controller_class(example.metadata[:controller]) { example.run } end