Skip to content

Commit ed488a4

Browse files
committed
test: update tests
1 parent b1cf18c commit ed488a4

6 files changed

Lines changed: 507 additions & 326 deletions

File tree

spec/controller/decidim/budgets/admin/projects_controller_spec.rb

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,27 @@ module Admin
88
describe ProjectsController, type: :controller do
99
routes { Decidim::Budgets::AdminEngine.routes }
1010

11-
let(:user) { create(:user, :confirmed, :admin, organization: component.organization) }
11+
let(:organization) { create(:organization) }
12+
let(:user) { create(:user, :confirmed, :admin, organization:) }
13+
let(:participatory_space) { create(:assembly, organization:) }
14+
let(:component) { create(:budgets_component, organization:, participatory_space:) }
1215

1316
before do
14-
request.env["decidim.current_organization"] = component.organization
15-
request.env["decidim.current_participatory_space"] = component.participatory_space
17+
request.env["decidim.current_organization"] = organization
18+
request.env["decidim.current_participatory_space"] = participatory_space
1619
request.env["decidim.current_component"] = component
1720
sign_in user
1821
end
1922

2023
describe "PATCH update" do
21-
let(:component) { create(:budgets_component) }
22-
let(:project) { create(:project, component: component) }
24+
let(:taxonomy) { create(:taxonomy, :with_parent, organization:) }
25+
let(:project) { create(:project, component:, taxonomies: [taxonomy]) }
2326
let(:project_title) { project.title }
2427
let(:project_params) do
2528
{
2629
title: project_title,
2730
description: project.description,
2831
budget_amount: project.budget_amount,
29-
decidim_scope_id: project.scope&.id,
30-
decidim_category_id: project.category&.id,
3132
proposal_ids: project.linked_resources(:proposals, "included_proposals").pluck(:id),
3233
selected: project.selected?,
3334
photos: project.photos.map { |a| a.id.to_s }
@@ -37,7 +38,9 @@ module Admin
3738
{
3839
id: project.id,
3940
budget_id: project.budget.id,
40-
project: project_params
41+
project: project_params,
42+
component_id: component.id,
43+
assembly_slug: participatory_space.slug
4144
}
4245
end
4346

@@ -66,29 +69,13 @@ def proposals_picker_projects_path
6669
patch :update, params: params
6770

6871
expect(flash[:alert]).not_to be_empty
69-
expect(response).to have_http_status(:ok)
72+
expect(response).to have_http_status(:unprocessable_entity)
7073
expect(subject).to render_template(:edit)
7174
expect(response.body).to include("There was a problem updating this project")
7275
end
7376
end
7477
end
7578
end
76-
77-
context "when proposal linking is not enabled" do
78-
let(:component) { create(:budgets_component) }
79-
80-
before do
81-
allow(Decidim::Budgets).to receive(:enable_proposal_linking).and_return(false)
82-
end
83-
84-
it "does not load the proposals admin picker concern" do
85-
expect(Decidim::Budgets::Admin::ProjectsController).not_to receive(:include).with(
86-
Decidim::Proposals::Admin::Picker
87-
)
88-
89-
load "#{Decidim::Budgets::Engine.root}/app/controllers/decidim/budgets/admin/projects_controller.rb"
90-
end
91-
end
9279
end
9380
end
9481
end

spec/serializers/project_serializer_spec.rb

Lines changed: 159 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,117 +2,165 @@
22

33
require "spec_helper"
44

5-
module Decidim
6-
module Budgets
7-
describe ProjectSerializer do
8-
let(:budget) { create(:budget) }
9-
let(:serialized) { subject.run }
10-
let(:attachment) { create :attachment, attached_to: project }
11-
let(:proposals_component) { create(:component, manifest_name: "proposals", participatory_space: project.participatory_space) }
12-
let(:proposals) { create_list(:proposal, 3, component: proposals_component) }
13-
let(:category) { create(:category, participatory_space: budget.component.participatory_space) }
14-
let(:scope) { create(:scope, organization: category.participatory_space.organization) }
15-
let(:project) { create(:project, budget: budget, category: category, scope: scope) }
16-
let!(:paper_ballot_result) { create :paper_ballot_result, project: project, votes: 10 }
17-
18-
subject { described_class.new(project) }
19-
20-
describe "#serializer" do
21-
before { project.link_resources(proposals, "included_proposals") }
22-
23-
it "includes the keys" do
24-
expect(serialized.keys).to eq([:id, :category, :scope, :participatory_space, :component, :title, :description, :budget, :budget_amount, :confirmed_votes, :paper_ballots, :total_votes, :comments, :created_at, :url, :related_proposals, :related_proposal_titles, :related_proposal_urls, :test_field])
25-
end
26-
27-
it "includes the id" do
28-
expect(serialized).to include(id: project.id)
29-
end
30-
31-
it "includes the category" do
32-
expect(serialized[:category]).to include(id: category.id)
33-
expect(serialized[:category]).to include(name: category.name)
34-
end
35-
36-
it "includes the scope" do
37-
expect(serialized[:scope]).to include(id: project.scope.id)
38-
expect(serialized[:scope]).to include(name: project.scope.name)
39-
end
40-
41-
it "includes the participatory space" do
42-
expect(serialized[:participatory_space]).to include(id: project.budget.component.participatory_space.id)
43-
expect(serialized[:participatory_space]).to include(url: Decidim::ResourceLocatorPresenter.new(project.participatory_space).url)
44-
end
45-
46-
it "includes the component" do
47-
expect(serialized[:component]).to include(id: project.component.id)
48-
end
49-
50-
it "includes the title" do
51-
expect(serialized[:title]).to include(project.title)
52-
end
53-
54-
it "includes the description" do
55-
expect(serialized[:description]).to include(project.description)
56-
end
57-
58-
it "includes the budget id" do
59-
expect(serialized[:budget]).to eq(id: project.budget.id)
60-
end
61-
62-
it "includes the budget amount" do
63-
expect(serialized[:budget_amount]).to eq(project.budget_amount)
64-
end
65-
66-
it "includes count of confirmed votes" do
67-
expect(serialized[:confirmed_votes]).to eq(project.confirmed_orders_count)
68-
end
69-
70-
it "includes count of paper ballots" do
71-
expect(serialized[:paper_ballots]).to eq(project.paper_ballots)
72-
end
73-
74-
it "includes count of total votes" do
75-
expect(serialized[:total_votes]).to eq(project.total_votes)
76-
end
77-
78-
it "includes comment count" do
79-
expect(serialized[:comments]).to eq(project.comments.count)
80-
end
81-
82-
it "includes the created at" do
83-
expect(serialized).to include(created_at: project.created_at)
84-
end
85-
86-
it "includes the url" do
87-
expect(serialized[:url]).to eq(project.polymorphic_resource_url({}))
88-
end
89-
90-
it "includes related proposal ids" do
91-
expect(serialized[:related_proposals]).to match_array(project.linked_resources(:proposals,
92-
"included_proposals").map(&:id))
93-
end
94-
95-
it "includes related proposal titles" do
96-
expect(serialized[:related_proposal_titles]).to include(proposals.first.title["en"])
97-
expect(serialized[:related_proposal_titles]).to include(proposals.second.title["en"])
98-
expect(serialized[:related_proposal_titles]).to include(proposals.last.title["en"])
99-
end
100-
101-
it "includes related proposal urls" do
102-
expect(serialized[:related_proposal_urls]).to include(Decidim::ResourceLocatorPresenter.new(proposals.first).url)
103-
expect(serialized[:related_proposal_urls]).to include(Decidim::ResourceLocatorPresenter.new(proposals.second).url)
104-
expect(serialized[:related_proposal_urls]).to include(Decidim::ResourceLocatorPresenter.new(proposals.last).url)
105-
end
106-
end
107-
108-
context "when subscribed to the serialize event" do
109-
ActiveSupport::Notifications.subscribe("decidim.serialize.budgets.project_serializer") do |_event_name, data|
110-
data[:serialized_data][:test_field] = "Resource class: #{data[:resource].class}"
111-
end
112-
113-
it "includes new field" do
114-
expect(serialized[:test_field]).to eq("Resource class: Decidim::Budgets::Project")
115-
end
5+
module Decidim::Budgets
6+
describe ProjectSerializer do
7+
let(:budget) { create(:budget, component:) }
8+
let(:serialized) { subject.run }
9+
let(:attachment) { create(:attachment, attached_to: project) }
10+
let(:proposals_component) { create(:component, manifest_name: "proposals", participatory_space: project.participatory_space) }
11+
let(:proposals) { create_list(:proposal, 3, component: proposals_component) }
12+
let(:taxonomies) { create_list(:taxonomy, 2, :with_parent, organization: budget.component.organization) }
13+
let(:project) { create(:project, budget:, taxonomies:) }
14+
let(:router) { Decidim::EngineRouter.main_proxy(budget.component) }
15+
let(:component) { create(:budgets_component) }
16+
let!(:paper_ballot_result) { create :paper_ballot_result, project:, votes: 10 }
17+
let(:serialized_taxonomies) do
18+
{ ids: taxonomies.pluck(:id) }.merge(taxonomies.to_h { |t| [t.id, t.name] })
19+
end
20+
21+
subject { described_class.new(project) }
22+
23+
describe "#serializer" do
24+
before { project.link_resources(proposals, "included_proposals") }
25+
26+
it "includes the id" do
27+
expect(serialized).to include(id: project.id)
28+
end
29+
30+
it "serializes the taxonomies" do
31+
expect(serialized[:taxonomies]).to eq(serialized_taxonomies)
32+
end
33+
34+
it "includes the participatory space" do
35+
expect(serialized[:participatory_space]).to include(id: project.budget.component.participatory_space.id)
36+
expect(serialized[:participatory_space]).to include(url: Decidim::ResourceLocatorPresenter.new(project.participatory_space).url)
37+
end
38+
39+
it "includes the component" do
40+
expect(serialized[:component]).to include(id: project.component.id)
41+
end
42+
43+
it "includes the title" do
44+
expect(serialized[:title]).to include(project.title)
45+
end
46+
47+
it "includes the description" do
48+
expect(serialized[:description]).to include(project.description)
49+
end
50+
51+
it "includes the budget" do
52+
expect(serialized[:budget]).to eq(
53+
id: project.budget.id,
54+
title: project.budget.title,
55+
url: router.budget_url(project.budget)
56+
)
57+
end
58+
59+
it "includes the budget amount" do
60+
expect(serialized[:budget_amount]).to eq(project.budget_amount)
61+
end
62+
63+
it "includes comment count" do
64+
expect(serialized[:comments]).to eq(project.comments.count)
65+
end
66+
67+
it "includes count of paper ballots" do
68+
expect(serialized[:paper_ballots]).to eq(project.paper_ballots)
69+
end
70+
71+
it "includes count of total votes" do
72+
expect(serialized[:total_votes]).to eq(project.total_votes)
73+
end
74+
75+
it "includes the created at" do
76+
expect(serialized).to include(created_at: project.created_at)
77+
end
78+
79+
it "includes the url" do
80+
expect(serialized[:url]).to eq(project.polymorphic_resource_url({}))
81+
end
82+
83+
it "serializes the address" do
84+
expect(serialized).to include(address: project.address)
85+
end
86+
87+
it "includes related proposal ids" do
88+
expect(serialized[:related_proposals]).to match_array(project.linked_resources(:proposals, "included_proposals").map(&:id))
89+
end
90+
91+
it "includes related proposal titles" do
92+
expect(serialized[:related_proposal_titles]).to include(proposals.first.title["en"])
93+
expect(serialized[:related_proposal_titles]).to include(proposals.second.title["en"])
94+
expect(serialized[:related_proposal_titles]).to include(proposals.last.title["en"])
95+
end
96+
97+
it "includes related proposal urls" do
98+
expect(serialized[:related_proposal_urls]).to include(Decidim::ResourceLocatorPresenter.new(proposals.first).url)
99+
expect(serialized[:related_proposal_urls]).to include(Decidim::ResourceLocatorPresenter.new(proposals.second).url)
100+
expect(serialized[:related_proposal_urls]).to include(Decidim::ResourceLocatorPresenter.new(proposals.last).url)
101+
end
102+
103+
it "includes the updated date" do
104+
expect(serialized).to include(updated_at: project.updated_at)
105+
end
106+
107+
it "includes the follows count" do
108+
expect(serialized).to include(follows_count: project.follows_count)
109+
end
110+
111+
it "includes the selected date" do
112+
expect(serialized).to include(selected_at: project.selected_at)
113+
end
114+
115+
it "serializes the reference" do
116+
expect(serialized).to include(reference: project.reference)
117+
end
118+
119+
it "serializes the latitude" do
120+
expect(serialized).to include(latitude: project.latitude)
121+
end
122+
123+
it "serializes the longitude" do
124+
expect(serialized).to include(longitude: project.longitude)
125+
end
126+
end
127+
128+
context "when subscribed to the serialize event" do
129+
before do
130+
I18n.backend.store_translations(
131+
:en,
132+
decidim: {
133+
open_data: {
134+
help: {
135+
projects: {
136+
test_field: "Test field for projects serializer subscription"
137+
}
138+
}
139+
}
140+
}
141+
)
142+
end
143+
144+
ActiveSupport::Notifications.subscribe("decidim.serialize.budgets.project_serializer") do |_event_name, data|
145+
data[:serialized_data][:test_field] = "Resource class: #{data[:resource].class}"
146+
end
147+
148+
it "includes new field" do
149+
expect(serialized[:test_field]).to eq("Resource class: Decidim::Budgets::Project")
150+
end
151+
end
152+
153+
context "when show votes is disabled" do
154+
it "does not include count of confirmed votes" do
155+
expect(serialized[:confirmed_votes]).to be_nil
156+
end
157+
end
158+
159+
context "when show votes is enabled" do
160+
let(:component) { create(:budgets_component, :with_show_votes_enabled) }
161+
162+
it "includes count of confirmed votes" do
163+
expect(serialized[:confirmed_votes]).to eq(project.confirmed_orders_count)
116164
end
117165
end
118166
end

spec/shared/import_proposals_to_projects_examples.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
include Decidim::ComponentPathHelper
99

1010
it "imports proposals from one component to a budget component" do
11+
click_button "Import"
1112
click_link "Import proposals to projects"
1213

1314
within ".import_proposals" do
1415
select origin_component.name["en"], from: :proposals_import_origin_component_id
1516
fill_in "Default budget", with: default_budget
16-
check :proposals_import_import_all_accepted_proposals
17+
check "Accepted" || "Rejected"
1718
end
1819

1920
click_button "Import proposals to projects"

0 commit comments

Comments
 (0)