discourse/plugins/discourse-post-voting/spec/system/post_voting_spec.rb
Régis HANOL fd0ddd5260
FIX: negative voter count in post voting who-voted popup (#39313)
When a post in a post-voting topic had both upvotes and downvotes,
clicking the vote count to view the voter list could display a "and -N
more users..." label with a negative number.

The root cause was that `calcRemainingCount` used the post's
`post_voting_vote_count` (the net score, i.e. upvotes − downvotes)
instead of the actual total number of voters. With 2 upvotes and 3
downvotes, net score is −1, minus 5 displayed voters = −6. Since −6 is
truthy in JS, the label would render.

The fix adds `total_voters_count` to the `/post_voting/voters` JSON
response so the client has a reliable total to subtract from. The
component now stores this value and uses it in `calcRemainingCount` with
a `Math.max(0, ...)` guard.

Also removes a no-op `{{#if whoVoted}}` template guard that referenced
the imported function (always truthy) instead of a result, and fixes the
voter popup z-index so it renders above subsequent posts.

Ref - t/181822
2026-04-17 16:43:38 +10:00

75 lines
2.5 KiB
Ruby

# frozen_string_literal: true
RSpec.describe "Post voting" do
fab!(:admin) { Fabricate(:admin, refresh_auto_groups: true) }
fab!(:user1) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:user2) { Fabricate(:user, refresh_auto_groups: true) }
let(:topic_page) { PageObjects::Pages::PostVotingTopic.new }
it "does not display button to add a comment and does not show comments when `post_voting_comment_enabled` site setting is false" do
SiteSetting.post_voting_comment_enabled = false
SiteSetting.post_voting_enabled = true
topic = Fabricate(:topic, subtype: Topic::POST_VOTING_SUBTYPE, user: user2)
post_1 = Fabricate(:post, topic: topic)
post_2 = Fabricate(:post, topic: topic)
sign_in(user1)
topic_page.visit_topic(topic)
expect(topic_page).to have_no_comment_menu
end
it "does not show a negative remaining voters label when downvotes outnumber upvotes" do
SiteSetting.post_voting_enabled = true
topic = Fabricate(:topic, subtype: Topic::POST_VOTING_SUBTYPE, user: admin)
post_1 = Fabricate(:post, topic: topic, user: admin)
answer = Fabricate(:post, topic: topic, user: admin)
user3 = Fabricate(:user, refresh_auto_groups: true)
Fabricate(:post_voting_vote, votable: answer, user: user1)
Fabricate(
:post_voting_vote,
votable: answer,
user: user2,
direction: PostVotingVote.directions[:down],
)
Fabricate(
:post_voting_vote,
votable: answer,
user: user3,
direction: PostVotingVote.directions[:down],
)
sign_in(admin)
topic_page.visit_topic(topic)
topic_page.click_vote_count(answer)
expect(topic_page).to have_no_remaining_voters_label
end
it "disallows voting on archived or closed topics" do
SiteSetting.post_voting_enabled = true
topic = Fabricate(:topic, subtype: Topic::POST_VOTING_SUBTYPE, user: user2, archived: true)
Fabricate(:post, topic: topic, post_number: 2)
post = Fabricate(:post, topic: topic, post_number: 3)
Fabricate(:post_voting_comment, post: post, user: user1)
sign_in(user1)
topic_page.visit_topic(topic)
expect(page).to have_css(PageObjects::Pages::PostVotingTopic::POST_VOTE_BUTTON, count: 4)
expect(page).to have_css(
"#{PageObjects::Pages::PostVotingTopic::POST_VOTE_BUTTON}:disabled",
count: 4,
)
expect(page).to have_css("#{PageObjects::Pages::PostVotingTopic::COMMENT_VOTE_BUTTON}:disabled")
expect(page).to have_no_css(PageObjects::Pages::PostVotingTopic::COMMENT_ACTIONS, visible: :all)
end
end