2023-02-21 19:55:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-03 16:18:23 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-09-04 01:12:25 -04:00
|
|
|
RSpec.describe Poll do
|
2024-10-15 08:48:10 -04:00
|
|
|
describe 'Scopes' do
|
2023-03-04 11:16:45 -05:00
|
|
|
let(:status) { Fabricate(:status) }
|
|
|
|
let(:attached_poll) { Fabricate(:poll, status: status) }
|
|
|
|
let(:not_attached_poll) do
|
|
|
|
Fabricate(:poll).tap do |poll|
|
|
|
|
poll.status = nil
|
|
|
|
poll.save(validate: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-15 08:48:10 -04:00
|
|
|
describe '.attached' do
|
2023-03-04 11:16:45 -05:00
|
|
|
it 'finds the correct records' do
|
|
|
|
results = described_class.attached
|
|
|
|
|
|
|
|
expect(results).to eq([attached_poll])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-15 08:48:10 -04:00
|
|
|
describe '.unattached' do
|
2023-03-04 11:16:45 -05:00
|
|
|
it 'finds the correct records' do
|
|
|
|
results = described_class.unattached
|
|
|
|
|
|
|
|
expect(results).to eq([not_attached_poll])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-10-17 08:56:24 -04:00
|
|
|
|
2024-10-15 08:48:10 -04:00
|
|
|
describe '#reset_votes!' do
|
|
|
|
let(:poll) { Fabricate :poll, cached_tallies: [2, 3], votes_count: 5, voters_count: 5 }
|
|
|
|
let!(:vote) { Fabricate :poll_vote, poll: }
|
|
|
|
|
|
|
|
it 'resets vote data and deletes votes' do
|
|
|
|
expect { poll.reset_votes! }
|
|
|
|
.to change(poll, :cached_tallies).to([0, 0])
|
|
|
|
.and change(poll, :votes_count).to(0)
|
|
|
|
.and(change(poll, :voters_count).to(0))
|
|
|
|
expect { vote.reload }
|
|
|
|
.to raise_error(ActiveRecord::RecordNotFound)
|
2023-10-17 08:56:24 -04:00
|
|
|
end
|
|
|
|
end
|
2024-10-15 08:48:10 -04:00
|
|
|
|
|
|
|
describe 'Validations' do
|
|
|
|
subject { Fabricate.build(:poll) }
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:expires_at) }
|
|
|
|
end
|
2019-03-03 16:18:23 -05:00
|
|
|
end
|