2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
require 'unread'
|
|
|
|
|
|
|
|
describe Unread do
|
|
|
|
|
2016-12-02 01:03:31 -05:00
|
|
|
let (:user) { Fabricate.build(:user, id: 1) }
|
|
|
|
let (:topic) do
|
|
|
|
Fabricate.build(:topic,
|
|
|
|
posts_count: 13,
|
|
|
|
highest_staff_post_number: 15,
|
|
|
|
highest_post_number: 13,
|
|
|
|
id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
let (:topic_user) do
|
|
|
|
Fabricate.build(:topic_user,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
topic_id: topic.id,
|
|
|
|
user_id: user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread
|
|
|
|
Unread.new(topic, topic_user, Guardian.new(user))
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'staff counts' do
|
|
|
|
it 'shoule correctly return based on staff post number' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-12-02 01:03:31 -05:00
|
|
|
user.admin = true
|
|
|
|
|
|
|
|
topic_user.last_read_post_number = 13
|
|
|
|
topic_user.highest_seen_post_number = 13
|
|
|
|
|
|
|
|
expect(unread.unread_posts).to eq(0)
|
|
|
|
expect(unread.new_posts).to eq(2)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'unread_posts' do
|
|
|
|
it 'should have 0 unread posts if the user has seen all posts' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.last_read_post_number = 13
|
|
|
|
topic_user.highest_seen_post_number = 13
|
|
|
|
expect(unread.unread_posts).to eq(0)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'should have 6 unread posts if the user has seen all but 6 posts' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.last_read_post_number = 5
|
|
|
|
topic_user.highest_seen_post_number = 11
|
|
|
|
expect(unread.unread_posts).to eq(6)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'should have 0 unread posts if the user has seen more posts than exist (deleted)' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.last_read_post_number = 100
|
|
|
|
topic_user.highest_seen_post_number = 13
|
|
|
|
expect(unread.unread_posts).to eq(0)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'new_posts' do
|
|
|
|
it 'should have 0 new posts if the user has read all posts' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.last_read_post_number = 13
|
|
|
|
expect(unread.new_posts).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 0 when the topic is the same length as when you last saw it' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.highest_seen_post_number = 13
|
|
|
|
expect(unread.new_posts).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has 3 new posts if the user has read 10 posts' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.highest_seen_post_number = 10
|
|
|
|
expect(unread.new_posts).to eq(3)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'has 0 new posts if the user has read 10 posts but is not tracking' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.highest_seen_post_number = 10
|
|
|
|
topic_user.notification_level = TopicUser.notification_levels[:regular]
|
|
|
|
expect(unread.new_posts).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has 0 new posts if the user read more posts than exist (deleted)' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.highest_seen_post_number = 16
|
|
|
|
expect(unread.new_posts).to eq(0)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2016-12-02 01:03:31 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|