Show a preview list of investments in the budget landing page
Note one of the tests dealing with random results is a bit flaky; since it's a permutation selecting 7 objects out of 12, it will fail about once every 4 million times. We think this is acceptable. Co-Authored-By: Julian Nicolas Herrero <microweb10@gmail.com>
This commit is contained in:
59
spec/components/budgets/investment_component_spec.rb
Normal file
59
spec/components/budgets/investment_component_spec.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Budgets::InvestmentComponent, type: :component do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:current_user).and_return(user)
|
||||
end
|
||||
|
||||
it "shows the investment image when defined" do
|
||||
investment = create(:budget_investment, :with_image)
|
||||
render_inline Budgets::InvestmentComponent.new(investment)
|
||||
|
||||
expect(page).not_to have_css "img[src*='budget_investment_no_image']"
|
||||
expect(page).to have_css "img[alt='#{investment.image.title}']"
|
||||
end
|
||||
|
||||
it "shows the default image when investment has not an image defined" do
|
||||
investment = create(:budget_investment)
|
||||
render_inline Budgets::InvestmentComponent.new(investment)
|
||||
|
||||
expect(page).to have_css "img[src*='budget_investment_no_image']"
|
||||
end
|
||||
|
||||
it "shows supports count when budget is valuating" do
|
||||
budget = create(:budget, :valuating)
|
||||
investment = create(:budget_investment, budget: budget)
|
||||
render_inline Budgets::InvestmentComponent.new(investment)
|
||||
|
||||
expect(page).to have_content "Supports"
|
||||
|
||||
budget.update!(phase: (Budget::Phase::PHASE_KINDS - ["valuating"]).sample)
|
||||
render_inline Budgets::InvestmentComponent.new(investment)
|
||||
|
||||
expect(page).not_to have_content "Supports"
|
||||
end
|
||||
|
||||
it "shows price when investment is selected and budget prices are published" do
|
||||
budget = create(:budget, :finished)
|
||||
investment = create(:budget_investment, :selected, budget: budget)
|
||||
render_inline Budgets::InvestmentComponent.new(investment)
|
||||
|
||||
expect(page).to have_content "Price"
|
||||
|
||||
budget.update!(phase: (Budget::Phase::PHASE_KINDS - Budget::Phase::PUBLISHED_PRICES_PHASES).sample)
|
||||
render_inline Budgets::InvestmentComponent.new(investment)
|
||||
|
||||
expect(page).not_to have_content "Price"
|
||||
end
|
||||
|
||||
it "shows investment information" do
|
||||
investment = create(:budget_investment)
|
||||
render_inline Budgets::InvestmentComponent.new(investment)
|
||||
|
||||
expect(page).to have_link investment.title
|
||||
expect(page).to have_link "Read more"
|
||||
expect(page).to have_css ".investment-project-info"
|
||||
end
|
||||
end
|
||||
63
spec/components/budgets/investments_list_component_spec.rb
Normal file
63
spec/components/budgets/investments_list_component_spec.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Budgets::InvestmentsListComponent, type: :component do
|
||||
describe "#investments_preview_list" do
|
||||
let(:budget) { create(:budget, :accepting) }
|
||||
let(:group) { create(:budget_group, budget: budget) }
|
||||
let(:heading) { create(:budget_heading, group: group) }
|
||||
let(:component) { Budgets::InvestmentsListComponent.new(budget) }
|
||||
|
||||
let!(:normal_investments) { create_list(:budget_investment, 4, heading: heading) }
|
||||
let!(:feasible_investments) { create_list(:budget_investment, 4, :feasible, heading: heading) }
|
||||
let!(:selected_investments) { create_list(:budget_investment, 4, :selected, heading: heading) }
|
||||
|
||||
it "returns an empty relation if phase is informing or finished" do
|
||||
%w[informing finished].each do |phase_name|
|
||||
budget.phase = phase_name
|
||||
|
||||
expect(component.investments).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "returns a maximum 9 investments by default" do
|
||||
expect(budget.investments.count).to be 12
|
||||
expect(component.investments.count).to be 9
|
||||
end
|
||||
|
||||
it "returns a different random array of investments every time" do
|
||||
expect(component.investments(limit: 7)).not_to eq component.investments(limit: 7)
|
||||
end
|
||||
|
||||
it "returns any investments while accepting and reviewing" do
|
||||
%w[accepting reviewing].each do |phase_name|
|
||||
budget.phase = phase_name
|
||||
|
||||
investments = component.investments(limit: 9)
|
||||
|
||||
expect(investments & normal_investments).not_to be_empty
|
||||
expect(investments & feasible_investments).not_to be_empty
|
||||
expect(investments & selected_investments).not_to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "returns only feasible investments if phase is selecting, valuating or publishing_prices" do
|
||||
%w[selecting valuating publishing_prices].each do |phase_name|
|
||||
budget.phase = phase_name
|
||||
|
||||
investments = component.investments(limit: 6)
|
||||
|
||||
expect(feasible_investments + selected_investments).to include(*investments)
|
||||
end
|
||||
end
|
||||
|
||||
it "returns only selected investments if phase is balloting or reviewing_ballots" do
|
||||
%w[balloting reviewing_ballots].each do |phase_name|
|
||||
budget.phase = phase_name
|
||||
|
||||
investments = component.investments(limit: 3)
|
||||
|
||||
expect(selected_investments).to include(*investments)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user