discourse/spec/models/notification_spec.rb

125 lines
3.7 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require 'spec_helper'
describe Notification do
it { should validate_presence_of :notification_type }
it { should validate_presence_of :data }
it { should belong_to :user }
it { should belong_to :topic }
describe 'unread counts' do
let(:user) { Fabricate(:user) }
2013-02-26 00:42:20 +08:00
context 'a regular notification' do
2013-02-06 03:16:51 +08:00
it 'increases unread_notifications' do
lambda { Fabricate(:notification, user: user); user.reload }.should change(user, :unread_notifications)
end
it "doesn't increase unread_private_messages" do
lambda { Fabricate(:notification, user: user); user.reload }.should_not change(user, :unread_private_messages)
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
context 'a private message' do
it "doesn't increase unread_notifications" do
lambda { Fabricate(:private_message_notification, user: user); user.reload }.should_not change(user, :unread_notifications)
end
it "increases unread_private_messages" do
lambda { Fabricate(:private_message_notification, user: user); user.reload }.should change(user, :unread_private_messages)
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
end
describe 'message bus' do
it 'updates the notification count on create' do
MessageBusObserver.any_instance.expects(:refresh_notification_count).with(instance_of(Notification))
Fabricate(:notification)
end
context 'destroy' do
let!(:notification) { Fabricate(:notification) }
it 'updates the notification count on destroy' do
2013-02-26 00:42:20 +08:00
MessageBusObserver.any_instance.expects(:refresh_notification_count).with(instance_of(Notification))
notification.destroy
2013-02-06 03:16:51 +08:00
end
end
end
describe '@mention' do
it "calls email_user_mentioned on creating a notification" do
UserEmailObserver.any_instance.expects(:email_user_mentioned).with(instance_of(Notification))
Fabricate(:notification)
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
describe '@mention' do
it "calls email_user_quoted on creating a quote notification" do
UserEmailObserver.any_instance.expects(:email_user_quoted).with(instance_of(Notification))
Fabricate(:quote_notification)
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
2013-02-26 00:42:20 +08:00
describe 'private message' do
before do
2013-02-06 03:16:51 +08:00
@topic = Fabricate(:private_message_topic)
@post = Fabricate(:post, :topic => @topic, :user => @topic.user)
@target = @post.topic.topic_allowed_users.reject{|a| a.user_id == @post.user_id}[0].user
end
it 'should create a private message notification' do
2013-03-01 20:07:44 +08:00
@target.notifications.first.notification_type.should == Notification.types[:private_message]
2013-02-06 03:16:51 +08:00
end
2013-02-26 00:42:20 +08:00
it 'should not add a pm notification for the creator' do
2013-02-06 03:16:51 +08:00
@post.user.unread_notifications.should == 0
end
end
describe '.post' do
let(:post) { Fabricate(:post) }
let!(:notification) { Fabricate(:notification, user: post.user, topic: post.topic, post_number: post.post_number) }
it 'returns the post' do
notification.post.should == post
end
end
describe 'data' do
let(:notification) { Fabricate.build(:notification) }
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
it 'should have a data hash' do
notification.data_hash.should be_present
end
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
it 'should have the data within the json' do
notification.data_hash[:poison].should == 'ivy'
end
end
2013-02-26 00:42:20 +08:00
describe 'mark_posts_read' do
it "marks multiple posts as read if needed" do
user = Fabricate(:user)
2013-02-26 00:42:20 +08:00
notifications = (1..3).map do |i|
Notification.create!(read: false, user_id: user.id, topic_id: 2, post_number: i, data: '[]', notification_type: 1)
end
Notification.create!(read: true, user_id: user.id, topic_id: 2, post_number: 4, data: '[]', notification_type: 1)
Notification.mark_posts_read(user,2,[1,2,3,4]).should == 3
end
end
2013-02-06 03:16:51 +08:00
end