FEATURE: Pie chart option for poll results (#8352)

This commit is contained in:
Mark VanLandingham
2019-11-25 11:51:01 -06:00
committed by GitHub
parent 720101b3ee
commit b92a8131c0
23 changed files with 947 additions and 47 deletions

View File

@ -73,7 +73,7 @@ describe "DiscoursePoll endpoints" do
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
}
expect(response.status).to eq(422)
expect(response.body).to include('post_id is invalid')
expect(response.body).to include('post_id')
end
end
@ -88,7 +88,7 @@ describe "DiscoursePoll endpoints" do
it 'should raise the right error' do
get "/polls/voters.json", params: { post_id: post.id, poll_name: 'wrongpoll' }
expect(response.status).to eq(422)
expect(response.body).to include('poll_name is invalid')
expect(response.body).to include('poll_name')
end
end
@ -119,4 +119,71 @@ describe "DiscoursePoll endpoints" do
end
end
end
describe "#grouped_poll_results" do
fab!(:user1) { Fabricate(:user) }
fab!(:user2) { Fabricate(:user) }
fab!(:user3) { Fabricate(:user) }
fab!(:user4) { Fabricate(:user) }
fab!(:post) { Fabricate(:post, raw: "[poll public=true]\n- A\n- B\n[/poll]") }
let(:option_a) { "5c24fc1df56d764b550ceae1b9319125" }
let(:option_b) { "e89dec30bbd9bf50fabf6a05b4324edf" }
before do
user_votes = {
user_0: option_a,
user_1: option_a,
user_2: option_b,
}
[user1, user2, user3].each_with_index do |user, index|
DiscoursePoll::Poll.vote(
post.id,
DiscoursePoll::DEFAULT_POLL_NAME,
[user_votes["user_#{index}".to_sym]],
user
)
UserCustomField.create(user_id: user.id, name: "something", value: "value#{index}")
end
# Add another user to one of the fields to prove it groups users properly
DiscoursePoll::Poll.vote(
post.id,
DiscoursePoll::DEFAULT_POLL_NAME,
[option_a, option_b],
user4
)
UserCustomField.create(user_id: user4.id, name: "something", value: "value1")
end
it "returns grouped poll results based on user field" do
SiteSetting.poll_groupable_user_fields = "something"
get "/polls/grouped_poll_results.json", params: {
post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
user_field_name: "something"
}
expect(response.status).to eq(200)
expect(JSON.parse(response.body).deep_symbolize_keys).to eq(
grouped_results: [
{ group: "Value0", options: [{ digest: option_a, html: "A", votes: 1 }, { digest: option_b, html: "B", votes: 0 }] },
{ group: "Value1", options: [{ digest: option_a, html: "A", votes: 2 }, { digest: option_b, html: "B", votes: 1 }] },
{ group: "Value2", options: [{ digest: option_a, html: "A", votes: 0 }, { digest: option_b, html: "B", votes: 1 }] },
]
)
end
it "returns an error when poll_groupable_user_fields is empty" do
SiteSetting.poll_groupable_user_fields = ""
get "/polls/grouped_poll_results.json", params: {
post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
user_field_name: "something"
}
expect(response.status).to eq(422)
expect(response.body).to include('user_field_name')
end
end
end