BigW Consortium Gitlab

subscribable_spec.rb 1.83 KB
Newer Older
1
require 'spec_helper'
2

3
describe Subscribable, 'Subscribable' do
4 5 6
  let(:resource) { create(:issue) }
  let(:user) { create(:user) }

7 8
  describe '#subscribed?' do
    it 'returns false when no subcription exists' do
9
      expect(resource.subscribed?(user)).to be_falsey
10 11 12 13 14
    end

    it 'returns true when a subcription exists and subscribed is true' do
      resource.subscriptions.create(user: user, subscribed: true)

15
      expect(resource.subscribed?(user)).to be_truthy
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
    end

    it 'returns false when a subcription exists and subscribed is false' do
      resource.subscriptions.create(user: user, subscribed: false)

      expect(resource.subscribed?(user)).to be_falsey
    end
  end
  describe '#subscribers' do
    it 'returns [] when no subcribers exists' do
      expect(resource.subscribers).to be_empty
    end

    it 'returns the subscribed users' do
      resource.subscriptions.create(user: user, subscribed: true)
      resource.subscriptions.create(user: create(:user), subscribed: false)

      expect(resource.subscribers).to eq [user]
    end
  end

  describe '#toggle_subscription' do
    it 'toggles the current subscription state for the given user' do
      expect(resource.subscribed?(user)).to be_falsey

41
      resource.toggle_subscription(user)
42 43 44 45 46

      expect(resource.subscribed?(user)).to be_truthy
    end
  end

47 48 49 50 51 52 53 54 55 56
  describe '#subscribe' do
    it 'subscribes the given user' do
      expect(resource.subscribed?(user)).to be_falsey

      resource.subscribe(user)

      expect(resource.subscribed?(user)).to be_truthy
    end
  end

57 58 59 60 61 62 63
  describe '#unsubscribe' do
    it 'unsubscribes the given current user' do
      resource.subscriptions.create(user: user, subscribed: true)
      expect(resource.subscribed?(user)).to be_truthy

      resource.unsubscribe(user)

64 65 66 67
      expect(resource.subscribed?(user)).to be_falsey
    end
  end
end