|
2 | 2 |
|
3 | 3 | require "spec_helper" |
4 | 4 |
|
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) |
116 | 164 | end |
117 | 165 | end |
118 | 166 | end |
|
0 commit comments