-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
budgets_spec.rb
307 lines (226 loc) · 9.96 KB
/
budgets_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
require "rails_helper"
feature "Admin budgets" do
background do
admin = create(:administrator)
login_as(admin.user)
end
it_behaves_like "translatable",
"budget",
"edit_admin_budget_path",
%w[name]
context "Feature flag" do
background do
Setting["process.budgets"] = nil
end
scenario "Disabled with a feature flag" do
expect{ visit admin_budgets_path }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
context "Index" do
scenario "Displaying no open budgets text" do
visit admin_budgets_path
expect(page).to have_content("There are no budgets.")
end
scenario "Displaying budgets" do
budget = create(:budget)
visit admin_budgets_path
expect(page).to have_content(budget.name)
expect(page).to have_content(translated_phase_name(phase_kind: budget.phase))
end
scenario "Filters by phase" do
drafting_budget = create(:budget, :drafting)
accepting_budget = create(:budget, :accepting)
selecting_budget = create(:budget, :selecting)
balloting_budget = create(:budget, :balloting)
finished_budget = create(:budget, :finished)
visit admin_budgets_path
expect(page).to have_content(drafting_budget.name)
expect(page).to have_content(accepting_budget.name)
expect(page).to have_content(selecting_budget.name)
expect(page).to have_content(balloting_budget.name)
expect(page).not_to have_content(finished_budget.name)
click_link "Finished"
expect(page).not_to have_content(drafting_budget.name)
expect(page).not_to have_content(accepting_budget.name)
expect(page).not_to have_content(selecting_budget.name)
expect(page).not_to have_content(balloting_budget.name)
expect(page).to have_content(finished_budget.name)
click_link "Open"
expect(page).to have_content(drafting_budget.name)
expect(page).to have_content(accepting_budget.name)
expect(page).to have_content(selecting_budget.name)
expect(page).to have_content(balloting_budget.name)
expect(page).not_to have_content(finished_budget.name)
end
scenario "Open filter is properly highlighted" do
filters_links = {"current" => "Open", "finished" => "Finished"}
visit admin_budgets_path
expect(page).not_to have_link(filters_links.values.first)
filters_links.keys.drop(1).each { |filter| expect(page).to have_link(filters_links[filter]) }
filters_links.each_pair do |current_filter, link|
visit admin_budgets_path(filter: current_filter)
expect(page).not_to have_link(link)
(filters_links.keys - [current_filter]).each do |filter|
expect(page).to have_link(filters_links[filter])
end
end
end
end
context "New" do
scenario "Create budget" do
visit admin_budgets_path
click_link "Create new budget"
fill_in "Name", with: "M30 - Summer campaign"
select "Accepting projects", from: "budget[phase]"
click_button "Create Budget"
expect(page).to have_content "New participatory budget created successfully!"
expect(page).to have_content "M30 - Summer campaign"
end
scenario "Name is mandatory" do
visit new_admin_budget_path
click_button "Create Budget"
expect(page).not_to have_content "New participatory budget created successfully!"
expect(page).to have_css("label.error", text: "Name")
end
scenario "Name should be unique" do
create(:budget, name: "Existing Name")
visit new_admin_budget_path
fill_in "Name", with: "Existing Name"
click_button "Create Budget"
expect(page).not_to have_content "New participatory budget created successfully!"
expect(page).to have_css("label.error", text: "Name")
expect(page).to have_css("small.error", text: "has already been taken")
end
end
context "Destroy" do
let!(:budget) { create(:budget) }
let(:heading) { create(:budget_heading, group: create(:budget_group, budget: budget)) }
scenario "Destroy a budget without investments" do
visit admin_budgets_path
click_link "Edit budget"
click_link "Delete budget"
expect(page).to have_content("Budget deleted successfully")
expect(page).to have_content("There are no budgets.")
end
scenario "Try to destroy a budget with investments" do
create(:budget_investment, heading: heading)
visit admin_budgets_path
click_link "Edit budget"
click_link "Delete budget"
expect(page).to have_content("You cannot destroy a budget that has associated investments")
expect(page).to have_content("There is 1 budget")
end
scenario "Try to destroy a budget with polls" do
create(:poll, budget: budget)
visit edit_admin_budget_path(budget)
click_link "Delete budget"
expect(page).to have_content("You cannot destroy a budget that has an associated poll")
expect(page).to have_content("There is 1 budget")
end
end
context "Edit" do
let!(:budget) { create(:budget) }
scenario "Show phases table" do
budget.update(phase: "selecting")
visit admin_budgets_path
click_link "Edit budget"
expect(page).to have_select("budget_phase", selected: "Selecting projects")
within "#budget-phases-table" do
Budget::Phase::PHASE_KINDS.each do |phase_kind|
phase_index = Budget::Phase::PHASE_KINDS.index(phase_kind)
break if phase_kind == Budget::Phase::PHASE_KINDS.last
next_phase_kind = Budget::Phase::PHASE_KINDS[phase_index + 1]
next_phase_name = translated_phase_name(phase_kind: next_phase_kind)
expect(translated_phase_name(phase_kind: phase_kind)).to appear_before(next_phase_name)
end
budget.phases.each do |phase|
edit_phase_link = edit_admin_budget_budget_phase_path(budget, phase)
within "#budget_phase_#{phase.id}" do
expect(page).to have_content(translated_phase_name(phase_kind: phase.kind))
expect(page).to have_content("#{phase.starts_at.to_date} - #{phase.ends_at.to_date}")
expect(page).to have_css(".budget-phase-enabled.enabled")
expect(page).to have_link("Edit phase", href: edit_phase_link)
expect(page).to have_content("Active") if budget.current_phase == phase
end
end
end
end
scenario "Changing name for current locale will update the slug if budget is in draft phase", :js do
budget.update(phase: "drafting")
old_slug = budget.slug
visit edit_admin_budget_path(budget)
select "Español", from: "translation_locale"
fill_in "Name", with: "Spanish name"
click_button "Update Budget"
expect(page).to have_content "Participatory budget updated successfully"
expect(budget.reload.slug).to eq old_slug
visit edit_admin_budget_path(budget)
click_link "English"
fill_in "Name", with: "New English Name"
click_button "Update Budget"
expect(page).to have_content "Participatory budget updated successfully"
expect(budget.reload.slug).not_to eq old_slug
expect(budget.slug).to eq "new-english-name"
end
end
context "Update" do
background do
create(:budget)
end
scenario "Update budget" do
visit admin_budgets_path
click_link "Edit budget"
fill_in "Name", with: "More trees on the streets"
click_button "Update Budget"
expect(page).to have_content("More trees on the streets")
expect(page).to have_current_path(admin_budgets_path)
end
end
context "Calculate Budget's Winner Investments" do
scenario "For a Budget in reviewing balloting", :js do
budget = create(:budget, phase: "reviewing_ballots")
group = create(:budget_group, budget: budget)
heading = create(:budget_heading, group: group, price: 4)
unselected = create(:budget_investment, :unselected, heading: heading, price: 1,
ballot_lines_count: 3)
winner = create(:budget_investment, :selected, heading: heading, price: 3,
ballot_lines_count: 2)
selected = create(:budget_investment, :selected, heading: heading, price: 2, ballot_lines_count: 1)
visit edit_admin_budget_path(budget)
expect(page).not_to have_content "See results"
click_link "Calculate Winner Investments"
expect(page).to have_content "Winners being calculated, it may take a minute."
expect(page).to have_content winner.title
expect(page).not_to have_content unselected.title
expect(page).not_to have_content selected.title
visit edit_admin_budget_path(budget)
expect(page).to have_content "See results"
end
scenario "For a finished Budget" do
budget = create(:budget, phase: "finished")
allow_any_instance_of(Budget).to receive(:has_winning_investments?).and_return(true)
visit edit_admin_budget_path(budget)
expect(page).to have_content "Calculate Winner Investments"
expect(page).to have_content "See results"
end
scenario "Recalculate for a finished Budget" do
budget = create(:budget, phase: "finished")
group = create(:budget_group, budget: budget)
heading = create(:budget_heading, group: group)
create(:budget_investment, :winner, heading: heading)
visit edit_admin_budget_path(budget)
expect(page).to have_content "Recalculate Winner Investments"
expect(page).to have_content "See results"
expect(page).not_to have_content "Calculate Winner Investments"
visit admin_budget_budget_investments_path(budget)
click_link "Advanced filters"
check "Winners"
click_button "Filter"
expect(page).to have_content "Recalculate Winner Investments"
expect(page).not_to have_content "Calculate Winner Investments"
end
end
end
def translated_phase_name(phase_kind: kind)
I18n.t("budgets.phase.#{phase_kind}")
end