You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.7 KiB

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