about summary refs log tree commit diff
path: root/spec/models
diff options
context:
space:
mode:
authorysksn <bluewhale1982@gmail.com>2017-11-09 22:37:10 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-11-09 14:37:10 +0100
commit07cca6e364c0b8acf7c1bc4429113cab035d6f30 (patch)
treeebb0576061626babb310dce2be00fa0b3ad7e01f /spec/models
parent54b42901df938257ae660168958fffe58b1a0287 (diff)
Add tests for Notification (#5640)
* Add tests for Notification#target_status

* Add tests for Notification#browserable?

* Add tests for Notification.reload_stale_associations!
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/notification_spec.rb113
1 files changed, 113 insertions, 0 deletions
diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb
index 97e8095cd..763b1523f 100644
--- a/spec/models/notification_spec.rb
+++ b/spec/models/notification_spec.rb
@@ -5,6 +5,74 @@ RSpec.describe Notification, type: :model do
     pending
   end
 
+  describe '#target_status' do
+    before do
+      allow(notification).to receive(:type).and_return(type)
+      allow(notification).to receive(:activity).and_return(activity)
+    end
+
+    let(:notification) { Fabricate(:notification) }
+    let(:status)       { instance_double('Status') }
+    let(:favourite)    { instance_double('Favourite') }
+    let(:mention)      { instance_double('Mention') }
+
+    context 'type is :reblog' do
+      let(:type)     { :reblog }
+      let(:activity) { status }
+
+      it 'calls activity.reblog' do
+        expect(activity).to receive(:reblog)
+        notification.target_status
+      end
+    end
+
+    context 'type is :favourite' do
+      let(:type)     { :favourite }
+      let(:activity) { favourite }
+
+      it 'calls activity.status' do
+        expect(activity).to receive(:status)
+        notification.target_status
+      end
+    end
+
+    context 'type is :mention' do
+      let(:type)     { :mention }
+      let(:activity) { mention }
+
+      it 'calls activity.status' do
+        expect(activity).to receive(:status)
+        notification.target_status
+      end
+    end
+  end
+
+  describe '#browserable?' do
+    let(:notification) { Fabricate(:notification) }
+
+    subject { notification.browserable? }
+
+    context 'type is :follow_request' do
+      before do
+        allow(notification).to receive(:type).and_return(:follow_request)
+      end
+
+      it 'returns false' do
+        is_expected.to be false
+      end
+    end
+
+    context 'type is not :follow_request' do
+      before do
+        allow(notification).to receive(:type).and_return(:else)
+      end
+
+      it 'returns true' do
+        is_expected.to be true
+      end
+    end
+  end
+
   describe '#type' do
     it 'returns :reblog for a Status' do
       notification = Notification.new(activity: Status.new)
@@ -26,4 +94,49 @@ RSpec.describe Notification, type: :model do
       expect(notification.type).to eq :follow
     end
   end
+
+  describe '.reload_stale_associations!' do
+    context 'account_ids are empty' do
+      let(:cached_items) { [] }
+
+      subject { described_class.reload_stale_associations!(cached_items) }
+
+      it 'returns nil' do
+        is_expected.to be nil
+      end
+    end
+
+    context 'account_ids are present' do
+      before do
+        allow(accounts_with_ids).to receive(:[]).with(stale_account1.id).and_return(account1)
+        allow(accounts_with_ids).to receive(:[]).with(stale_account2.id).and_return(account2)
+        allow(Account).to receive_message_chain(:where, :map, :to_h).and_return(accounts_with_ids)
+      end
+
+      let(:cached_items) do
+        [
+          Fabricate(:notification, activity: Fabricate(:status)),
+          Fabricate(:notification, activity: Fabricate(:follow)),
+        ]
+      end
+
+      let(:stale_account1) { cached_items[0].from_account }
+      let(:stale_account2) { cached_items[1].from_account }
+
+      let(:account1) { Fabricate(:account) }
+      let(:account2) { Fabricate(:account) }
+
+      let(:accounts_with_ids) { { account1.id => account1, account2.id => account2 } }
+
+      it 'reloads associations' do
+        expect(cached_items[0].from_account).to be stale_account1
+        expect(cached_items[1].from_account).to be stale_account2
+
+        described_class.reload_stale_associations!(cached_items)
+
+        expect(cached_items[0].from_account).to be account1
+        expect(cached_items[1].from_account).to be account2
+      end
+    end
+  end
 end