The code powering m.abunchtell.com https://m.abunchtell.com
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

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