2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-25 18:10:20 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-25 12:28:55 +00:00
|
|
|
describe Awardable do
|
2016-04-25 18:10:20 +00:00
|
|
|
let!(:issue) { create(:issue) }
|
|
|
|
let!(:award_emoji) { create(:award_emoji, :downvote, awardable: issue) }
|
|
|
|
|
|
|
|
describe "Associations" do
|
2017-04-25 12:28:55 +00:00
|
|
|
subject { build(:issue) }
|
|
|
|
|
2016-04-25 18:10:20 +00:00
|
|
|
it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "ClassMethods" do
|
|
|
|
let!(:issue2) { create(:issue) }
|
2017-08-30 07:48:55 +00:00
|
|
|
let!(:award_emoji2) { create(:award_emoji, awardable: issue2) }
|
2016-04-25 18:10:20 +00:00
|
|
|
|
2017-08-30 07:48:55 +00:00
|
|
|
describe "orders" do
|
|
|
|
it "orders on upvotes" do
|
|
|
|
expect(Issue.order_upvotes_desc.to_a).to eq [issue2, issue]
|
|
|
|
end
|
2016-04-25 18:10:20 +00:00
|
|
|
|
2017-08-30 07:48:55 +00:00
|
|
|
it "orders on downvotes" do
|
|
|
|
expect(Issue.order_downvotes_desc.to_a).to eq [issue, issue2]
|
|
|
|
end
|
2016-04-25 18:10:20 +00:00
|
|
|
end
|
|
|
|
|
2018-10-27 15:52:26 +00:00
|
|
|
describe "#awarded" do
|
2017-08-30 07:48:55 +00: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 15:52:26 +00: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 06:02:01 +00: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 15:52:26 +00: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 18:10:20 +00: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 18:19:37 +00: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 10:13:41 +00:00
|
|
|
expect(issue.user_can_award?(user)).to be_truthy
|
2018-04-06 18:19:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is falsy when the project is archived' do
|
|
|
|
issue.project.update!(archived: true)
|
|
|
|
|
2018-08-24 10:13:41 +00:00
|
|
|
expect(issue.user_can_award?(user)).to be_falsy
|
2018-04-06 18:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-29 14:01:00 +00: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 18:10:20 +00:00
|
|
|
end
|