discourse/spec/components/suggested_topics_builder_spec.rb

114 lines
3.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'rails_helper'
2013-07-13 02:38:20 +08:00
require 'suggested_topics_builder'
describe SuggestedTopicsBuilder do
let(:topic) { Fabricate(:topic) }
let(:builder) { SuggestedTopicsBuilder.new(topic) }
2013-07-13 02:38:20 +08:00
before do
SiteSetting.suggested_topics = 5
2013-07-13 02:38:20 +08:00
end
context "splicing category results" do
def fake_topic(topic_id, category_id)
build(:topic, id: topic_id, category_id: category_id)
end
let(:builder) do
2017-07-28 09:20:09 +08:00
SuggestedTopicsBuilder.new(fake_topic(1, 1))
end
it "prioritizes category correctly" do
2017-07-28 09:20:09 +08:00
builder.splice_results([fake_topic(2, 2)], :high)
builder.splice_results([fake_topic(3, 1)], :high)
builder.splice_results([fake_topic(4, 1)], :high)
2017-07-28 09:20:09 +08:00
expect(builder.results.map(&:id)).to eq([3, 4, 2])
# we have 2 items in category 1
2015-01-10 00:34:37 +08:00
expect(builder.category_results_left).to eq(3)
end
it "inserts using default approach for non high priority" do
2017-07-28 09:20:09 +08:00
builder.splice_results([fake_topic(2, 2)], :high)
builder.splice_results([fake_topic(3, 1)], :low)
2017-07-28 09:20:09 +08:00
expect(builder.results.map(&:id)).to eq([2, 3])
end
it "inserts multiple results and puts topics in the correct order" do
2017-07-28 09:20:09 +08:00
builder.splice_results([fake_topic(2, 1), fake_topic(3, 2), fake_topic(4, 1)], :high)
expect(builder.results.map(&:id)).to eq([2, 4, 3])
end
end
2013-07-13 02:38:20 +08:00
it "has the correct defaults" do
2015-01-10 00:34:37 +08:00
expect(builder.excluded_topic_ids.include?(topic.id)).to eq(true)
expect(builder.results_left).to eq(5)
expect(builder.size).to eq(0)
expect(builder).not_to be_full
2013-07-13 02:38:20 +08:00
end
it "returns full correctly" do
builder.stubs(:results_left).returns(0)
2015-01-10 00:34:37 +08:00
expect(builder).to be_full
2013-07-13 02:38:20 +08:00
end
context "adding results" do
it "adds nothing with nil results" do
builder.add_results(nil)
2015-01-10 00:34:37 +08:00
expect(builder.results_left).to eq(5)
expect(builder.size).to eq(0)
expect(builder).not_to be_full
2013-07-13 02:38:20 +08:00
end
context "adding topics" do
let!(:other_topic) { Fabricate(:topic) }
before do
# Add all topics
builder.add_results(Topic)
end
it "added the result correctly" do
2015-01-10 00:34:37 +08:00
expect(builder.size).to eq(1)
expect(builder.results_left).to eq(4)
expect(builder).not_to be_full
expect(builder.excluded_topic_ids.include?(topic.id)).to eq(true)
expect(builder.excluded_topic_ids.include?(other_topic.id)).to eq(true)
2013-07-13 02:38:20 +08:00
end
end
context "adding topics that are not open" do
2017-07-28 09:20:09 +08:00
let!(:archived_topic) { Fabricate(:topic, archived: true) }
let!(:closed_topic) { Fabricate(:topic, closed: true) }
let!(:invisible_topic) { Fabricate(:topic, visible: false) }
2013-07-13 02:38:20 +08:00
it "adds archived and closed, but not invisible topics" do
2013-07-13 02:38:20 +08:00
builder.add_results(Topic)
2015-01-10 00:34:37 +08:00
expect(builder.size).to eq(2)
expect(builder).not_to be_full
2013-07-13 02:38:20 +08:00
end
end
context "category definition topics" do
let!(:category) { Fabricate(:category) }
it "doesn't add a category definition topic" do
2015-01-10 00:34:37 +08:00
expect(category.topic_id).to be_present
builder.add_results(Topic)
2015-01-10 00:34:37 +08:00
expect(builder.size).to eq(0)
expect(builder).not_to be_full
end
end
2013-07-13 02:38:20 +08:00
end
end