Skip to content

Commit

Permalink
feat: Add missing specs about budget's component
Browse files Browse the repository at this point in the history
  • Loading branch information
AyakorK committed Dec 27, 2024
1 parent 434a45b commit a2aace0
Show file tree
Hide file tree
Showing 3 changed files with 345 additions and 7 deletions.
6 changes: 3 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ en:
budgets:
settings:
global:
default_sort_order: Default Projects sorting
default_sort_order_help: Default means that if the supports are enabled, the projects will be shown sorted by random, and if the supports are blocked, then they will be sorted by the most supported.
default_sort_order: Default projects sorting
default_sort_order_help: Default means that if the supports are disabled, the projects will be shown sorted by random, and if the supports are enabled, then they will be sorted by the most supported.
default_sort_order_options:
alphabetical: A-Z (Alphabetical)
default: Default
Expand All @@ -107,7 +107,7 @@ en:
most_voted: Most supported
random: Random
step:
default_sort_order: Default Projects sorting
default_sort_order: Default projects sorting
default_sort_order_help: Default means that if the supports are enabled, the projects will be shown sorted by random, and if the supports are blocked, then they will be sorted by the most supported.
default_sort_order_options:
alphabetical: A-Z (Alphabetical)
Expand Down
8 changes: 4 additions & 4 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ fr:
budgets:
settings:
global:
default_sort_order: Default Projects sorting
default_sort_order_help: Default means that if the supports are enabled, the projects will be shown sorted by random, and if the supports are blocked, then they will be sorted by the most supported.
default_sort_order: Tri des projets par défaut
default_sort_order_help: Par défaut signifie que si les votes sont désactivés, les propositions seront affichées par ordre aléatoire, et si les votes sont activés, elles seront triées par ordre décroissant des votes.
default_sort_order_options:
alphabetical: A-Z (Alphabétique)
default: Par défaut
Expand All @@ -109,8 +109,8 @@ fr:
most_voted: Les plus votés
random: Ordre aléatoire
step:
default_sort_order: Default Projects sorting
default_sort_order_help: Default means that if the supports are enabled, the projects will be shown sorted by random, and if the supports are blocked, then they will be sorted by the most supported.
default_sort_order: Tri des projets par défaut
default_sort_order_help: Par défaut signifie que si les votes sont désactivés, les propositions seront affichées par ordre aléatoire, et si les votes sont activés, elles seront triées par ordre décroissant des votes.
default_sort_order_options:
alphabetical: A-Z (alphabétique)
default: Par défaut
Expand Down
338 changes: 338 additions & 0 deletions spec/lib/decidim/budgets/admin/component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,338 @@
# frozen_string_literal: true

require "spec_helper"

describe "Budgets component" do # rubocop:disable RSpec/DescribeClass
let!(:component) { create(:budgets_component) }
let(:organization) { component.organization }
let!(:current_user) { create(:user, :confirmed, :admin, organization: organization) }

let(:possible_orders) { %w(default random most_voted alphabetical highest_cost lowest_cost) }

describe "on update" do
let(:manifest) { component.manifest }
let(:participatory_space) { component.participatory_space }

let(:form) do
Decidim::Budgets::Admin::ComponentForm.from_params(
id: component.id,
weight: 0,
manifest: manifest,
participatory_space: participatory_space,
name: generate_localized_title,
default_step_settings: {},
settings: new_settings(:global, settings)
).with_context(current_organization: organization)
end

let(:percent_enabled) { false }
let(:percent) { 70 }
let(:minimum_enabled) { false }
let(:projects_enabled) { false }
let(:geocoding_enabled) { false }
let(:minimum_number) { 3 }
let(:maximum_number) { 6 }

let(:settings) do
{
total_budget: 100_000_000,
vote_rule_threshold_percent_enabled: percent_enabled,
vote_threshold_percent: percent,
vote_rule_minimum_budget_projects_enabled: minimum_enabled,
vote_minimum_budget_projects_number: minimum_number,
vote_rule_selected_projects_enabled: projects_enabled,
vote_selected_projects_minimum: minimum_number,
vote_selected_projects_maximum: maximum_number,
geocoding_enabled: geocoding_enabled,
default_sort_order: "default"
}
end

def new_settings(name, data)
Decidim::Component.build_settings(manifest, name, data, organization)
end

describe "with geocoding enabled" do
let(:geocoding_enabled) { true }
# One budget rule must me activated
let(:percent_enabled) { true }

it "updates the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:ok)
end
end

describe "with minimum projects number to vote" do
let(:minimum_enabled) { true }

context "when the minimum projects number is valid" do
it "updates the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:ok)
end
end

context "when the minimum projects number is NOT valid" do
let(:minimum_number) { 0 }

it "does NOT update the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:invalid)
end
end
end

describe "with projects rule enabled" do
let(:projects_enabled) { true }

context "when the projects rule is valid" do
it "updates the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:ok)
end
end

context "when the projects rule is NOT valid" do
let(:maximum_number) { 0 }

it "does NOT update the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:invalid)
end
end

context "when the maximum projects number is more than the minimum" do
let(:maximum_number) { 4 }

it "updates the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:ok)
end
end

context "when the maximum projects number is less than the minimum" do
let(:maximum_number) { 2 }

it "does NOT update the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:invalid)
end
end
end

describe "with threshold percent enabled" do
let(:percent_enabled) { true }

context "when the threshold percent number is valid" do
it "updates the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:ok)
end
end

context "when the threshold percent is NOT valid" do
let(:percent) { -1 }

