follow_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Follow do
  4. describe 'Associations' do
  5. it { is_expected.to belong_to(:account).required }
  6. it { is_expected.to belong_to(:target_account).required }
  7. end
  8. describe 'Validations' do
  9. subject { Fabricate.build :follow, rate_limit: true }
  10. let(:account) { Fabricate(:account) }
  11. context 'when account follows too many people' do
  12. before { account.update(following_count: FollowLimitValidator::LIMIT) }
  13. it { is_expected.to_not allow_value(account).for(:account).against(:base) }
  14. end
  15. context 'when account is on brink of following too many people' do
  16. before { account.update(following_count: FollowLimitValidator::LIMIT - 1) }
  17. it { is_expected.to allow_value(account).for(:account).against(:base) }
  18. end
  19. end
  20. describe '.recent' do
  21. let!(:follow_earlier) { Fabricate(:follow) }
  22. let!(:follow_later) { Fabricate(:follow) }
  23. it 'sorts with most recent follows first' do
  24. results = described_class.recent
  25. expect(results.size).to eq 2
  26. expect(results).to eq [follow_later, follow_earlier]
  27. end
  28. end
  29. describe 'revoke_request!' do
  30. let(:follow) { Fabricate(:follow, account: account, target_account: target_account) }
  31. let(:account) { Fabricate(:account) }
  32. let(:target_account) { Fabricate(:account) }
  33. it 'revokes the follow relation' do
  34. follow.revoke_request!
  35. expect(account.following?(target_account)).to be false
  36. end
  37. it 'creates a follow request' do
  38. follow.revoke_request!
  39. expect(account.requested?(target_account)).to be true
  40. end
  41. end
  42. describe '#local?' do
  43. it { is_expected.to_not be_local }
  44. end
  45. describe 'Callbacks' do
  46. describe 'Setting a URI' do
  47. context 'when URI exists' do
  48. subject { Fabricate.build :follow, uri: 'https://uri/value' }
  49. it 'does not change' do
  50. expect { subject.save }
  51. .to not_change(subject, :uri)
  52. end
  53. end
  54. context 'when URI is blank' do
  55. subject { Fabricate.build :follow, uri: nil }
  56. it 'populates the value' do
  57. expect { subject.save }
  58. .to change(subject, :uri).to(be_present)
  59. end
  60. end
  61. end
  62. describe 'Maintaining counters' do
  63. subject { Fabricate.build :follow, account:, target_account: }
  64. let(:account) { Fabricate :account }
  65. let(:target_account) { Fabricate :account }
  66. before do
  67. account.account_stat.update following_count: 123
  68. target_account.account_stat.update followers_count: 123
  69. end
  70. describe 'saving the follow' do
  71. it 'increments counters' do
  72. expect { subject.save }
  73. .to change(account, :following_count).by(1)
  74. .and(change(target_account, :followers_count).by(1))
  75. end
  76. end
  77. describe 'destroying the follow' do
  78. it 'decrements counters' do
  79. expect { subject.destroy }
  80. .to change(account, :following_count).by(-1)
  81. .and(change(target_account, :followers_count).by(-1))
  82. end
  83. end
  84. end
  85. end
  86. end