2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-16 17:31:39 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe TopicList do
|
2017-02-15 05:29:06 +08:00
|
|
|
let!(:topic) {
|
|
|
|
t = Fabricate(:topic)
|
|
|
|
t.allowed_user_ids = [t.user.id]
|
|
|
|
t
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:31:39 +08:00
|
|
|
let(:user) { topic.user }
|
|
|
|
let(:topic_list) { TopicList.new("liked", user, [topic]) }
|
|
|
|
|
2016-06-09 22:01:48 +08:00
|
|
|
before do
|
|
|
|
TopicList.preloaded_custom_fields.clear
|
|
|
|
end
|
|
|
|
|
2016-05-16 17:31:39 +08:00
|
|
|
after do
|
|
|
|
TopicList.preloaded_custom_fields.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".preloaded_custom_fields" do
|
|
|
|
it "should return a unique set of values" do
|
|
|
|
TopicList.preloaded_custom_fields << "test"
|
|
|
|
TopicList.preloaded_custom_fields << "test"
|
|
|
|
TopicList.preloaded_custom_fields << "apple"
|
|
|
|
|
|
|
|
expect(TopicList.preloaded_custom_fields).to eq(Set.new(%w{test apple}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-15 05:29:06 +08:00
|
|
|
context "preload" do
|
|
|
|
it "allows preloading of data" do
|
|
|
|
preloaded_topic = false
|
2017-02-16 01:04:02 +08:00
|
|
|
preloader = lambda do |topics, topic_list|
|
|
|
|
expect(TopicList === topic_list).to eq(true)
|
2017-02-15 05:29:06 +08:00
|
|
|
expect(topics.length).to eq(1)
|
|
|
|
preloaded_topic = true
|
|
|
|
end
|
|
|
|
|
2017-02-15 05:32:33 +08:00
|
|
|
TopicList.on_preload(&preloader)
|
2017-02-15 05:29:06 +08:00
|
|
|
|
|
|
|
topic_list.topics
|
|
|
|
expect(preloaded_topic).to eq(true)
|
|
|
|
|
2017-02-15 05:32:33 +08:00
|
|
|
TopicList.cancel_preload(&preloader)
|
2017-02-15 05:29:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-13 04:26:13 +08:00
|
|
|
describe '#top_tags' do
|
2016-07-07 21:17:56 +08:00
|
|
|
it 'should return the right tags' do
|
2016-07-09 05:13:32 +08:00
|
|
|
tag = Fabricate(:tag, topics: [topic])
|
|
|
|
other_tag = Fabricate(:tag, topics: [topic], name: "use-anywhere")
|
2016-07-07 21:17:56 +08:00
|
|
|
output = [tag.name, other_tag.name]
|
2018-01-13 04:26:13 +08:00
|
|
|
expect(topic_list.top_tags.sort).to eq(output.sort)
|
2016-07-07 21:17:56 +08:00
|
|
|
end
|
|
|
|
|
2016-07-09 05:13:32 +08:00
|
|
|
describe 'when there are tags restricted to a category' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
fab!(:other_topic) { Fabricate(:topic) } # uncategorized
|
|
|
|
fab!(:tag) { Fabricate(:tag, topics: [topic], categories: [category], name: "category-tag") }
|
|
|
|
fab!(:other_tag) { Fabricate(:tag, topics: [topic], name: "use-anywhere") }
|
2017-07-28 09:20:09 +08:00
|
|
|
let(:topic_list) { TopicList.new('latest', topic.user, [topic], category: category.id, category_id: category.id) }
|
2016-07-07 21:17:56 +08:00
|
|
|
|
2018-02-09 05:36:39 +08:00
|
|
|
it 'should return tags used in the category' do
|
|
|
|
expect(topic_list.top_tags).to eq([tag.name, other_tag.name].sort)
|
2016-07-09 05:13:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "with no category, should return all tags" do
|
2018-01-13 04:26:13 +08:00
|
|
|
expect(TopicList.new('latest', other_topic.user, [other_topic]).top_tags.sort).to eq([tag.name, other_tag.name].sort)
|
2016-07-09 05:13:32 +08:00
|
|
|
end
|
2016-07-07 21:17:56 +08:00
|
|
|
|
2016-09-23 03:23:10 +08:00
|
|
|
it "with another category with no tags, should return no tags" do
|
2016-07-09 05:13:32 +08:00
|
|
|
other_category = Fabricate(:category)
|
|
|
|
topic3 = Fabricate(:topic, category: other_category)
|
2017-07-28 09:20:09 +08:00
|
|
|
list = TopicList.new('latest', topic3.user, [topic3], category: other_category.id, category_id: other_category.id)
|
2018-01-13 04:26:13 +08:00
|
|
|
expect(list.top_tags).to be_empty
|
2016-07-07 21:17:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-16 17:31:39 +08:00
|
|
|
end
|