Files
grecia/spec/components/admin/stats/budget_balloting_component_spec.rb
Javi Martín dd28163be7 Show admin heading stats for the current budget
To get the heading where a user voted, we were relying on the
`balloted_heading_id` field.

Our guess is this was done so the total number of users is the same as
the sum of users who voted on a heading. That is, if 2000 people voted
just on the "All city" heading, 1000 voted just on the "North district"
heading, and 500 people voted on both, instead of showing "3500 people
voted in total, 2500 voted in all city, 1500 voted in north district",
we show something like "3500 people voted in total, 2250 voted in all
city, and 1250 voted in north district".

However, this approach has some disadvantages.

The first disadvantage is, the stats aren't correct. In the case above,
2500 voted on the "All city heading", so the statistics for this heading
don't show reality.

The second one is we weren't considering the last heading where users
voted inside the budget being displayed, but the last heading where
users voted, period. That means that, if all the people above voted on a
later budget, the stats for the budget above would become "3500 people
voted in total, 0 voted in all city, and 0 voted in north district".
That also means we were including headings from previous budgets in the
statistics for more recent budgets when people hadn't voted on the
recent ones.

So we're removing the `balloted_heading_id` since its data is lost once
people vote on a new budget. And, in order to show the right stats and
simplify the code, we're no longer trying to add votes just to one
heading when users vote on several headings.

Co-Authored-By: Julian Nicolas Herrero <microweb10@gmail.com>
2023-02-20 14:21:03 +01:00

81 lines
3.4 KiB
Ruby

require "rails_helper"
describe Admin::Stats::BudgetBallotingComponent do
let(:budget) { create(:budget, :balloting) }
let(:heading) { create(:budget_heading, budget: budget, name: "Main heading") }
let(:investment) { create(:budget_investment, :feasible, :selected, heading: heading) }
it "shows the number of votes in investment projects" do
investment_2 = create(:budget_investment, :feasible, :selected, budget: budget)
create(:user, ballot_lines: [investment, investment_2])
create(:user, ballot_lines: [investment_2])
render_inline Admin::Stats::BudgetBallotingComponent.new(budget)
expect(page).to have_css "p", exact_text: "Votes 3", normalize_ws: true
end
it "shows the number of users that have voted a investment project" do
create(:user, ballot_lines: [investment])
create(:user, ballot_lines: [investment])
create(:user)
render_inline Admin::Stats::BudgetBallotingComponent.new(budget)
expect(page).to have_css "p", exact_text: "Participants 2", normalize_ws: true
end
it "does not show participants per district from old budgets" do
old_budget = create(:budget, :balloting)
old_heading = create(:budget_heading, budget: old_budget, name: "Old Heading")
old_investment = create(:budget_investment, :feasible, :selected, heading: old_heading)
create(:user, ballot_lines: [old_investment])
create(:user, ballot_lines: [old_investment])
create(:user, ballot_lines: [investment])
render_inline Admin::Stats::BudgetBallotingComponent.new(budget)
expect(page).to have_css "p", exact_text: "Votes 1", normalize_ws: true
expect(page).to have_css "p", exact_text: "Participants 1", normalize_ws: true
expect(page).not_to have_content "Old Heading"
end
it "keeps votes on old budgets when there are new votes" do
old_budget = create(:budget, :balloting)
old_heading = create(:budget_heading, budget: old_budget, name: "Old Heading")
old_investment = create(:budget_investment, :feasible, :selected, heading: old_heading)
user = create(:user)
create(:budget_ballot, user: user, budget: old_budget, investments: [old_investment])
create(:budget_ballot, user: user, budget: budget, investments: [investment])
render_inline Admin::Stats::BudgetBallotingComponent.new(old_budget)
expect(page).to have_css "p", exact_text: "Votes 1", normalize_ws: true
expect(page).to have_css "p", exact_text: "Participants 1", normalize_ws: true
page.find ".user-count-by-heading tbody" do |table_body|
expect(table_body).to have_css "tr", exact_text: "Old Heading 1", normalize_ws: true
end
end
it "counts a participant in all headings where the participant voted" do
another_heading = create(:budget_heading, budget: budget, name: "Another heading")
another_investment = create(:budget_investment, :selected, heading: another_heading)
create(:user, ballot_lines: [investment, another_investment])
render_inline Admin::Stats::BudgetBallotingComponent.new(budget)
expect(page).to have_css "p", exact_text: "Votes 2", normalize_ws: true
expect(page).to have_css "p", exact_text: "Participants 1", normalize_ws: true
page.find ".user-count-by-heading tbody" do |table_body|
expect(table_body).to have_css "tr", exact_text: "Main heading 1", normalize_ws: true
expect(table_body).to have_css "tr", exact_text: "Another heading 1", normalize_ws: true
end
end
end