Extend notifications to be marked as read and unread
This commit is contained in:
@@ -2,29 +2,89 @@ require 'rails_helper'
|
||||
|
||||
describe Notification do
|
||||
|
||||
describe "#unread (scope)" do
|
||||
it "returns only unread notifications" do
|
||||
2.times { create :notification }
|
||||
expect(described_class.unread.size).to be 2
|
||||
let(:notification) { build(:notification) }
|
||||
|
||||
context "validations" do
|
||||
|
||||
it "should be valid" do
|
||||
expect(notification).to be_valid
|
||||
end
|
||||
|
||||
it "should not be valid without a user" do
|
||||
notification.user = nil
|
||||
expect(notification).to_not be_valid
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "scopes" do
|
||||
|
||||
describe "#read" do
|
||||
it "returns only read notifications" do
|
||||
read_notification1 = create(:notification, :read)
|
||||
read_notification2 = create(:notification, :read)
|
||||
unread_notification = create(:notification)
|
||||
|
||||
expect(described_class.read).to include read_notification1
|
||||
expect(described_class.read).to include read_notification2
|
||||
expect(described_class.read).not_to include unread_notification
|
||||
end
|
||||
end
|
||||
|
||||
describe "#unread" do
|
||||
it "returns only unread notifications" do
|
||||
read_notification = create(:notification, :read)
|
||||
unread_notification1 = create(:notification)
|
||||
unread_notification2 = create(:notification)
|
||||
|
||||
expect(described_class.unread).to include unread_notification1
|
||||
expect(described_class.unread).to include unread_notification2
|
||||
expect(described_class.unread).not_to include read_notification
|
||||
end
|
||||
end
|
||||
|
||||
describe "#recent" do
|
||||
it "returns notifications sorted by id descendant" do
|
||||
old_notification = create :notification
|
||||
new_notification = create :notification
|
||||
|
||||
sorted_notifications = described_class.recent
|
||||
expect(sorted_notifications.size).to be 2
|
||||
expect(sorted_notifications.first).to eq new_notification
|
||||
expect(sorted_notifications.last).to eq old_notification
|
||||
end
|
||||
end
|
||||
|
||||
describe "#for_render" do
|
||||
it "returns notifications including notifiable and user" do
|
||||
allow(described_class).to receive(:includes).with(:notifiable).exactly(:once)
|
||||
described_class.for_render
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#mark_as_read" do
|
||||
it "destroys notification" do
|
||||
notification = create(:notification)
|
||||
expect(described_class.read.size).to eq 0
|
||||
expect(described_class.unread.size).to eq 1
|
||||
|
||||
notification.mark_as_read
|
||||
expect(described_class.read.size).to eq 1
|
||||
expect(described_class.unread.size).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "#recent (scope)" do
|
||||
it "returns notifications sorted by id descendant" do
|
||||
old_notification = create :notification
|
||||
new_notification = create :notification
|
||||
describe "#mark_as_unread" do
|
||||
it "destroys notification" do
|
||||
notification = create(:notification, :read)
|
||||
expect(described_class.unread.size).to eq 0
|
||||
expect(described_class.read.size).to eq 1
|
||||
|
||||
sorted_notifications = described_class.recent
|
||||
expect(sorted_notifications.size).to be 2
|
||||
expect(sorted_notifications.first).to eq new_notification
|
||||
expect(sorted_notifications.last).to eq old_notification
|
||||
end
|
||||
end
|
||||
|
||||
describe "#for_render (scope)" do
|
||||
it "returns notifications including notifiable and user" do
|
||||
allow(described_class).to receive(:includes).with(:notifiable).exactly(:once)
|
||||
described_class.for_render
|
||||
notification.mark_as_unread
|
||||
expect(described_class.unread.size).to eq 1
|
||||
expect(described_class.read.size).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,13 +97,66 @@ describe Notification do
|
||||
end
|
||||
end
|
||||
|
||||
describe "#mark_as_read" do
|
||||
it "destroys notification" do
|
||||
notification = create :notification
|
||||
expect(described_class.unread.size).to eq 1
|
||||
describe "#existent" do
|
||||
it "returns the notifiable when there is an existent notification of that notifiable" do
|
||||
user = create(:user)
|
||||
comment = create(:comment)
|
||||
notification = create(:notification, user: user, notifiable: comment)
|
||||
|
||||
notification.mark_as_read
|
||||
expect(described_class.unread.size).to eq 0
|
||||
expect(described_class.existent(user, comment)).to eq(notification)
|
||||
end
|
||||
|
||||
it "returns nil when there are no notifications of that notifiable for a user" do
|
||||
user = create(:user)
|
||||
comment1 = create(:comment)
|
||||
comment2 = create(:comment)
|
||||
create(:notification, user: user, notifiable: comment1)
|
||||
|
||||
expect(described_class.existent(user, comment2)).to eq(nil)
|
||||
end
|
||||
|
||||
it "returns nil when there are notifications of a notifiable for another user" do
|
||||
user1 = create(:user)
|
||||
user2 = create(:user)
|
||||
comment = create(:comment)
|
||||
notification = create(:notification, user: user1, notifiable: comment)
|
||||
|
||||
expect(described_class.existent(user2, comment)).to eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#add" do
|
||||
it "creates a new notification" do
|
||||
user = create(:user)
|
||||
comment = create(:comment)
|
||||
|
||||
described_class.add(user, comment)
|
||||
expect(user.notifications.count).to eq(1)
|
||||
end
|
||||
|
||||
it "increments the notification counter for an unread notification of the same notifiable" do
|
||||
user = create(:user)
|
||||
comment = create(:comment)
|
||||
|
||||
described_class.add(user, comment)
|
||||
described_class.add(user, comment)
|
||||
|
||||
expect(user.notifications.count).to eq(1)
|
||||
expect(user.notifications.first.counter).to eq(2)
|
||||
end
|
||||
|
||||
it "creates a new notification for a read notification of the same notifiable" do
|
||||
user = create(:user)
|
||||
comment = create(:comment)
|
||||
|
||||
first_notification = described_class.add(user, comment)
|
||||
first_notification.update(read_at: Time.current)
|
||||
|
||||
second_notification = described_class.add(user, comment)
|
||||
|
||||
expect(user.notifications.count).to eq(2)
|
||||
expect(first_notification.counter).to eq(1)
|
||||
expect(second_notification.counter).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user