2016-04-25 14:10:20 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-25 08:28:55 -04:00
|
|
|
describe Awardable do
|
2016-04-25 14:10:20 -04:00
|
|
|
let!(:issue) { create(:issue) }
|
|
|
|
let!(:award_emoji) { create(:award_emoji, :downvote, awardable: issue) }
|
|
|
|
|
|
|
|
describe "Associations" do
|
2017-04-25 08:28:55 -04:00
|
|
|
subject { build(:issue) }
|
|
|
|
|
2016-04-25 14:10:20 -04:00
|
|
|
it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "ClassMethods" do
|
|
|
|
let!(:issue2) { create(:issue) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:award_emoji, awardable: issue2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "orders on upvotes" do
|
|
|
|
expect(Issue.order_upvotes_desc.to_a).to eq [issue2, issue]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "orders on downvotes" do
|
|
|
|
expect(Issue.order_downvotes_desc.to_a).to eq [issue, issue2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#upvotes" do
|
|
|
|
it "counts the number of upvotes" do
|
|
|
|
expect(issue.upvotes).to be 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#downvotes" do
|
|
|
|
it "counts the number of downvotes" do
|
|
|
|
expect(issue.downvotes).to be 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#toggle_award_emoji" do
|
|
|
|
it "adds an emoji if it isn't awarded yet" do
|
2016-06-06 01:42:12 -04:00
|
|
|
expect { issue.toggle_award_emoji("thumbsup", award_emoji.user) }.to change { AwardEmoji.count }.by(1)
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "toggles already awarded emoji" do
|
2016-06-06 01:42:12 -04:00
|
|
|
expect { issue.toggle_award_emoji("thumbsdown", award_emoji.user) }.to change { AwardEmoji.count }.by(-1)
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-29 10:01:00 -04:00
|
|
|
|
|
|
|
describe 'querying award_emoji on an Awardable' do
|
|
|
|
let(:issue) { create(:issue) }
|
|
|
|
|
|
|
|
it 'sorts in ascending fashion' do
|
|
|
|
create_list(:award_emoji, 3, awardable: issue)
|
|
|
|
|
|
|
|
expect(issue.award_emoji).to eq issue.award_emoji.sort_by(&:id)
|
|
|
|
end
|
|
|
|
end
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|