BigW Consortium Gitlab

awardable_spec.rb 1.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
require 'spec_helper'

describe Issue, "Awardable" do
  let!(:issue)        { create(:issue) }
  let!(:award_emoji)  { create(:award_emoji, :downvote, awardable: issue) }

  describe "Associations" do
    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
41
      expect { issue.toggle_award_emoji("thumbsup", award_emoji.user) }.to change { AwardEmoji.count }.by(1)
42 43 44
    end

    it "toggles already awarded emoji" do
45
      expect { issue.toggle_award_emoji("thumbsdown", award_emoji.user) }.to change { AwardEmoji.count }.by(-1)
46 47 48
    end
  end
end