notification_spec.rb 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Notification do
  4. describe '#target_status' do
  5. let(:notification) { Fabricate(:notification, activity: activity) }
  6. let(:status) { Fabricate(:status) }
  7. let(:reblog) { Fabricate(:status, reblog: status) }
  8. let(:favourite) { Fabricate(:favourite, status: status) }
  9. let(:mention) { Fabricate(:mention, status: status) }
  10. context 'when Activity is reblog' do
  11. let(:activity) { reblog }
  12. it 'returns status' do
  13. expect(notification.target_status).to eq status
  14. end
  15. end
  16. context 'when Activity is favourite' do
  17. let(:type) { :favourite }
  18. let(:activity) { favourite }
  19. it 'returns status' do
  20. expect(notification.target_status).to eq status
  21. end
  22. end
  23. context 'when Activity is mention' do
  24. let(:activity) { mention }
  25. it 'returns status' do
  26. expect(notification.target_status).to eq status
  27. end
  28. end
  29. end
  30. describe '#type' do
  31. it 'returns :reblog for a Status' do
  32. notification = described_class.new(activity: Status.new)
  33. expect(notification.type).to eq :reblog
  34. end
  35. it 'returns :mention for a Mention' do
  36. notification = described_class.new(activity: Mention.new)
  37. expect(notification.type).to eq :mention
  38. end
  39. it 'returns :favourite for a Favourite' do
  40. notification = described_class.new(activity: Favourite.new)
  41. expect(notification.type).to eq :favourite
  42. end
  43. it 'returns :follow for a Follow' do
  44. notification = described_class.new(activity: Follow.new)
  45. expect(notification.type).to eq :follow
  46. end
  47. end
  48. describe 'Setting account from activity_type' do
  49. context 'when activity_type is a Status' do
  50. it 'sets the notification from_account correctly' do
  51. status = Fabricate(:status)
  52. notification = Fabricate.build(:notification, activity_type: 'Status', activity: status)
  53. expect(notification.from_account).to eq(status.account)
  54. end
  55. end
  56. context 'when activity_type is a Follow' do
  57. it 'sets the notification from_account correctly' do
  58. follow = Fabricate(:follow)
  59. notification = Fabricate.build(:notification, activity_type: 'Follow', activity: follow)
  60. expect(notification.from_account).to eq(follow.account)
  61. end
  62. end
  63. context 'when activity_type is a Favourite' do
  64. it 'sets the notification from_account correctly' do
  65. favourite = Fabricate(:favourite)
  66. notification = Fabricate.build(:notification, activity_type: 'Favourite', activity: favourite)
  67. expect(notification.from_account).to eq(favourite.account)
  68. end
  69. end
  70. context 'when activity_type is a FollowRequest' do
  71. it 'sets the notification from_account correctly' do
  72. follow_request = Fabricate(:follow_request)
  73. notification = Fabricate.build(:notification, activity_type: 'FollowRequest', activity: follow_request)
  74. expect(notification.from_account).to eq(follow_request.account)
  75. end
  76. end
  77. context 'when activity_type is a Poll' do
  78. it 'sets the notification from_account correctly' do
  79. poll = Fabricate(:poll)
  80. notification = Fabricate.build(:notification, activity_type: 'Poll', activity: poll)
  81. expect(notification.from_account).to eq(poll.account)
  82. end
  83. end
  84. context 'when activity_type is a Report' do
  85. it 'sets the notification from_account correctly' do
  86. report = Fabricate(:report)
  87. notification = Fabricate.build(:notification, activity_type: 'Report', activity: report)
  88. expect(notification.from_account).to eq(report.account)
  89. end
  90. end
  91. context 'when activity_type is a Mention' do
  92. it 'sets the notification from_account correctly' do
  93. mention = Fabricate(:mention)
  94. notification = Fabricate.build(:notification, activity_type: 'Mention', activity: mention)
  95. expect(notification.from_account).to eq(mention.status.account)
  96. end
  97. end
  98. context 'when activity_type is an Account' do
  99. it 'sets the notification from_account correctly' do
  100. account = Fabricate(:account)
  101. notification = Fabricate.build(:notification, activity_type: 'Account', account: account)
  102. expect(notification.account).to eq(account)
  103. end
  104. end
  105. end
  106. describe '.preload_cache_collection_target_statuses' do
  107. subject do
  108. described_class.preload_cache_collection_target_statuses(notifications) do |target_statuses|
  109. # preload account for testing instead of using cache_collection
  110. Status.preload(:account).where(id: target_statuses.map(&:id))
  111. end
  112. end
  113. context 'when notifications are empty' do
  114. let(:notifications) { [] }
  115. it 'returns []' do
  116. expect(subject).to eq []
  117. end
  118. end
  119. context 'when notifications are present' do
  120. before do
  121. notifications.each(&:reload)
  122. end
  123. let(:mention) { Fabricate(:mention) }
  124. let(:status) { Fabricate(:status) }
  125. let(:reblog) { Fabricate(:status, reblog: Fabricate(:status)) }
  126. let(:follow) { Fabricate(:follow) }
  127. let(:follow_request) { Fabricate(:follow_request) }
  128. let(:favourite) { Fabricate(:favourite) }
  129. let(:poll) { Fabricate(:poll) }
  130. let(:notifications) do
  131. [
  132. Fabricate(:notification, type: :mention, activity: mention),
  133. Fabricate(:notification, type: :status, activity: status),
  134. Fabricate(:notification, type: :reblog, activity: reblog),
  135. Fabricate(:notification, type: :follow, activity: follow),
  136. Fabricate(:notification, type: :follow_request, activity: follow_request),
  137. Fabricate(:notification, type: :favourite, activity: favourite),
  138. Fabricate(:notification, type: :poll, activity: poll),
  139. ]
  140. end
  141. context 'with a preloaded target status' do
  142. it 'preloads mention' do
  143. expect(subject[0].type).to eq :mention
  144. expect(subject[0].association(:mention)).to be_loaded
  145. expect(subject[0].mention.association(:status)).to be_loaded
  146. end
  147. it 'preloads status' do
  148. expect(subject[1].type).to eq :status
  149. expect(subject[1].association(:status)).to be_loaded
  150. end
  151. it 'preloads reblog' do
  152. expect(subject[2].type).to eq :reblog
  153. expect(subject[2].association(:status)).to be_loaded
  154. expect(subject[2].status.association(:reblog)).to be_loaded
  155. end
  156. it 'preloads follow as nil' do
  157. expect(subject[3].type).to eq :follow
  158. expect(subject[3].target_status).to be_nil
  159. end
  160. it 'preloads follow_request as nill' do
  161. expect(subject[4].type).to eq :follow_request
  162. expect(subject[4].target_status).to be_nil
  163. end
  164. it 'preloads favourite' do
  165. expect(subject[5].type).to eq :favourite
  166. expect(subject[5].association(:favourite)).to be_loaded
  167. expect(subject[5].favourite.association(:status)).to be_loaded
  168. end
  169. it 'preloads poll' do
  170. expect(subject[6].type).to eq :poll
  171. expect(subject[6].association(:poll)).to be_loaded
  172. expect(subject[6].poll.association(:status)).to be_loaded
  173. end
  174. end
  175. context 'with a cached status' do
  176. it 'replaces mention' do
  177. expect(subject[0].type).to eq :mention
  178. expect(subject[0].target_status.association(:account)).to be_loaded
  179. expect(subject[0].target_status).to eq mention.status
  180. end
  181. it 'replaces status' do
  182. expect(subject[1].type).to eq :status
  183. expect(subject[1].target_status.association(:account)).to be_loaded
  184. expect(subject[1].target_status).to eq status
  185. end
  186. it 'replaces reblog' do
  187. expect(subject[2].type).to eq :reblog
  188. expect(subject[2].target_status.association(:account)).to be_loaded
  189. expect(subject[2].target_status).to eq reblog.reblog
  190. end
  191. it 'replaces follow' do
  192. expect(subject[3].type).to eq :follow
  193. expect(subject[3].target_status).to be_nil
  194. end
  195. it 'replaces follow_request' do
  196. expect(subject[4].type).to eq :follow_request
  197. expect(subject[4].target_status).to be_nil
  198. end
  199. it 'replaces favourite' do
  200. expect(subject[5].type).to eq :favourite
  201. expect(subject[5].target_status.association(:account)).to be_loaded
  202. expect(subject[5].target_status).to eq favourite.status
  203. end
  204. it 'replaces poll' do
  205. expect(subject[6].type).to eq :poll
  206. expect(subject[6].target_status.association(:account)).to be_loaded
  207. expect(subject[6].target_status).to eq poll.status
  208. end
  209. end
  210. end
  211. end
  212. end