From f92796c7facf53a19905c5e31743901c31ec72e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 7 Aug 2021 20:05:21 +0200 Subject: [PATCH] Move list of investments tests to component tests In general, slow system tests requiring no interaction from the user are good candidates to be moved to component tests because component tests are much faster. In this case, the system tests were also updating the database after starting the browser, which might cause concurrency issues. We could split the test and have one system test per phase, but IMHO there's no need. We're still having a couple of system tests for the happy path, in order to make sure users actually see the list of investments. --- .../investments_list_component_spec.rb | 96 ++++++++++++++++++- spec/system/budgets/budgets_spec.rb | 82 +--------------- 2 files changed, 98 insertions(+), 80 deletions(-) diff --git a/spec/components/budgets/investments_list_component_spec.rb b/spec/components/budgets/investments_list_component_spec.rb index e167b24b9..60717acd8 100644 --- a/spec/components/budgets/investments_list_component_spec.rb +++ b/spec/components/budgets/investments_list_component_spec.rb @@ -1,10 +1,15 @@ require "rails_helper" describe Budgets::InvestmentsListComponent, type: :component do + include Rails.application.routes.url_helpers + + let(:budget) { create(:budget, :accepting) } + let(:group) { create(:budget_group, budget: budget) } + let(:heading) { create(:budget_heading, group: group) } + + before { allow(controller).to receive(:current_user).and_return(nil) } + describe "#investments" 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) } @@ -60,4 +65,89 @@ describe Budgets::InvestmentsListComponent, type: :component do end end end + + describe "investment list" do + before { create_list(:budget_investment, 3, :selected, heading: heading, price: 999) } + + it "is not shown in the informing or finished phases" do + %w[informing finished].each do |phase_name| + budget.phase = phase_name + + render_inline Budgets::InvestmentsListComponent.new(budget) + + expect(page).not_to have_content "List of investments" + expect(page).not_to have_css ".investments-list" + expect(page).not_to have_css ".budget-investment" + end + end + + it "is shown without supports nor prices in the accepting phases" do + %w[accepting reviewing selecting].each do |phase_name| + budget.phase = phase_name + + render_inline Budgets::InvestmentsListComponent.new(budget) + + expect(page).to have_content "List of investments" + expect(page).not_to have_content "Supports" + expect(page).not_to have_content "Price" + end + end + + it "is shown with supports in the valuating phase" do + budget.phase = "valuating" + + render_inline Budgets::InvestmentsListComponent.new(budget) + + expect(page).to have_content "List of investments" + expect(page).to have_content "Supports", count: 3 + expect(page).not_to have_content "Price" + end + + it "is shown with prices in the balloting phases" do + %w[publishing_prices balloting reviewing_ballots].each do |phase_name| + budget.phase = phase_name + + render_inline Budgets::InvestmentsListComponent.new(budget) + + expect(page).to have_content "List of investments" + expect(page).to have_content "Price", count: 3 + expect(page).not_to have_content "Supports" + end + end + + it "is not rendered for budgets with multiple headings" do + create(:budget_heading, budget: budget) + + Budget::Phase::PHASE_KINDS.each do |phase_name| + budget.phase = phase_name + + render_inline Budgets::InvestmentsListComponent.new(budget) + + expect(page.native.inner_html).to be_empty + end + end + end + + describe "link to see all investments" do + before { create_list(:budget_investment, 3, :selected, heading: heading, price: 999) } + + it "is not shown in the informing phase" do + budget.phase = "informing" + + render_inline Budgets::InvestmentsListComponent.new(budget) + + expect(page).not_to have_link "See all investments" + end + + it "is shown in all other phases" do + (Budget::Phase::PHASE_KINDS - ["informing"]).each do |phase_name| + budget.phase = phase_name + + render_inline Budgets::InvestmentsListComponent.new(budget) + + expect(page).to have_link "See all investments", + href: budget_investments_path(budget) + end + end + end end diff --git a/spec/system/budgets/budgets_spec.rb b/spec/system/budgets/budgets_spec.rb index 8b9ba64f4..8d393f668 100644 --- a/spec/system/budgets/budgets_spec.rb +++ b/spec/system/budgets/budgets_spec.rb @@ -301,107 +301,35 @@ describe "Budgets" do expect(page).to have_link "See results" end - scenario "Show link to see all investments" do - budget = create(:budget) - group = create(:budget_group, budget: budget) - heading = create(:budget_heading, group: group) - - create_list(:budget_investment, 3, :selected, heading: heading, price: 999) - - budget.update!(phase: "informing") - - visit budget_path(budget) - expect(page).not_to have_link "See all investments" - - (Budget::Phase::PHASE_KINDS - ["informing"]).each do |phase_name| - budget.update!(phase: phase_name) - - visit budget_path(budget) - expect(page).to have_link "See all investments", - href: budget_investments_path(budget) - end - end - scenario "Show investments list" do - budget = create(:budget) + budget = create(:budget, phase: "balloting") group = create(:budget_group, budget: budget) heading = create(:budget_heading, group: group) create_list(:budget_investment, 3, :selected, heading: heading, price: 999) - %w[informing finished].each do |phase_name| - budget.update!(phase: phase_name) - - visit budget_path(budget) - - expect(page).not_to have_content "List of investments" - expect(page).not_to have_css ".investments-list" - expect(page).not_to have_css ".budget-investment" - end - - %w[accepting reviewing selecting].each do |phase_name| - budget.update!(phase: phase_name) - - visit budget_path(budget) - - within(".investments-list") do - expect(page).to have_content "List of investments" - expect(page).not_to have_content "SUPPORTS" - expect(page).not_to have_content "PRICE" - end - end - - budget.update!(phase: "valuating") - visit budget_path(budget) within(".investments-list") do expect(page).to have_content "List of investments" - expect(page).to have_content("SUPPORTS", count: 3) - expect(page).not_to have_content "PRICE" + expect(page).to have_content "PRICE", count: 3 end - %w[publishing_prices balloting reviewing_ballots].each do |phase_name| - budget.update!(phase: phase_name) - - visit budget_path(budget) - - within(".investments-list") do - expect(page).to have_content "List of investments" - expect(page).to have_content("PRICE", count: 3) - end - end + expect(page).to have_link "See all investments", + href: budget_investments_path(budget) end scenario "Do not show investments list when budget has multiple headings" do - budget = create(:budget) + budget = create(:budget, phase: "accepting") group = create(:budget_group, budget: budget) heading_1 = create(:budget_heading, group: group) create(:budget_heading, group: group) create_list(:budget_investment, 3, :selected, heading: heading_1, price: 999) - %w[accepting reviewing selecting].each do |phase_name| - budget.update!(phase: phase_name) - - visit budget_path(budget) - - expect(page).not_to have_css ".investments-list" - end - - budget.update!(phase: "valuating") - visit budget_path(budget) expect(page).not_to have_css ".investments-list" - - %w[publishing_prices balloting reviewing_ballots].each do |phase_name| - budget.update!(phase: phase_name) - - visit budget_path(budget) - - expect(page).not_to have_css ".investments-list" - end end scenario "Show supports info on selecting phase" do