Add method to stub current_user in component tests

We're choosing `sign_in` instead of `login_as` because IMHO component
tests are more similar to controller tests than they are to system
tests.
This commit is contained in:
Javi Martín
2021-09-04 15:30:04 +02:00
parent 4cbf945228
commit e97c375063
11 changed files with 30 additions and 31 deletions

View File

@@ -5,9 +5,7 @@ describe Budgets::BudgetComponent do
let(:heading) { create(:budget_heading, budget: budget) }
let(:user) { create(:user) }
before do
allow(controller).to receive(:current_user).and_return(user)
end
before { sign_in(user) }
describe "budget header" do
it "shows budget name and link to help" do

View File

@@ -1,11 +1,7 @@
require "rails_helper"
describe Budgets::InvestmentComponent do
let(:user) { create(:user) }
before do
allow(controller).to receive(:current_user).and_return(user)
end
before { sign_in(create(:user)) }
it "shows the investment image when defined" do
investment = create(:budget_investment, :with_image)

View File

@@ -4,10 +4,7 @@ describe Budgets::Investments::FormComponent do
include Rails.application.routes.url_helpers
let(:budget) { create(:budget) }
before do
allow(controller).to receive(:current_user).and_return(create(:user))
end
before { sign_in(create(:user)) }
around do |example|
with_request_url(new_budget_investment_path(budget)) { example.run }

View File

@@ -9,7 +9,7 @@ describe Budgets::Investments::VotesComponent do
before { allow(investment).to receive(:should_show_votes?).and_return(true) }
it "displays a button to support the investment to identified users" do
allow(controller).to receive(:current_user).and_return(create(:user))
sign_in(create(:user))
render_inline component
@@ -19,7 +19,7 @@ describe Budgets::Investments::VotesComponent do
end
it "disables the button to support the investment to unidentified users" do
allow(controller).to receive(:current_user).and_return(nil)
sign_in(nil)
render_inline component
@@ -30,7 +30,7 @@ describe Budgets::Investments::VotesComponent do
it "shows the button to remove support when users have supported the investment" do
user = create(:user)
user.up_votes(investment)
allow(controller).to receive(:current_user).and_return(user)
sign_in(user)
render_inline component

View File

@@ -7,7 +7,7 @@ describe Budgets::InvestmentsListComponent do
let(:group) { create(:budget_group, budget: budget) }
let(:heading) { create(:budget_heading, group: group) }
before { allow(controller).to receive(:current_user).and_return(nil) }
before { sign_in(nil) }
describe "#investments" do
let(:component) { Budgets::InvestmentsListComponent.new(budget) }

View File

@@ -2,7 +2,7 @@ require "rails_helper"
describe Budgets::SubheaderComponent do
it "shows budget current phase name" do
allow(controller).to receive(:current_user).and_return(create(:user))
sign_in(create(:user))
budget = create(:budget, :informing)
render_inline Budgets::SubheaderComponent.new(budget)
@@ -17,7 +17,7 @@ describe Budgets::SubheaderComponent do
let(:budget) { create(:budget, :accepting) }
it "and user is level_two_or_three_verified shows a link to create a new investment" do
allow(controller).to receive(:current_user).and_return(create(:user, :level_two))
sign_in(create(:user, :level_two))
render_inline Budgets::SubheaderComponent.new(budget)
@@ -34,7 +34,7 @@ describe Budgets::SubheaderComponent do
end
it "and user is not verified shows a link to account verification" do
allow(controller).to receive(:current_user).and_return(create(:user))
sign_in(create(:user))
render_inline Budgets::SubheaderComponent.new(budget)
@@ -53,7 +53,7 @@ describe Budgets::SubheaderComponent do
end
it "and user is not logged in shows links to sign in and sign up" do
allow(controller).to receive(:current_user).and_return(nil)
sign_in(nil)
render_inline Budgets::SubheaderComponent.new(budget)
@@ -77,12 +77,12 @@ describe Budgets::SubheaderComponent do
describe "See results link" do
it "is showed when budget is finished and results are enabled for all users" do
budget = create(:budget, :finished)
allow(controller).to receive(:current_user).and_return(create(:user))
sign_in(create(:user))
render_inline Budgets::SubheaderComponent.new(budget)
expect(page).to have_link "See results"
allow(controller).to receive(:current_user).and_return(create(:administrator).user)
sign_in(create(:administrator).user)
render_inline Budgets::SubheaderComponent.new(budget)
expect(page).to have_link "See results"
@@ -90,12 +90,12 @@ describe Budgets::SubheaderComponent do
it "is not showed when budget is finished or results are disabled for all users" do
budget = create(:budget, :balloting, results_enabled: true)
allow(controller).to receive(:current_user).and_return(create(:user))
sign_in(create(:user))
render_inline Budgets::SubheaderComponent.new(budget)
expect(page).not_to have_link "See results"
allow(controller).to receive(:current_user).and_return(create(:administrator).user)
sign_in(create(:administrator).user)
render_inline Budgets::SubheaderComponent.new(budget)
expect(page).not_to have_link "See results"

