awardable_spec.rb 2.98 KB
Newer Older
1 2
require 'spec_helper'

Sean McGivern's avatar
Sean McGivern committed
3
describe Awardable do
4 5 6 7
  let!(:issue)        { create(:issue) }
  let!(:award_emoji)  { create(:award_emoji, :downvote, awardable: issue) }

  describe "Associations" do
Sean McGivern's avatar
Sean McGivern committed
8 9
    subject { build(:issue) }

10 11 12 13 14
    it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
  end

  describe "ClassMethods" do
    let!(:issue2) { create(:issue) }
Hiroyuki Sato's avatar
Hiroyuki Sato committed
15
    let!(:award_emoji2) { create(:award_emoji, awardable: issue2) }
16

Hiroyuki Sato's avatar
Hiroyuki Sato committed
17 18 19 20
    describe "orders" do
      it "orders on upvotes" do
        expect(Issue.order_upvotes_desc.to_a).to eq [issue2, issue]
      end
21

Hiroyuki Sato's avatar
Hiroyuki Sato committed
22 23 24
      it "orders on downvotes" do
        expect(Issue.order_downvotes_desc.to_a).to eq [issue, issue2]
      end
25 26
    end

Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
27
    describe "#awarded" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
28 29 30 31 32 33
      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
Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
34 35 36 37 38 39

      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)

40 41
        expect(Issue.awarded(award_emoji.user)).to contain_exactly(issue, issue3)
        expect(Issue.awarded(award_emoji2.user)).to contain_exactly(issue2, issue3)
Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
42 43 44 45 46 47 48 49
      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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    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

65 66 67 68 69 70 71 72
  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
73
      expect(issue.user_can_award?(user)).to be_truthy
74 75 76 77 78
    end

    it 'is falsy when the project is archived' do
      issue.project.update!(archived: true)

79
      expect(issue.user_can_award?(user)).to be_falsy
80 81 82
    end
  end

83 84
  describe "#toggle_award_emoji" do
    it "adds an emoji if it isn't awarded yet" do
85
      expect { issue.toggle_award_emoji("thumbsup", award_emoji.user) }.to change { AwardEmoji.count }.by(1)
86 87 88
    end

    it "toggles already awarded emoji" do
89
      expect { issue.toggle_award_emoji("thumbsdown", award_emoji.user) }.to change { AwardEmoji.count }.by(-1)
90 91
    end
  end
92 93 94 95 96 97 98 99 100 101

  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
102
end