notification_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 '#type' do
  30. it 'returns :reblog for a Status' do
  31. notification = Notification.new(activity: Status.new)
  32. expect(notification.type).to eq :reblog
  33. end
  34. it 'returns :mention for a Mention' do
  35. notification = Notification.new(activity: Mention.new)
  36. expect(notification.type).to eq :mention
  37. end
  38. it 'returns :favourite for a Favourite' do
  39. notification = Notification.new(activity: Favourite.new)
  40. expect(notification.type).to eq :favourite
  41. end
  42. it 'returns :follow for a Follow' do
  43. notification = Notification.new(activity: Follow.new)
  44. expect(notification.type).to eq :follow
  45. end
  46. end
  47. describe '.reload_stale_associations!' do
  48. context 'account_ids are empty' do
  49. let(:cached_items) { [] }
  50. subject { described_class.reload_stale_associations!(cached_items) }
  51. it 'returns nil' do
  52. is_expected.to be nil
  53. end
  54. end
  55. context 'account_ids are present' do
  56. before do
  57. allow(accounts_with_ids).to receive(:[]).with(stale_account1.id).and_return(account1)
  58. allow(accounts_with_ids).to receive(:[]).with(stale_account2.id).and_return(account2)
  59. allow(Account).to receive_message_chain(:where, :includes, :each_with_object).and_return(accounts_with_ids)
  60. end
  61. let(:cached_items) do
  62. [
  63. Fabricate(:notification, activity: Fabricate(:status)),
  64. Fabricate(:notification, activity: Fabricate(:follow)),
  65. ]
  66. end
  67. let(:stale_account1) { cached_items[0].from_account }
  68. let(:stale_account2) { cached_items[1].from_account }
  69. let(:account1) { Fabricate(:account) }
  70. let(:account2) { Fabricate(:account) }
  71. let(:accounts_with_ids) { { account1.id => account1, account2.id => account2 } }
  72. it 'reloads associations' do
  73. expect(cached_items[0].from_account).to be stale_account1
  74. expect(cached_items[1].from_account).to be stale_account2
  75. described_class.reload_stale_associations!(cached_items)
  76. expect(cached_items[0].from_account).to be account1
  77. expect(cached_items[1].from_account).to be account2
  78. end
  79. end
  80. end
  81. end