poll_vote_spec.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe PollVote do
  4. describe '#object_type' do
  5. let(:poll_vote) { Fabricate.build(:poll_vote) }
  6. it 'returns :vote' do
  7. expect(poll_vote.object_type).to eq :vote
  8. end
  9. end
  10. describe 'validations' do
  11. context 'with a vote on an expired poll' do
  12. it 'marks the vote invalid' do
  13. poll = Fabricate.build(:poll, expires_at: 30.days.ago)
  14. vote = Fabricate.build(:poll_vote, poll: poll)
  15. expect(vote).to_not be_valid
  16. end
  17. end
  18. context 'with invalid choices' do
  19. it 'marks vote invalid with negative choice' do
  20. poll = Fabricate.build(:poll)
  21. vote = Fabricate.build(:poll_vote, poll: poll, choice: -100)
  22. expect(vote).to_not be_valid
  23. end
  24. it 'marks vote invalid with choice in excess of options' do
  25. poll = Fabricate.build(:poll, options: %w(a b c))
  26. vote = Fabricate.build(:poll_vote, poll: poll, choice: 10)
  27. expect(vote).to_not be_valid
  28. end
  29. end
  30. context 'with a poll where multiple is true' do
  31. it 'does not allow a second vote on same choice from same account' do
  32. poll = Fabricate(:poll, multiple: true, options: %w(a b c))
  33. first_vote = Fabricate(:poll_vote, poll: poll, choice: 1)
  34. expect(first_vote).to be_valid
  35. second_vote = Fabricate.build(:poll_vote, account: first_vote.account, poll: poll, choice: 1)
  36. expect(second_vote).to_not be_valid
  37. end
  38. end
  39. context 'with a poll where multiple is false' do
  40. it 'does not allow a second vote from same account' do
  41. poll = Fabricate(:poll, multiple: false, options: %w(a b c))
  42. first_vote = Fabricate(:poll_vote, poll: poll)
  43. expect(first_vote).to be_valid
  44. second_vote = Fabricate.build(:poll_vote, account: first_vote.account, poll: poll)
  45. expect(second_vote).to_not be_valid
  46. end
  47. end
  48. end
  49. end