notification_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. require 'rails_helper'
  2. RSpec.describe Notification, type: :model do
  3. describe '#target_status' do
  4. let(:notification) { Fabricate(:notification, activity: activity) }
  5. let(:status) { Fabricate(:status) }
  6. let(:reblog) { Fabricate(:status, reblog: status) }
  7. let(:favourite) { Fabricate(:favourite, status: status) }
  8. let(:mention) { Fabricate(:mention, status: status) }
  9. context 'activity is reblog' do
  10. let(:activity) { reblog }
  11. it 'returns status' do
  12. expect(notification.target_status).to eq status
  13. end
  14. end
  15. context 'activity is favourite' do
  16. let(:type) { :favourite }
  17. let(:activity) { favourite }
  18. it 'returns status' do
  19. expect(notification.target_status).to eq status
  20. end
  21. end
  22. context 'activity is mention' do
  23. let(:activity) { mention }
  24. it 'returns status' do
  25. expect(notification.target_status).to eq status
  26. end
  27. end
  28. end
  29. describe '#browserable?' do
  30. let(:notification) { Fabricate(:notification) }
  31. subject { notification.browserable? }
  32. context 'type is :follow_request' do
  33. before do
  34. allow(notification).to receive(:type).and_return(:follow_request)
  35. end
  36. it 'returns false' do
  37. is_expected.to be false
  38. end
  39. end
  40. context 'type is not :follow_request' do
  41. before do
  42. allow(notification).to receive(:type).and_return(:else)
  43. end
  44. it 'returns true' do
  45. is_expected.to be true
  46. end
  47. end
  48. end
  49. describe '#type' do
  50. it 'returns :reblog for a Status' do
  51. notification = Notification.new(activity: Status.new)
  52. expect(notification.type).to eq :reblog
  53. end
  54. it 'returns :mention for a Mention' do
  55. notification = Notification.new(activity: Mention.new)
  56. expect(notification.type).to eq :mention
  57. end
  58. it 'returns :favourite for a Favourite' do
  59. notification = Notification.new(activity: Favourite.new)
  60. expect(notification.type).to eq :favourite
  61. end
  62. it 'returns :follow for a Follow' do
  63. notification = Notification.new(activity: Follow.new)
  64. expect(notification.type).to eq :follow
  65. end
  66. end
  67. describe '.reload_stale_associations!' do
  68. context 'account_ids are empty' do
  69. let(:cached_items) { [] }
  70. subject { described_class.reload_stale_associations!(cached_items) }
  71. it 'returns nil' do
  72. is_expected.to be nil
  73. end
  74. end
  75. context 'account_ids are present' do
  76. before do
  77. allow(accounts_with_ids).to receive(:[]).with(stale_account1.id).and_return(account1)
  78. allow(accounts_with_ids).to receive(:[]).with(stale_account2.id).and_return(account2)
  79. allow(Account).to receive_message_chain(:where, :includes, :each_with_object).and_return(accounts_with_ids)
  80. end
  81. let(:cached_items) do
  82. [
  83. Fabricate(:notification, activity: Fabricate(:status)),
  84. Fabricate(:notification, activity: Fabricate(:follow)),
  85. ]
  86. end
  87. let(:stale_account1) { cached_items[0].from_account }
  88. let(:stale_account2) { cached_items[1].from_account }
  89. let(:account1) { Fabricate(:account) }
  90. let(:account2) { Fabricate(:account) }
  91. let(:accounts_with_ids) { { account1.id => account1, account2.id => account2 } }
  92. it 'reloads associations' do
  93. expect(cached_items[0].from_account).to be stale_account1
  94. expect(cached_items[1].from_account).to be stale_account2
  95. described_class.reload_stale_associations!(cached_items)
  96. expect(cached_items[0].from_account).to be account1
  97. expect(cached_items[1].from_account).to be account2
  98. end
  99. end
  100. end
  101. end