2021-02-04 08:27:34 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe DismissTopics do
|
2021-02-15 05:50:33 +08:00
|
|
|
fab!(:user) { Fabricate(:user, created_at: 1.days.ago) }
|
2021-02-04 08:27:34 +08:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
fab!(:topic1) { Fabricate(:topic, category: category, created_at: 60.minutes.ago) }
|
|
|
|
fab!(:topic2) { Fabricate(:topic, category: category, created_at: 120.minutes.ago) }
|
|
|
|
|
|
|
|
describe '#perform!' do
|
|
|
|
it 'dismisses two topics' do
|
|
|
|
expect { described_class.new(user, Topic.all).perform! }.to change { DismissedTopicUser.count }.by(2)
|
|
|
|
end
|
|
|
|
|
2021-02-15 05:50:33 +08:00
|
|
|
it 'returns dismissed topic ids' do
|
|
|
|
expect(described_class.new(user, Topic.all).perform!.sort).to eq([topic1.id, topic2.id])
|
|
|
|
end
|
|
|
|
|
2021-02-04 08:27:34 +08:00
|
|
|
it 'respects max_new_topics limit' do
|
|
|
|
SiteSetting.max_new_topics = 1
|
|
|
|
expect { described_class.new(user, Topic.all).perform! }.to change { DismissedTopicUser.count }.by(1)
|
|
|
|
|
|
|
|
dismissed_topic_user = DismissedTopicUser.last
|
|
|
|
|
|
|
|
expect(dismissed_topic_user.user_id).to eq(user.id)
|
|
|
|
expect(dismissed_topic_user.topic_id).to eq(topic1.id)
|
|
|
|
expect(dismissed_topic_user.created_at).not_to be_nil
|
|
|
|
end
|
|
|
|
|
2021-02-09 07:39:30 +08:00
|
|
|
it 'respects seen topics' do
|
2021-02-18 07:39:05 +08:00
|
|
|
Fabricate(:topic_user, user: user, topic: topic1, last_read_post_number: 1)
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic2, last_read_post_number: 1)
|
2021-02-09 07:39:30 +08:00
|
|
|
expect { described_class.new(user, Topic.all).perform! }.to change { DismissedTopicUser.count }.by(0)
|
|
|
|
end
|
|
|
|
|
2021-02-18 07:39:05 +08:00
|
|
|
it 'dismisses when topic user without last_read_post_number' do
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic1, last_read_post_number: nil)
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic2, last_read_post_number: nil)
|
|
|
|
expect { described_class.new(user, Topic.all).perform! }.to change { DismissedTopicUser.count }.by(2)
|
|
|
|
end
|
|
|
|
|
2021-02-04 08:27:34 +08:00
|
|
|
it 'respects new_topic_duration_minutes' do
|
|
|
|
user.user_option.update!(new_topic_duration_minutes: 70)
|
|
|
|
|
|
|
|
expect { described_class.new(user, Topic.all).perform! }.to change { DismissedTopicUser.count }.by(1)
|
|
|
|
|
|
|
|
dismissed_topic_user = DismissedTopicUser.last
|
|
|
|
|
|
|
|
expect(dismissed_topic_user.user_id).to eq(user.id)
|
|
|
|
expect(dismissed_topic_user.topic_id).to eq(topic1.id)
|
|
|
|
expect(dismissed_topic_user.created_at).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|