2020-11-24 16:19:06 -05:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
def serialize_option(option, user)
|
|
|
|
|
PollOptionSerializer.new(
|
|
|
|
|
option,
|
|
|
|
|
root: false,
|
|
|
|
|
scope: { can_see_results: poll.can_see_results?(user) }
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
|
RSpec.describe PollOptionSerializer do
|
2020-11-24 16:19:06 -05:00
|
|
|
|
let(:voter) { Fabricate(:user) }
|
|
|
|
|
let(:poll) { post.polls.first }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
poll.poll_votes.create!(poll_option_id: poll.poll_options.first.id, user_id: voter.id)
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
context 'when poll results are public' do
|
2020-11-24 16:19:06 -05:00
|
|
|
|
let(:post) { Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]") }
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
context 'when user is not staff' do
|
2020-11-24 16:19:06 -05:00
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
|
|
it 'include votes' do
|
|
|
|
|
serializer = serialize_option(poll.poll_options.first, user)
|
|
|
|
|
|
|
|
|
|
expect(serializer.include_votes?).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
context 'when poll results are staff only' do
|
2020-11-24 16:19:06 -05:00
|
|
|
|
let(:post) { Fabricate(:post, raw: "[poll results=staff_only]\n- A\n- B\n[/poll]") }
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
context 'when user is not staff' do
|
2020-11-24 16:19:06 -05:00
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
|
|
it 'doesn’t include votes' do
|
|
|
|
|
serializer = serialize_option(poll.poll_options.first, user)
|
|
|
|
|
|
|
|
|
|
expect(serializer.include_votes?).to eq(false)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
context 'when user is staff' do
|
2020-11-24 16:19:06 -05:00
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
|
|
it 'includes votes' do
|
|
|
|
|
serializer = serialize_option(poll.poll_options.first, admin)
|
|
|
|
|
|
|
|
|
|
expect(serializer.include_votes?).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|