notification_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 '.preload_cache_collection_target_statuses' do
  48. subject do
  49. described_class.preload_cache_collection_target_statuses(notifications) do |target_statuses|
  50. # preload account for testing instead of using cache_collection
  51. Status.preload(:account).where(id: target_statuses.map(&:id))
  52. end
  53. end
  54. context 'notifications are empty' do
  55. let(:notifications) { [] }
  56. it 'returns []' do
  57. is_expected.to eq []
  58. end
  59. end
  60. context 'notifications are present' do
  61. before do
  62. notifications.each(&:reload)
  63. end
  64. let(:mention) { Fabricate(:mention) }
  65. let(:status) { Fabricate(:status) }
  66. let(:reblog) { Fabricate(:status, reblog: Fabricate(:status)) }
  67. let(:follow) { Fabricate(:follow) }
  68. let(:follow_request) { Fabricate(:follow_request) }
  69. let(:favourite) { Fabricate(:favourite) }
  70. let(:poll) { Fabricate(:poll) }
  71. let(:notifications) do
  72. [
  73. Fabricate(:notification, type: :mention, activity: mention),
  74. Fabricate(:notification, type: :status, activity: status),
  75. Fabricate(:notification, type: :reblog, activity: reblog),
  76. Fabricate(:notification, type: :follow, activity: follow),
  77. Fabricate(:notification, type: :follow_request, activity: follow_request),
  78. Fabricate(:notification, type: :favourite, activity: favourite),
  79. Fabricate(:notification, type: :poll, activity: poll),
  80. ]
  81. end
  82. it 'preloads target status' do
  83. # mention
  84. expect(subject[0].type).to eq :mention
  85. expect(subject[0].association(:mention)).to be_loaded
  86. expect(subject[0].mention.association(:status)).to be_loaded
  87. # status
  88. expect(subject[1].type).to eq :status
  89. expect(subject[1].association(:status)).to be_loaded
  90. # reblog
  91. expect(subject[2].type).to eq :reblog
  92. expect(subject[2].association(:status)).to be_loaded
  93. expect(subject[2].status.association(:reblog)).to be_loaded
  94. # follow: nothing
  95. expect(subject[3].type).to eq :follow
  96. expect(subject[3].target_status).to be_nil
  97. # follow_request: nothing
  98. expect(subject[4].type).to eq :follow_request
  99. expect(subject[4].target_status).to be_nil
  100. # favourite
  101. expect(subject[5].type).to eq :favourite
  102. expect(subject[5].association(:favourite)).to be_loaded
  103. expect(subject[5].favourite.association(:status)).to be_loaded
  104. # poll
  105. expect(subject[6].type).to eq :poll
  106. expect(subject[6].association(:poll)).to be_loaded
  107. expect(subject[6].poll.association(:status)).to be_loaded
  108. end
  109. it 'replaces to cached status' do
  110. # mention
  111. expect(subject[0].type).to eq :mention
  112. expect(subject[0].target_status.association(:account)).to be_loaded
  113. expect(subject[0].target_status).to eq mention.status
  114. # status
  115. expect(subject[1].type).to eq :status
  116. expect(subject[1].target_status.association(:account)).to be_loaded
  117. expect(subject[1].target_status).to eq status
  118. # reblog
  119. expect(subject[2].type).to eq :reblog
  120. expect(subject[2].target_status.association(:account)).to be_loaded
  121. expect(subject[2].target_status).to eq reblog.reblog
  122. # follow: nothing
  123. expect(subject[3].type).to eq :follow
  124. expect(subject[3].target_status).to be_nil
  125. # follow_request: nothing
  126. expect(subject[4].type).to eq :follow_request
  127. expect(subject[4].target_status).to be_nil
  128. # favourite
  129. expect(subject[5].type).to eq :favourite
  130. expect(subject[5].target_status.association(:account)).to be_loaded
  131. expect(subject[5].target_status).to eq favourite.status
  132. # poll
  133. expect(subject[6].type).to eq :poll
  134. expect(subject[6].target_status.association(:account)).to be_loaded
  135. expect(subject[6].target_status).to eq poll.status
  136. end
  137. end
  138. end
  139. end