View File

@@ -4,7 +4,7 @@ describe Budgets::SupportsInfoComponent do
let(:budget) { create(:budget, :selecting) }
let(:group) { create(:budget_group, budget: budget) }
let(:component) { Budgets::SupportsInfoComponent.new(budget) }
before { allow(component).to receive(:current_user).and_return(nil) }
before { sign_in(nil) }
it "renders when the budget is selecting" do
create(:budget_heading, group: group)
@@ -42,7 +42,7 @@ describe Budgets::SupportsInfoComponent do
context "logged users" do
let(:user) { create(:user, :level_two) }
before { allow(component).to receive(:current_user).and_return(user) }
before { sign_in(user) }
it "shows supported investments" do
heading = create(:budget_heading, budget: budget)
@@ -67,8 +67,6 @@ describe Budgets::SupportsInfoComponent do
it "does not show supports for another budget" do
second_budget = create(:budget, phase: "selecting")
second_component = Budgets::SupportsInfoComponent.new(second_budget)
allow(second_component).to receive(:current_user).and_return(user)
create_list(:budget_investment, 2, :selected, budget: budget, voters: [user])
create_list(:budget_investment, 3, :selected, budget: second_budget, voters: [user])
@@ -76,6 +74,8 @@ describe Budgets::SupportsInfoComponent do
expect(page).to have_content "So far you've supported 2 projects."
sign_in(user)
render_inline second_component
expect(page).to have_content "So far you've supported 3 projects."

View File

@@ -7,7 +7,7 @@ describe MachineLearning::CommentsSummaryComponent do
before do
Setting["feature.machine_learning"] = true
Setting["machine_learning.comments_summary"] = true
allow(controller).to receive(:current_user).and_return(nil)
sign_in(nil)
end
it "is displayed when the setting is enabled" do

View File

@@ -15,7 +15,7 @@ describe Relationable::RelatedListComponent do
child_relationable: machine_proposal,
machine_learning: true)
allow(controller).to receive(:current_user).and_return(nil)
sign_in(nil)
end
it "displays machine learning and user content when machine learning is enabled" do

View File

@@ -9,7 +9,7 @@ describe Shared::TagListComponent do
before do
Setting["feature.machine_learning"] = true
Setting["machine_learning.tags"] = true
allow(controller).to receive(:current_user).and_return(create(:administrator).user)
sign_in(create(:administrator).user)
end
it "displays machine learning tags when machine learning is enabled" do

View File

@@ -13,6 +13,14 @@ require "capybara/rspec"
require "selenium/webdriver"
require "view_component/test_helpers"
module ViewComponent
module TestHelpers
def sign_in(user)
allow(controller).to receive(:current_user).and_return(user)
end
end
end
RSpec.configure do |config|
config.include ViewComponent::TestHelpers, type: :component
end