it "does NOT update the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:invalid)
end
end
end

describe "with more than one voting rule enabled" do
let(:percent_enabled) { true }
let(:minimum_enabled) { true }
let(:projects_enabled) { true }

it "does NOT update the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:invalid)
end
end

describe "with no voting rule enabled" do
let(:percent_enabled) { false }
let(:minimum_enabled) { false }
let(:projects_enabled) { false }

it "does NOT update the component" do
expect do
Decidim::Admin::UpdateComponent.call(form, component, current_user)
end.to broadcast(:invalid)
end
end
end

describe "on edit", type: :system do
let(:edit_component_path) do
Decidim::EngineRouter.admin_proxy(component.participatory_space).edit_component_path(component.id)
end

before do
switch_to_host(organization.host)
login_as current_user, scope: :user
end

describe "Budget component settings" do
before do
visit edit_component_path
end

context "when default sort order is selected" do
it "shows the default sort order select" do
expect(page).to have_content("Default projects sorting")
expect(page).to have_css("select#component_settings_default_sort_order")
expect(page).to have_select("component_settings_default_sort_order", selected: "Default")
end

it "can change the default sort order" do
expect(page).not_to have_select("select#component_settings_default_sort_order", selected: "Random")
select "Random", from: "component_settings_default_sort_order"
click_button "Update"
expect(page).to have_content("The component was updated successfully")
end

it "does not show a sort that doesn't exist" do
expect(page).not_to have_select("select#component_settings_default_sort_order", selected: "Nonexistent")
end
end

context "when minimum projects rule is checked" do
before do
check "Enable rule: Minimum number of projects to be voted on"
end

it "is shown the number input" do
expect(page).to have_content("Minimum number of projects to vote")
expect(page).to have_css("input#component_settings_vote_minimum_budget_projects_number")
end

it "is hidden the percent input" do
expect(page).to have_no_content("Vote threshold percent")
expect(page).to have_no_css("input#component_settings_vote_threshold_percent")
end

it "is hidden the project rule inputs" do
expect(page).to have_no_content("Minimum amount of projects to be selected")
expect(page).to have_no_content("Maximum amount of projects to be selected")
expect(page).to have_no_css("input#component_settings_vote_selected_projects_minimum")
expect(page).to have_no_css("input#component_settings_vote_selected_projects_maximum")
end
end

context "when projects rule is checked" do
before do
check "Enable rule: Minimum and maximum number of projects to be voted on"
end

it "is shown the number input" do
expect(page).to have_content("Minimum amount of projects to be selected")
expect(page).to have_content("Maximum amount of projects to be selected")
expect(page).to have_css("input#component_settings_vote_selected_projects_minimum")
expect(page).to have_css("input#component_settings_vote_selected_projects_maximum")
end

it "is hidden the percent input" do
expect(page).to have_no_content("Vote threshold percent")
expect(page).to have_no_css("input#component_settings_vote_threshold_percent")
end

it "is hidden the number input" do
expect(page).to have_no_content("Minimum number of projects to vote")
expect(page).to have_no_css("input#component_settings_vote_minimum_budget_projects_number")
end
end

context "when threshold percent rule is checked" do
before do
check "Enable rule: Minimum budget percentage"
end

it "is shown the percent input" do
expect(page).to have_content("Vote threshold percent")
expect(page).to have_css("input#component_settings_vote_threshold_percent")
end

it "is hidden the number input" do
expect(page).to have_no_content("Minimum number of projects to vote")
expect(page).to have_no_css("input#component_settings_vote_minimum_budget_projects_number")
end

it "is hidden the project rule inputs" do
expect(page).to have_no_content("Minimum amount of projects to be selected")
expect(page).to have_no_content("Maximum amount of projects to be selected")
expect(page).to have_no_css("input#component_settings_vote_selected_projects_minimum")
expect(page).to have_no_css("input#component_settings_vote_selected_projects_maximum")
end
end
end
end

describe "component projects exporter" do
subject do
component
.manifest
.export_manifests
.find { |manifest| manifest.name == :projects }
.collection
.call(component, user)
end

let(:component) { create(:budgets_component) }
let(:component2) { create(:budgets_component) }
let(:budget) { create(:budget, component: component) }
let(:budget2) { create(:budget, component: component2) }
let(:budget3) { create(:budget, component: component2) }
let!(:components_projects) { create_list(:project, 2, budget: budget) }
let!(:another_component_projects) { create_list(:project, 3, budget: budget2) }
let!(:another_component_projects2) { create_list(:project, 4, budget: budget3) }
let(:organization) { component.participatory_space.organization }

context "when the user is an admin" do
let!(:user) { create :user, admin: true, organization: organization }

it "exports all budgets from the component" do
expect(subject.count).to eq(2)
expect(subject).to match_array(components_projects)
end
end
end

describe "budget projects exporter" do
subject do
component
.manifest
.export_manifests
.find { |manifest| manifest.name == :projects }
.collection
.call(component, user, budget1.id)
end

let(:component) { create(:budgets_component) }
let(:budget1) { create(:budget, component: component) }
let(:budget2) { create(:budget, component: component) }
let!(:budget1_projects) { create_list(:project, 3, budget: budget1) }
let!(:budget2_projects) { create_list(:project, 2, budget: budget2) }
let(:organization) { component.participatory_space.organization }

context "when the user is an admin" do
let!(:user) { create :user, admin: true, organization: organization }

it "exports projects of individual budget" do
expect(subject.count).to eq(3)
end
end
end
end

0 comments on commit a2aace0

Please sign in to comment.