follow_spec.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. require 'rails_helper'
  2. RSpec.describe Follow, type: :model do
  3. let(:alice) { Fabricate(:account, username: 'alice') }
  4. let(:bob) { Fabricate(:account, username: 'bob') }
  5. describe 'validations' do
  6. subject { Follow.new(account: alice, target_account: bob) }
  7. it 'has a valid fabricator' do
  8. follow = Fabricate.build(:follow)
  9. expect(follow).to be_valid
  10. end
  11. it 'is invalid without an account' do
  12. follow = Fabricate.build(:follow, account: nil)
  13. follow.valid?
  14. expect(follow).to model_have_error_on_field(:account)
  15. end
  16. it 'is invalid without a target_account' do
  17. follow = Fabricate.build(:follow, target_account: nil)
  18. follow.valid?
  19. expect(follow).to model_have_error_on_field(:target_account)
  20. end
  21. it 'is invalid if account already follows too many people' do
  22. alice.update(following_count: FollowLimitValidator::LIMIT)
  23. expect(subject).to_not be_valid
  24. expect(subject).to model_have_error_on_field(:base)
  25. end
  26. it 'is valid if account is only on the brink of following too many people' do
  27. alice.update(following_count: FollowLimitValidator::LIMIT - 1)
  28. expect(subject).to be_valid
  29. expect(subject).to_not model_have_error_on_field(:base)
  30. end
  31. end
  32. describe 'recent' do
  33. it 'sorts so that more recent follows comes earlier' do
  34. follow0 = Follow.create!(account: alice, target_account: bob)
  35. follow1 = Follow.create!(account: bob, target_account: alice)
  36. a = Follow.recent.to_a
  37. expect(a.size).to eq 2
  38. expect(a[0]).to eq follow1
  39. expect(a[1]).to eq follow0
  40. end
  41. end
  42. describe 'revoke_request!' do
  43. let(:follow) { Fabricate(:follow, account: account, target_account: target_account) }
  44. let(:account) { Fabricate(:account) }
  45. let(:target_account) { Fabricate(:account) }
  46. it 'revokes the follow relation' do
  47. follow.revoke_request!
  48. expect(account.following?(target_account)).to be false
  49. end
  50. it 'creates a follow request' do
  51. follow.revoke_request!
  52. expect(account.requested?(target_account)).to be true
  53. end
  54. end
  55. end