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) }
|
2017-08-30 03:48:55 -04:00
|
|
|
let!(:award_emoji2) { create(:award_emoji, awardable: issue2) }
|
2016-04-25 14:10:20 -04:00
|
|
|
|
2017-08-30 03:48:55 -04:00
|
|
|
describe "orders" do
|
|
|
|
it "orders on upvotes" do
|
|
|
|
expect(Issue.order_upvotes_desc.to_a).to eq [issue2, issue]
|
|
|
|
end
|
2016-04-25 14:10:20 -04:00
|
|
|
|
2017-08-30 03:48:55 -04:00
|
|
|
it "orders on downvotes" do
|
|
|
|
expect(Issue.order_downvotes_desc.to_a).to eq [issue, issue2]
|
|
|
|
end
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|
|
|
|
|
2018-10-27 11:52:26 -04:00
|
|
|
describe "#awarded" do
|
2017-08-30 03:48:55 -04:00
|
|
|
it "filters by user and emoji name" do
|
|
|
|
expect(Issue.awarded(award_emoji.user, "thumbsup")).to be_empty
|
|
|
|
expect(Issue.awarded(award_emoji.user, "thumbsdown")).to eq [issue]
|
|
|
|
expect(Issue.awarded(award_emoji2.user, "thumbsup")).to eq [issue2]
|
|
|
|
expect(Issue.awarded(award_emoji2.user, "thumbsdown")).to be_empty
|
|
|
|
end
|
2018-10-27 11:52:26 -04:00
|
|
|
|
|
|
|
it "filters by user and any emoji" do
|
|
|
|
issue3 = create(:issue)
|
|
|
|
create(:award_emoji, awardable: issue3, name: "star", user: award_emoji.user)
|
|
|
|
create(:award_emoji, awardable: issue3, name: "star", user: award_emoji2.user)
|
|
|
|
|
2018-11-06 01:02:01 -05:00
|
|
|
expect(Issue.awarded(award_emoji.user)).to contain_exactly(issue, issue3)
|
|
|
|
expect(Issue.awarded(award_emoji2.user)).to contain_exactly(issue2, issue3)
|
2018-10-27 11:52:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#not_awarded" do
|
|
|
|
it "returns issues not awarded by user" do
|
|
|
|
expect(Issue.not_awarded(award_emoji.user)).to eq [issue2]
|
|
|
|
expect(Issue.not_awarded(award_emoji2.user)).to eq [issue]
|
|
|
|
end
|
2016-04-25 14:10:20 -04:00
|
|
|
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
|
|
|
|
|
2018-04-06 14:19:37 -04:00
|
|
|
describe '#user_can_award?' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
issue.project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is truthy when the user is allowed to award emoji' do
|
2018-08-24 06:13:41 -04:00
|
|
|
expect(issue.user_can_award?(user)).to be_truthy
|
2018-04-06 14:19:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is falsy when the project is archived' do
|
|
|
|
issue.project.update!(archived: true)
|
|
|
|
|
2018-08-24 06:13:41 -04:00
|
|
|
expect(issue.user_can_award?(user)).to be_falsy
|
2018-04-06 14:19:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-25 14:10:20 -04:00
|
|
|
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
|