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.

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